001: /*
002: * $RCSfile: NegotiableCollection.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.util.Collection;
014: import java.util.Iterator;
015: import java.util.Vector;
016:
017: /**
018: * A class that wraps an <code>Collection</code> to implement the
019: * <code>Negotiable</code> interface. <code>NegotiableCollection</code>
020: * is a convenience class to specify a <code>Negotiable</code> value for
021: * a parameter whose valid values are contained in an <code>Collection</code>.
022: *
023: * @since JAI 1.1
024: */
025: public class NegotiableCollection implements Negotiable {
026:
027: private Vector elements;
028: private Class elementClass;
029:
030: /**
031: * Creates a <code>NegotiableCollection</code> given an
032: * <code>Collection</code>.
033: *
034: * @throws IllegalArgumentException if collection is null.
035: * @throws IllegalArgumentException if all the elements of collection
036: * are not of the same <code>Class</code> type.
037: */
038: public NegotiableCollection(Collection collection) {
039:
040: if (collection == null) {
041: throw new IllegalArgumentException(JaiI18N
042: .getString("NegotiableCollection0"));
043: }
044:
045: elements = new Vector();
046: Object obj;
047:
048: Iterator i = collection.iterator();
049: if (i.hasNext()) {
050: obj = i.next();
051: elements.add(obj);
052: elementClass = obj.getClass();
053: } else {
054: // no elements, so elementClass will be initialized to null,
055: // which is correct. elements will also be null, which is
056: // also correct.
057: }
058:
059: for (; i.hasNext();) {
060: obj = i.next();
061: if (obj.getClass() != elementClass) {
062: throw new IllegalArgumentException(JaiI18N
063: .getString("NegotiableCollection1"));
064: }
065: elements.add(obj);
066: }
067: }
068:
069: /**
070: * Creates a <code>NegotiableCollection</code> given an array of
071: * <code>Object</code>s. The elements of the <code>Object</code>
072: * array are treated as being the elements of an <code>Collection</code>.
073: *
074: * @throws IllegalArgumentException if objects is null.
075: * @throws IllegalArgumentException if all the elements of objects are not
076: * of the same <code>Class</code> type.
077: */
078: public NegotiableCollection(Object objects[]) {
079:
080: if (objects == null) {
081: throw new IllegalArgumentException(JaiI18N
082: .getString("NegotiableCollection0"));
083: }
084:
085: int length = objects.length;
086: if (length != 0) {
087: elementClass = objects[0].getClass();
088: } else {
089: // no elements, so elementClass will be initialized to null,
090: // which is correct. elements will also be null, which is
091: // also correct.
092: }
093:
094: elements = new Vector(length);
095: for (int i = 0; i < length; i++) {
096: if (objects[i].getClass() != elementClass) {
097: throw new IllegalArgumentException(JaiI18N
098: .getString("NegotiableCollection1"));
099: }
100: elements.add(objects[i]);
101: }
102: }
103:
104: /**
105: * Returns the <code>Collection</code> of values which are currently
106: * valid for this class, null if there are no valid values.
107: */
108: public Collection getCollection() {
109: if (elements.isEmpty())
110: return null;
111: return elements;
112: }
113:
114: /**
115: * Returns a <code>NegotiableCollection</code> that contains those
116: * elements that are common to this <code>NegotiableCollection</code>
117: * and the one supplied. If the supplied <code>Negotiable</code> is not
118: * a <code>NegotiableCollection</code> with its elements being of the
119: * same <code>Class</code> as this class', or if there are no common
120: * elements, the negotiation will fail and <code>null</code> (signifying
121: * the failure of the negotiation) will be returned.
122: *
123: * @param other The <code>Negotiable</code> to negotiate with.
124: */
125: public Negotiable negotiate(Negotiable other) {
126:
127: if (other == null) {
128: return null;
129: }
130:
131: // if other is not an instance of NegotiableCollection
132: if (!(other instanceof NegotiableCollection)
133: || other.getNegotiatedValueClass() != elementClass) {
134: return null;
135: }
136:
137: Object obj;
138: Vector result = new Vector();
139:
140: Collection otherCollection = ((NegotiableCollection) other)
141: .getCollection();
142:
143: // If the collection is null, i.e there are no valid values, then
144: // negotiation fails.
145: if (otherCollection == null)
146: return null;
147:
148: // Return a NegotiableCollection whose elements are those that
149: // were common to both the collections.
150: for (Iterator i = elements.iterator(); i.hasNext();) {
151: obj = i.next();
152: // If element is present in both the collections
153: if (otherCollection.contains(obj)) {
154: // Do not insert duplicates
155: if (!result.contains(obj)) {
156: result.add(obj);
157: }
158: }
159: }
160:
161: // If there are no common elements, negotiation failed.
162: if (result.isEmpty()) {
163: return null;
164: }
165:
166: return new NegotiableCollection(result);
167: }
168:
169: /**
170: * Returns a single value that is valid for this
171: * <code>NegotiableCollection</code>. The returned value is the first
172: * element contained in this <code>NegotiableCollection</code>. Returns
173: * <code>null</code> if there are no valid elements in this
174: * <code>NegotiableCollection</code>.
175: */
176: public Object getNegotiatedValue() {
177:
178: // Return the first element in this NegotiableCollection
179: // else return
180: // <code>null</code>
181: if (elements != null && elements.size() > 0) {
182: return elements.elementAt(0);
183: } else {
184: return null;
185: }
186: }
187:
188: /**
189: * Returns the <code>Class</code> of the Object returned as the result
190: * of the negotiation. If the <code>Collection</code> used to construct
191: * this <code>NegotiableCollection</code> was empty, i.e. had no
192: * elements, the <code>Class</code> of the elements is indeterminate,
193: * therefore null will be returned from this method in such a case.
194: */
195: public Class getNegotiatedValueClass() {
196: return elementClass;
197: }
198: }
|