1. /*
  2. * @(#)IntegerSyntax.java 1.4 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. /**
  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. /**
  26. * This integer attribute's integer value.
  27. * @serial
  28. */
  29. private int value;
  30. /**
  31. * Construct a new integer attribute with the given integer value.
  32. *
  33. * @param value Integer value.
  34. */
  35. protected IntegerSyntax(int value) {
  36. this.value = value;
  37. }
  38. /**
  39. * Construct a new integer attribute with the given integer value, which
  40. * must lie within the given range.
  41. *
  42. * @param value Integer value.
  43. * @param lowerBound Lower bound.
  44. * @param upperBound Upper bound.
  45. *
  46. * @exception IllegalArgumentException
  47. * (Unchecked exception) Thrown if <CODE>value</CODE> is less than
  48. * <CODE>lowerBound</CODE> or greater than
  49. * <CODE>upperBound</CODE>.
  50. */
  51. protected IntegerSyntax(int value, int lowerBound, int upperBound) {
  52. if (lowerBound > value || value > upperBound) {
  53. throw new IllegalArgumentException("Value " + value +
  54. " not in range " + lowerBound +
  55. ".." + upperBound);
  56. }
  57. this.value = value;
  58. }
  59. /**
  60. * Returns this integer attribute's integer value.
  61. * @return the integer value
  62. */
  63. public int getValue() {
  64. return value;
  65. }
  66. /**
  67. * Returns whether this integer attribute is equivalent to the passed in
  68. * object. To be equivalent, all of the following conditions must be true:
  69. * <OL TYPE=1>
  70. * <LI>
  71. * <CODE>object</CODE> is not null.
  72. * <LI>
  73. * <CODE>object</CODE> is an instance of class IntegerSyntax.
  74. * <LI>
  75. * This integer attribute's value and <CODE>object</CODE>'s value are
  76. * equal.
  77. * </OL>
  78. *
  79. * @param object Object to compare to.
  80. *
  81. * @return True if <CODE>object</CODE> is equivalent to this integer
  82. * attribute, false otherwise.
  83. */
  84. public boolean equals(Object object) {
  85. return (object != null && object instanceof IntegerSyntax &&
  86. value == ((IntegerSyntax) object).value);
  87. }
  88. /**
  89. * Returns a hash code value for this integer attribute. The hash code is
  90. * just this integer attribute's integer value.
  91. */
  92. public int hashCode() {
  93. return value;
  94. }
  95. /**
  96. * Returns a string value corresponding to this integer attribute. The
  97. * string value is just this integer attribute's integer value converted to
  98. * a string.
  99. */
  100. public String toString() {
  101. return "" + value;
  102. }
  103. }