Path: blob/master/src/java.desktop/share/classes/javax/print/attribute/DateTimeSyntax.java
41159 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.print.attribute;2627import java.io.Serial;28import java.io.Serializable;29import java.util.Date;3031/**32* Class {@code DateTimeSyntax} is an abstract base class providing the common33* implementation of all attributes whose value is a date and time.34* <p>35* Under the hood, a date-time attribute is stored as a value of class36* {@code java.util.Date}. You can get a date-time attribute's {@code Date}37* value by calling {@link #getValue() getValue()}. A date-time attribute's38* {@code Date} value is established when it is constructed (see39* {@link #DateTimeSyntax(Date) DateTimeSyntax(Date)}). Once constructed, a40* date-time attribute's value is immutable.41* <p>42* To construct a date-time attribute from separate values of the year, month,43* day, hour, minute, and so on, use a {@code java.util.Calendar} object to44* construct a {@code java.util.Date} object, then use the45* {@code java.util.Date} object to construct the date-time attribute. To46* convert a date-time attribute to separate values of the year, month, day,47* hour, minute, and so on, create a {@code java.util.Calendar} object and set48* it to the {@code java.util.Date} from the date-time attribute. Class49* {@code DateTimeSyntax} stores its value in the form of a50* {@code java.util.Date} rather than a {@code java.util.Calendar} because it51* typically takes less memory to store and less time to compare a52* {@code java.util.Date} than a {@code java.util.Calendar}.53*54* @author Alan Kaminsky55*/56public abstract class DateTimeSyntax implements Serializable, Cloneable {5758/**59* Use serialVersionUID from JDK 1.4 for interoperability.60*/61@Serial62private static final long serialVersionUID = -1400819079791208582L;6364// Hidden data members.6566/**67* This date-time attribute's {@code java.util.Date} value.68*69* @serial70*/71private Date value;7273// Hidden constructors.7475/**76* Construct a new date-time attribute with the given {@code java.util.Date}77* value.78*79* @param value {@code java.util.Date} value80* @throws NullPointerException if {@code value} is {@code null}81*/82protected DateTimeSyntax(Date value) {83if (value == null) {84throw new NullPointerException("value is null");85}86this.value = value;87}8889// Exported operations.9091/**92* Returns this date-time attribute's {@code java.util.Date} value.93*94* @return the {@code Date}95*/96public Date getValue() {97return new Date (value.getTime());98}99100// Exported operations inherited and overridden from class Object.101102/**103* Returns whether this date-time attribute is equivalent to the passed in104* object. To be equivalent, all of the following conditions must be true:105* <ol type=1>106* <li>{@code object} is not {@code null}.107* <li>{@code object} is an instance of class {@code DateTimeSyntax}.108* <li>This date-time attribute's {@code java.util.Date} value and109* {@code object}'s {@code java.util.Date} value are equal.110* </ol>111*112* @param object {@code Object} to compare to113* @return {@code true} if {@code object} is equivalent to this date-time114* attribute, {@code false} otherwise115*/116public boolean equals(Object object) {117return (object != null &&118object instanceof DateTimeSyntax &&119value.equals(((DateTimeSyntax) object).value));120}121122/**123* Returns a hash code value for this date-time attribute. The hashcode is124* that of this attribute's {@code java.util.Date} value.125*/126public int hashCode() {127return value.hashCode();128}129130/**131* Returns a string value corresponding to this date-time attribute. The132* string value is just this attribute's {@code java.util.Date} value133* converted to a string.134*/135public String toString() {136return "" + value;137}138}139140141