1. /*
  2. * @(#)IntegerSyntax.java 1.6 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. /**
  10. * Class IntegerSyntax is an abstract base class providing the common
  11. * implementation of all attributes with integer values.
  12. * <P>
  13. * Under the hood, an integer attribute is just an integer. You can get an
  14. * integer attribute's integer value by calling {@link #getValue()
  15. * <CODE>getValue()</CODE>}. An integer attribute's integer value is
  16. * established when it is constructed (see {@link #IntegerSyntax(int)
  17. * <CODE>IntegerSyntax(int)</CODE>}). Once constructed, an integer attribute's
  18. * value is immutable.
  19. * <P>
  20. *
  21. * @author David Mendenhall
  22. * @author Alan Kaminsky
  23. */
  24. public abstract class IntegerSyntax implements Serializable, Cloneable {
  25. private static final long serialVersionUID = 3644574816328081943L;
  26. /**
  27. * This integer attribute's integer value.
  28. * @serial
  29. */
  30. private int value;
  31. /**
  32. * Construct a new integer attribute with the given integer value.
  33. *
  34. * @param value Integer value.
  35. */
  36. protected IntegerSyntax(int value) {
  37. this.value = value;
  38. }
  39. /**
  40. * Construct a new integer attribute with the given integer value, which
  41. * must lie within the given range.
  42. *
  43. * @param value Integer value.
  44. * @param lowerBound Lower bound.
  45. * @param upperBound Upper bound.
  46. *
  47. * @exception IllegalArgumentException
  48. * (Unchecked exception) Thrown if <CODE>value</CODE> is less than
  49. * <CODE>lowerBound</CODE> or greater than
  50. * <CODE>upperBound</CODE>.
  51. */
  52. protected IntegerSyntax(int value, int lowerBound, int upperBound) {
  53. if (lowerBound > value || value > upperBound) {
  54. throw new IllegalArgumentException("Value " + value +
  55. " not in range " + lowerBound +
  56. ".." + upperBound);
  57. }
  58. this.value = value;
  59. }
  60. /**
  61. * Returns this integer attribute's integer value.
  62. * @return the integer value
  63. */
  64. public int getValue() {
  65. return value;
  66. }
  67. /**
  68. * Returns whether this integer attribute is equivalent to the passed in
  69. * object. To be equivalent, all of the following conditions must be true:
  70. * <OL TYPE=1>
  71. * <LI>
  72. * <CODE>object</CODE> is not null.
  73. * <LI>
  74. * <CODE>object</CODE> is an instance of class IntegerSyntax.
  75. * <LI>
  76. * This integer attribute's value and <CODE>object</CODE>'s value are
  77. * equal.
  78. * </OL>
  79. *
  80. * @param object Object to compare to.
  81. *
  82. * @return True if <CODE>object</CODE> is equivalent to this integer
  83. * attribute, false otherwise.
  84. */
  85. public boolean equals(Object object) {
  86. return (object != null && object instanceof IntegerSyntax &&
  87. value == ((IntegerSyntax) object).value);
  88. }
  89. /**
  90. * Returns a hash code value for this integer attribute. The hash code is
  91. * just this integer attribute's integer value.
  92. */
  93. public int hashCode() {
  94. return value;
  95. }
  96. /**
  97. * Returns a string value corresponding to this integer attribute. The
  98. * string value is just this integer attribute's integer value converted to
  99. * a string.
  100. */
  101. public String toString() {
  102. return "" + value;
  103. }
  104. }