1. /*
  2. * @(#)DateTimeSyntax.java 1.5 04/01/07
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.print.attribute;
  8. import java.io.Serializable;
  9. import java.util.Date;
  10. /**
  11. * Class DateTimeSyntax is an abstract base class providing the common
  12. * implementation of all attributes whose value is a date and time.
  13. * <P>
  14. * Under the hood, a date-time attribute is stored as a value of class <code>
  15. * java.util.Date</code>}. You can get a date-time attribute's Date value by
  16. * calling {@link #getValue() <CODE>getValue()</CODE>}. A date-time attribute's
  17. * Date value is established when it is constructed (see {@link
  18. * #DateTimeSyntax(Date) <CODE>DateTimeSyntax(Date)</CODE>}). Once
  19. * constructed, a date-time attribute's value is immutable.
  20. * <P>
  21. * To construct a date-time attribute from separate values of the year, month,
  22. * day, hour, minute, and so on, use a <code>java.util.Calendar</code>
  23. * object to construct a <code>java.util.Date</code> object, then use the
  24. * <code>java.util.Date</code >object to construct the date-time attribute.
  25. * To convert
  26. * a date-time attribute to separate values of the year, month, day, hour,
  27. * minute, and so on, create a <code>java.util.Calendar</code> object and
  28. * set it to the <code>java.util.Date</code> from the date-time attribute. Class
  29. * DateTimeSyntax stores its value in the form of a <code>java.util.Date
  30. * </code>
  31. * rather than a <code>java.util.Calendar</code> because it typically takes
  32. * less memory to store and less time to compare a <code>java.util.Date</code>
  33. * than a <code>java.util.Calendar</code>.
  34. * <P>
  35. *
  36. * @author Alan Kaminsky
  37. */
  38. public abstract class DateTimeSyntax implements Serializable, Cloneable {
  39. private static final long serialVersionUID = -1400819079791208582L;
  40. // Hidden data members.
  41. /**
  42. * This date-time attribute's<code>java.util.Date</code> value.
  43. * @serial
  44. */
  45. private Date value;
  46. // Hidden constructors.
  47. /**
  48. * Construct a new date-time attribute with the given
  49. * <code>java.util.Date </code> value.
  50. *
  51. * @param value <code>java.util.Date</code> value.
  52. *
  53. * @exception NullPointerException
  54. * (unchecked exception) Thrown if <CODE>theValue</CODE> is null.
  55. */
  56. protected DateTimeSyntax(Date value) {
  57. if (value == null) {
  58. throw new NullPointerException("value is null");
  59. }
  60. this.value = value;
  61. }
  62. // Exported operations.
  63. /**
  64. * Returns this date-time attribute's <code>java.util.Date</code>
  65. * value.
  66. * @return the Date.
  67. */
  68. public Date getValue() {
  69. return new Date (value.getTime());
  70. }
  71. // Exported operations inherited and overridden from class Object.
  72. /**
  73. * Returns whether this date-time attribute is equivalent to the passed in
  74. * object. To be equivalent, all of the following conditions must be true:
  75. * <OL TYPE=1>
  76. * <LI>
  77. * <CODE>object</CODE> is not null.
  78. * <LI>
  79. * <CODE>object</CODE> is an instance of class DateTimeSyntax.
  80. * <LI>
  81. * This date-time attribute's <code>java.util.Date</code> value and
  82. * <CODE>object</CODE>'s <code>java.util.Date</code> value are
  83. * equal. </OL>
  84. *
  85. * @param object Object to compare to.
  86. *
  87. * @return True if <CODE>object</CODE> is equivalent to this date-time
  88. * attribute, false otherwise.
  89. */
  90. public boolean equals(Object object) {
  91. return (object != null &&
  92. object instanceof DateTimeSyntax &&
  93. value.equals(((DateTimeSyntax) object).value));
  94. }
  95. /**
  96. * Returns a hash code value for this date-time attribute. The hashcode is
  97. * that of this attribute's <code>java.util.Date</code> value.
  98. */
  99. public int hashCode() {
  100. return value.hashCode();
  101. }
  102. /**
  103. * Returns a string value corresponding to this date-time attribute.
  104. * The string value is just this attribute's
  105. * <code>java.util.Date</code> value
  106. * converted to a string.
  107. */
  108. public String toString() {
  109. return "" + value;
  110. }
  111. }