001: /*
002: * $RCSfile: NegotiableNumericRange.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: import java.math.BigInteger;
014: import java.math.BigDecimal;
015: import javax.media.jai.util.Range;
016:
017: /**
018: * A class that wraps a <code>Range</code> which contains numeric elements,
019: * to implement the <code>Negotiable</code> interface.
020: * <code>NegotiableNumericRange</code> is a convenience class to specify a
021: * <code>Negotiable</code> parameter whose valid numeric values are
022: * specified by a <code>Range</code>.
023: *
024: * @since JAI 1.1
025: */
026: public class NegotiableNumericRange implements Negotiable {
027:
028: private Range range;
029:
030: /**
031: * Creates a <code>NegotiableNumericRange</code> given an
032: * <code>Range</code> containing elements of a subclass of
033: * <code>Number</code>.
034: *
035: * @throws IllegalArgumentException if range is null.
036: * @throws IllegalArgumentException if the elements of the supplied range
037: * are not a <code>Number</code> subclass.
038: */
039: public NegotiableNumericRange(Range range) {
040:
041: if (range == null) {
042: throw new IllegalArgumentException(JaiI18N
043: .getString("NegotiableNumericRange0"));
044: }
045:
046: // If the elementClass of the supplied Range is not a subclass of
047: // Number, throw an IllegalArgumentException
048: if (!(Number.class.isAssignableFrom(range.getElementClass()))) {
049: throw new IllegalArgumentException(JaiI18N
050: .getString("NegotiableNumericRange1"));
051: }
052:
053: this .range = range;
054: }
055:
056: /**
057: * Returns the <code>Range</code> of values which are currently valid
058: * for this class, null if there are no valid values.
059: */
060: public Range getRange() {
061: if (range.isEmpty())
062: return null;
063: return range;
064: }
065:
066: /**
067: * Returns a <code>NegotiableNumericRange</code> that contains the range
068: * of values that are common to this <code>NegotiableNumericRange</code>
069: * and the one supplied. If the supplied <code>Negotiable</code> is not
070: * a <code>NegotiableNumericRange</code> with its elements being of the
071: * same <code>Class</code> as this class', or if there is no common
072: * range of values, the negotiation will fail and
073: * <code>null</code> will be returned.
074: *
075: * @param other The <code>Negotiable</code> to negotiate with.
076: */
077: public Negotiable negotiate(Negotiable other) {
078:
079: if (other == null)
080: return null;
081:
082: // if other is not an instance of NegotiableNumericRange,
083: // or the element class doesn't match, negotiation fails
084: if (!(other instanceof NegotiableNumericRange))
085: return null;
086:
087: NegotiableNumericRange otherNNRange = (NegotiableNumericRange) other;
088: Range otherRange = otherNNRange.getRange();
089:
090: // If the range is null, i.e there are no valid values, then
091: // negotiation fails.
092: if (otherRange == null)
093: return null;
094:
095: // If the elementClass' don't match, negotiation fails.
096: if (otherRange.getElementClass() != range.getElementClass())
097: return null;
098:
099: Range result = range.intersect(otherRange);
100:
101: // If there are no valid values, negotiation failed.
102: if (result.isEmpty())
103: return null;
104:
105: return new NegotiableNumericRange(result);
106: }
107:
108: /**
109: * Returns a single value that is valid for this
110: * <code>NegotiableNumericRange</code>. The returned value is the lowest
111: * value contained in this <code>NegotiableNumericRange</code> if the
112: * range is not unbounded on the minimum end, or the highest value
113: * in the range, if the range is unbounded on the minimum end. If both
114: * ends are unbounded, 0 will be returned wrapped in the appropriate
115: * <code>Number</code> wrapper. Returns <code>null</code> if there
116: * are no valid elements in this <code>NegotiableNumericRange</code>.
117: */
118: public Object getNegotiatedValue() {
119:
120: // If there are no valid values, negotiation fails.
121: if (range.isEmpty())
122: return null;
123:
124: Number minValue = (Number) range.getMinValue();
125: // Is minimum end unbounded
126: if (minValue == null) {
127: Number maxValue = (Number) range.getMaxValue();
128: // Is maximum unbounded
129: if (maxValue == null) {
130: // Both ends are unbounded
131: Class elementClass = range.getElementClass();
132: // Have elementClass specific case statements, and get the
133: // negotiated value on the datatype basis
134: if (elementClass == Byte.class) {
135: return new Byte((byte) 0);
136: } else if (elementClass == Short.class) {
137: return new Short((short) 0);
138: } else if (elementClass == Integer.class) {
139: return new Integer(0);
140: } else if (elementClass == Long.class) {
141: return new Long((long) 0);
142: } else if (elementClass == Float.class) {
143: return new Float(0);
144: } else if (elementClass == Double.class) {
145: return new Double(0);
146: } else if (elementClass == BigInteger.class) {
147: return BigInteger.ZERO;
148: } else if (elementClass == BigDecimal.class) {
149: return new BigDecimal(BigInteger.ZERO);
150: }
151: } else {
152: return maxValue;
153: }
154: }
155:
156: return minValue;
157: }
158:
159: /**
160: * Returns the <code>Class</code> of the Object returned as the result
161: * of the negotiation. This will be a subclass of <code>Number</code>.
162: */
163: public Class getNegotiatedValueClass() {
164: return range.getElementClass();
165: }
166: }
|