KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > print > attribute > IntegerSyntax


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
8 package javax.print.attribute;
9
10 import java.io.Serializable JavaDoc;
11
12 /**
13  * Class IntegerSyntax is an abstract base class providing the common
14  * implementation of all attributes with integer values.
15  * <P>
16  * Under the hood, an integer attribute is just an integer. You can get an
17  * integer attribute's integer value by calling {@link #getValue()
18  * <CODE>getValue()</CODE>}. An integer attribute's integer value is
19  * established when it is constructed (see {@link #IntegerSyntax(int)
20  * <CODE>IntegerSyntax(int)</CODE>}). Once constructed, an integer attribute's
21  * value is immutable.
22  * <P>
23  *
24  * @author David Mendenhall
25  * @author Alan Kaminsky
26  */

27 public abstract class IntegerSyntax implements Serializable JavaDoc, Cloneable JavaDoc {
28
29     private static final long serialVersionUID = 3644574816328081943L;
30
31     /**
32      * This integer attribute's integer value.
33      * @serial
34      */

35     private int value;
36
37     /**
38      * Construct a new integer attribute with the given integer value.
39      *
40      * @param value Integer value.
41      */

42     protected IntegerSyntax(int value) {
43     this.value = value;
44     }
45
46     /**
47      * Construct a new integer attribute with the given integer value, which
48      * must lie within the given range.
49      *
50      * @param value Integer value.
51      * @param lowerBound Lower bound.
52      * @param upperBound Upper bound.
53      *
54      * @exception IllegalArgumentException
55      * (Unchecked exception) Thrown if <CODE>value</CODE> is less than
56      * <CODE>lowerBound</CODE> or greater than
57      * <CODE>upperBound</CODE>.
58      */

59     protected IntegerSyntax(int value, int lowerBound, int upperBound) {
60     if (lowerBound > value || value > upperBound) {
61         throw new IllegalArgumentException JavaDoc("Value " + value +
62                            " not in range " + lowerBound +
63                            ".." + upperBound);
64     }
65     this.value = value;
66     }
67
68     /**
69      * Returns this integer attribute's integer value.
70      * @return the integer value
71      */

72     public int getValue() {
73     return value;
74     }
75
76     /**
77      * Returns whether this integer attribute is equivalent to the passed in
78      * object. To be equivalent, all of the following conditions must be true:
79      * <OL TYPE=1>
80      * <LI>
81      * <CODE>object</CODE> is not null.
82      * <LI>
83      * <CODE>object</CODE> is an instance of class IntegerSyntax.
84      * <LI>
85      * This integer attribute's value and <CODE>object</CODE>'s value are
86      * equal.
87      * </OL>
88      *
89      * @param object Object to compare to.
90      *
91      * @return True if <CODE>object</CODE> is equivalent to this integer
92      * attribute, false otherwise.
93      */

94     public boolean equals(Object JavaDoc object) {
95
96     return (object != null && object instanceof IntegerSyntax JavaDoc &&
97         value == ((IntegerSyntax JavaDoc) object).value);
98     }
99
100     /**
101      * Returns a hash code value for this integer attribute. The hash code is
102      * just this integer attribute's integer value.
103      */

104     public int hashCode() {
105     return value;
106     }
107
108     /**
109      * Returns a string value corresponding to this integer attribute. The
110      * string value is just this integer attribute's integer value converted to
111      * a string.
112      */

113     public String JavaDoc toString() {
114     return "" + value;
115     }
116 }
117
Popular Tags