001 /*
002 * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.print.attribute;
027
028 import java.io.Serializable;
029
030 /**
031 * Class IntegerSyntax is an abstract base class providing the common
032 * implementation of all attributes with integer values.
033 * <P>
034 * Under the hood, an integer attribute is just an integer. You can get an
035 * integer attribute's integer value by calling {@link #getValue()
036 * <CODE>getValue()</CODE>}. An integer attribute's integer value is
037 * established when it is constructed (see {@link #IntegerSyntax(int)
038 * <CODE>IntegerSyntax(int)</CODE>}). Once constructed, an integer attribute's
039 * value is immutable.
040 * <P>
041 *
042 * @author David Mendenhall
043 * @author Alan Kaminsky
044 */
045 public abstract class IntegerSyntax implements Serializable, Cloneable {
046
047 private static final long serialVersionUID = 3644574816328081943L;
048
049 /**
050 * This integer attribute's integer value.
051 * @serial
052 */
053 private int value;
054
055 /**
056 * Construct a new integer attribute with the given integer value.
057 *
058 * @param value Integer value.
059 */
060 protected IntegerSyntax(int value) {
061 this .value = value;
062 }
063
064 /**
065 * Construct a new integer attribute with the given integer value, which
066 * must lie within the given range.
067 *
068 * @param value Integer value.
069 * @param lowerBound Lower bound.
070 * @param upperBound Upper bound.
071 *
072 * @exception IllegalArgumentException
073 * (Unchecked exception) Thrown if <CODE>value</CODE> is less than
074 * <CODE>lowerBound</CODE> or greater than
075 * <CODE>upperBound</CODE>.
076 */
077 protected IntegerSyntax(int value, int lowerBound, int upperBound) {
078 if (lowerBound > value || value > upperBound) {
079 throw new IllegalArgumentException("Value " + value
080 + " not in range " + lowerBound + ".." + upperBound);
081 }
082 this .value = value;
083 }
084
085 /**
086 * Returns this integer attribute's integer value.
087 * @return the integer value
088 */
089 public int getValue() {
090 return value;
091 }
092
093 /**
094 * Returns whether this integer attribute is equivalent to the passed in
095 * object. To be equivalent, all of the following conditions must be true:
096 * <OL TYPE=1>
097 * <LI>
098 * <CODE>object</CODE> is not null.
099 * <LI>
100 * <CODE>object</CODE> is an instance of class IntegerSyntax.
101 * <LI>
102 * This integer attribute's value and <CODE>object</CODE>'s value are
103 * equal.
104 * </OL>
105 *
106 * @param object Object to compare to.
107 *
108 * @return True if <CODE>object</CODE> is equivalent to this integer
109 * attribute, false otherwise.
110 */
111 public boolean equals(Object object) {
112
113 return (object != null && object instanceof IntegerSyntax && value == ((IntegerSyntax) object).value);
114 }
115
116 /**
117 * Returns a hash code value for this integer attribute. The hash code is
118 * just this integer attribute's integer value.
119 */
120 public int hashCode() {
121 return value;
122 }
123
124 /**
125 * Returns a string value corresponding to this integer attribute. The
126 * string value is just this integer attribute's integer value converted to
127 * a string.
128 */
129 public String toString() {
130 return "" + value;
131 }
132 }
|