001: /*
002: * $RCSfile: NegotiableNumeric.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:51 $
010: * $State: Exp $
011: */package javax.media.jai.remote;
012:
013: /**
014: * A class that wraps a numeric primitive data type or a subclass of
015: * <code>Number</code> to implement the <code>Negotiable</code> interface.
016: * <code>NegotiableNumeric</code> is a convenience class to specify a
017: * <code>Negotiable</code> value for a parameter which has a single
018: * valid numeric value.
019: *
020: * @since JAI 1.1
021: */
022: public class NegotiableNumeric implements Negotiable {
023:
024: // The numeric value stored as a Number subclass;
025: Number number;
026:
027: // The Class of the Number.
028: Class elementClass;
029:
030: /**
031: * Creates a <code>NegotiableNumeric</code> given a <code>byte</code>.
032: *
033: * @param b The <code>byte</code> to be wrapped to implement
034: * <code>Negotiable</code>.
035: */
036: public NegotiableNumeric(byte b) {
037: number = new Byte(b);
038: elementClass = number.getClass();
039: }
040:
041: /**
042: * Creates a <code>NegotiableNumeric</code> given a <code>short</code>.
043: *
044: * @param s The <code>short</code> to be wrapped to implement
045: * <code>Negotiable</code>.
046: */
047: public NegotiableNumeric(short s) {
048: number = new Short(s);
049: elementClass = number.getClass();
050: }
051:
052: /**
053: * Creates a <code>NegotiableNumeric</code> given an <code>int</code>.
054: *
055: * @param i The <code>int</code> to be wrapped to implement
056: * <code>Negotiable</code>.
057: */
058: public NegotiableNumeric(int i) {
059: number = new Integer(i);
060: elementClass = number.getClass();
061: }
062:
063: /**
064: * Creates a <code>NegotiableNumeric</code> given a <code>long</code>.
065: *
066: * @param l The <code>long</code> to be wrapped to implement
067: * <code>Negotiable</code>.
068: */
069: public NegotiableNumeric(long l) {
070: number = new Long(l);
071: elementClass = number.getClass();
072: }
073:
074: /**
075: * Creates a <code>NegotiableNumeric</code> given a <code>float</code>.
076: *
077: * @param f The <code>float</code> to be wrapped to implement
078: * <code>Negotiable</code>.
079: */
080: public NegotiableNumeric(float f) {
081: number = new Float(f);
082: elementClass = number.getClass();
083: }
084:
085: /**
086: * Creates a <code>NegotiableNumeric</code> given a <code>double</code>.
087: *
088: * @param d The <code>double</code> to be wrapped to implement
089: * <code>Negotiable</code>.
090: */
091: public NegotiableNumeric(double d) {
092: number = new Double(d);
093: elementClass = number.getClass();
094: }
095:
096: /**
097: * Creates a <code>NegotiableNumeric</code> given a <code>Number</code>.
098: *
099: * @param n The <code>Number</code> to be wrapped to implement
100: * <code>Negotiable</code>.
101: *
102: * @throws IllegalArgumentException if n is null.
103: */
104: public NegotiableNumeric(Number n) {
105:
106: if (n == null) {
107: throw new IllegalArgumentException(JaiI18N
108: .getString("NegotiableNumeric0"));
109: }
110:
111: number = n;
112: elementClass = number.getClass();
113: }
114:
115: /**
116: * Returns the <code>Number</code> that is currently the valid value
117: * for this class. A valid primitive data type value, such as int,
118: * will be returned as a member of the corresponding wrapper class,
119: * such as <code>Integer</code>.
120: */
121: public Number getNumber() {
122: return number;
123: }
124:
125: /**
126: * Returns a <code>NegotiableNumeric</code> that contains the value
127: * that is common to this <code>NegotiableNumeric</code>
128: * and the one supplied, i.e the <code>Number</code> encapsulated in
129: * both the <code>NegotiableNumeric</code> are equal. If the supplied
130: * <code>Negotiable</code> is not a <code>NegotiableNumeric</code> with
131: * its element being of the same <code>Class</code> as this class', or
132: * if there is no common value (i.e the values are not equal), the
133: * negotiation will fail and <code>null</code> will be returned.
134: *
135: * @param other The <code>Negotiable</code> to negotiate with.
136: */
137: public Negotiable negotiate(Negotiable other) {
138: if (other == null)
139: return null;
140:
141: if (!(other instanceof NegotiableNumeric)
142: || other.getNegotiatedValueClass() != elementClass) {
143: return null;
144: }
145:
146: NegotiableNumeric otherNN = (NegotiableNumeric) other;
147:
148: if (number.equals(otherNN.getNumber())) {
149: return new NegotiableNumeric(number);
150: } else {
151: return null;
152: }
153: }
154:
155: /**
156: * Returns the result of the negotiation as a <code>Number</code>
157: * subclass. Values belonging to a base type, such as <code>int</code>,
158: * will be returned as a member of the corresponding <code>Number</code>
159: * subclass, such as <code>Integer</code>.
160: */
161: public Object getNegotiatedValue() {
162: return number;
163: }
164:
165: /**
166: * Returns the <code>Class</code> of the negotiated value. Values
167: * belonging to a base type, such as <code>int</code>, will be returned
168: * as a member of the corresponding <code>Number</code> subclass, such as
169: * <code>Integer</code>. The <code>Class</code> returned similarly will be
170: * a <code>Number</code> subclass.
171: */
172: public Class getNegotiatedValueClass() {
173: return elementClass;
174: }
175:
176: /**
177: * A convenience method to return the single negotiated value as a
178: * <code>byte</code>.
179: *
180: * @throws ClassCastException if the value is of a different Class type.
181: */
182: public byte getNegotiatedValueAsByte() {
183: if (elementClass != Byte.class)
184: throw new ClassCastException(JaiI18N
185: .getString("NegotiableNumeric1"));
186: return number.byteValue();
187: }
188:
189: /**
190: * A convenience method to return the single negotiated value as a
191: * <code>short</code>.
192: *
193: * @throws ClassCastException if the value is of a different Class type.
194: */
195: public short getNegotiatedValueAsShort() {
196: if (elementClass != Short.class)
197: throw new ClassCastException(JaiI18N
198: .getString("NegotiableNumeric1"));
199: return number.shortValue();
200: }
201:
202: /**
203: * A convenience method to return the single negotiated value as a
204: * <code>int</code>.
205: *
206: * @throws ClassCastException if the value is of a different Class type.
207: */
208: public int getNegotiatedValueAsInt() {
209: if (elementClass != Integer.class)
210: throw new ClassCastException(JaiI18N
211: .getString("NegotiableNumeric1"));
212: return number.intValue();
213: }
214:
215: /**
216: * A convenience method to return the single negotiated value as a
217: * <code>long</code>.
218: *
219: * @throws ClassCastException if the value is of a different Class type.
220: */
221: public long getNegotiatedValueAsLong() {
222: if (elementClass != Long.class)
223: throw new ClassCastException(JaiI18N
224: .getString("NegotiableNumeric1"));
225: return number.longValue();
226: }
227:
228: /**
229: * A convenience method to return the single negotiated value as a
230: * <code>float</code>.
231: *
232: * @throws ClassCastException if the value is of a different Class type.
233: */
234: public float getNegotiatedValueAsFloat() {
235: if (elementClass != Float.class)
236: throw new ClassCastException(JaiI18N
237: .getString("NegotiableNumeric1"));
238: return number.floatValue();
239: }
240:
241: /**
242: * A convenience method to return the single negotiated value as a
243: * <code>double</code>.
244: *
245: * @throws ClassCastException if the value is of a different Class type.
246: */
247: public double getNegotiatedValueAsDouble() {
248: if (elementClass != Double.class)
249: throw new ClassCastException(JaiI18N
250: .getString("NegotiableNumeric1"));
251: return number.doubleValue();
252: }
253: }
|