1. /*
  2. * @(#)DateTimeSyntax.java 1.3 03/01/23
  3. *
  4. * Copyright 2003 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. // Hidden data members.
  40. /**
  41. * This date-time attribute's<code>java.util.Date</code> value.
  42. * @serial
  43. */
  44. private Date value;
  45. // Hidden constructors.
  46. /**
  47. * Construct a new date-time attribute with the given
  48. * <code>java.util.Date </code> value.
  49. *
  50. * @param value <code>java.util.Date</code> value.
  51. *
  52. * @exception NullPointerException
  53. * (unchecked exception) Thrown if <CODE>theValue</CODE> is null.
  54. */
  55. protected DateTimeSyntax(Date value) {
  56. if (value == null) {
  57. throw new NullPointerException("value is null");
  58. }
  59. this.value = value;
  60. }
  61. // Exported operations.
  62. /**
  63. * Returns this date-time attribute's <code>java.util.Date</code>
  64. * value.
  65. * @return the Date.
  66. */
  67. public Date getValue() {
  68. return new Date (value.getTime());
  69. }
  70. // Exported operations inherited and overridden from class Object.
  71. /**
  72. * Returns whether this date-time attribute is equivalent to the passed in
  73. * object. To be equivalent, all of the following conditions must be true:
  74. * <OL TYPE=1>
  75. * <LI>
  76. * <CODE>object</CODE> is not null.
  77. * <LI>
  78. * <CODE>object</CODE> is an instance of class DateTimeSyntax.
  79. * <LI>
  80. * This date-time attribute's <code>java.util.Date</code> value and
  81. * <CODE>object</CODE>'s <code>java.util.Date</code> value are
  82. * equal. </OL>
  83. *
  84. * @param object Object to compare to.
  85. *
  86. * @return True if <CODE>object</CODE> is equivalent to this date-time
  87. * attribute, false otherwise.
  88. */
  89. public boolean equals(Object object) {
  90. return (object != null &&
  91. object instanceof DateTimeSyntax &&
  92. value.equals(((DateTimeSyntax) object).value));
  93. }
  94. /**
  95. * Returns a hash code value for this date-time attribute. The hashcode is
  96. * that of this attribute's <code>java.util.Date</code> value.
  97. */
  98. public int hashCode() {
  99. return value.hashCode();
  100. }
  101. /**
  102. * Returns a string value corresponding to this date-time attribute.
  103. * The string value is just this attribute's
  104. * <code>java.util.Date</code> value
  105. * converted to a string.
  106. */
  107. public String toString() {
  108. return "" + value;
  109. }
  110. }