View Javadoc
1   /*
2    * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package javax.print.attribute;
27  
28  import java.io.Serializable;
29  
30  /**
31   * Class IntegerSyntax is an abstract base class providing the common
32   * implementation of all attributes with integer values.
33   * <P>
34   * Under the hood, an integer attribute is just an integer. You can get an
35   * integer attribute's integer value by calling {@link #getValue()
36   * getValue()}. An integer attribute's integer value is
37   * established when it is constructed (see {@link #IntegerSyntax(int)
38   * IntegerSyntax(int)}). Once constructed, an integer attribute's
39   * value is immutable.
40   * <P>
41   *
42   * @author  David Mendenhall
43   * @author  Alan Kaminsky
44   */
45  public abstract class IntegerSyntax implements Serializable, Cloneable {
46  
47      private static final long serialVersionUID = 3644574816328081943L;
48  
49      /**
50       * This integer attribute's integer value.
51       * @serial
52       */
53      private int value;
54  
55      /**
56       * Construct a new integer attribute with the given integer value.
57       *
58       * @param  value  Integer value.
59       */
60      protected IntegerSyntax(int value) {
61          this.value = value;
62      }
63  
64      /**
65       * Construct a new integer attribute with the given integer value, which
66       * must lie within the given range.
67       *
68       * @param  value       Integer value.
69       * @param  lowerBound  Lower bound.
70       * @param  upperBound  Upper bound.
71       *
72       * @exception  IllegalArgumentException
73       *     (Unchecked exception) Thrown if <CODE>value</CODE> is less than
74       *     <CODE>lowerBound</CODE> or greater than
75       *     <CODE>upperBound</CODE>.
76       */
77      protected IntegerSyntax(int value, int lowerBound, int upperBound) {
78          if (lowerBound > value || value > upperBound) {
79              throw new IllegalArgumentException("Value " + value +
80                                                 " not in range " + lowerBound +
81                                                 ".." + upperBound);
82          }
83          this.value = value;
84      }
85  
86      /**
87       * Returns this integer attribute's integer value.
88       * @return the integer value
89       */
90      public int getValue() {
91          return value;
92      }
93  
94      /**
95       * Returns whether this integer attribute is equivalent to the passed in
96       * object. To be equivalent, all of the following conditions must be true:
97       * <OL TYPE=1>
98       * <LI>
99       * <CODE>object</CODE> is not null.
100      * <LI>
101      * <CODE>object</CODE> is an instance of class IntegerSyntax.
102      * <LI>
103      * This integer attribute's value and <CODE>object</CODE>'s value are
104      * equal.
105      * </OL>
106      *
107      * @param  object  Object to compare to.
108      *
109      * @return  True if <CODE>object</CODE> is equivalent to this integer
110      *          attribute, false otherwise.
111      */
112     public boolean equals(Object object) {
113 
114         return (object != null && object instanceof IntegerSyntax &&
115                 value == ((IntegerSyntax) object).value);
116     }
117 
118     /**
119      * Returns a hash code value for this integer attribute. The hash code is
120      * just this integer attribute's integer value.
121      */
122     public int hashCode() {
123         return value;
124     }
125 
126     /**
127      * Returns a string value corresponding to this integer attribute. The
128      * string value is just this integer attribute's integer value converted to
129      * a string.
130      */
131     public String toString() {
132         return "" + value;
133     }
134 }