001: /*
002: * $RCSfile: AttributedImageCollection.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:04 $
010: * $State: Exp $
011: */
012: package javax.media.jai;
013:
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.Iterator;
017: import java.util.Set;
018: import java.util.HashSet;
019:
020: /**
021: * Class representing a CollectionImage wherein all image elements are
022: * AttributedImage instances. All Collection methods will be overridden
023: * such that contained images are forced to be AttributedImages.
024: *
025: * <p> Note that the methods <code>getAll(attribute)</code> and
026: * <code>removeAll(attribute)</code> use the <code>equals()</code> method
027: * of the attribute parameter rather that that of the
028: * <code>AttributedImage</code>s in the <code>Collection</code>. This
029: * permits "filtering" if the attribute of the <code>AttributedImage</code>s
030: * contains more than one type of value. For example, if the attribute
031: * contained both position and time, then the parameter <code>attribute</code>
032: * could be an instance of a class which compared only the position if it
033: * were desired to obtain or remove all images at a given position
034: * irrespective of the time stamp.
035: *
036: * @since JAI 1.1
037: */
038: public class AttributedImageCollection extends CollectionImage {
039:
040: protected AttributedImageCollection() {
041: }
042:
043: /**
044: * Constructs an <code>AttributedImageCollection</code> with contents
045: * set to the contents of the supplied <code>Collection</code>. Only
046: * elements in the <code>Collection</code> which are instances of
047: * <code>AttributedImage</code> will be added.
048: *
049: * @throws IllegalArgumentException if <code>images</code> is <code>null</code>
050: */
051: public AttributedImageCollection(Collection images) {
052: super ();
053:
054: if (images == null) {
055: throw new IllegalArgumentException(JaiI18N
056: .getString("AttributedImageCollection0"));
057: }
058:
059: try {
060: // Try to create a Collection of the same class.
061: imageCollection = (Collection) images.getClass()
062: .newInstance();
063: } catch (Exception e) {
064: // As a fallback create a List.
065: imageCollection = new ArrayList(images.size());
066: }
067:
068: // only add AttributedImages which have not yet been added
069: Iterator iter = images.iterator();
070: while (iter.hasNext()) {
071: Object o = iter.next();
072:
073: if (o instanceof AttributedImage
074: && !imageCollection.contains(o)) {
075: imageCollection.add(o);
076: }
077: }
078: }
079:
080: /**
081: * Returns a Set of all AttributedImages the attribute of which is
082: * equal to the parameter object according to the equals() method of
083: * the parameter object. If no match is found null will be returned.
084: * If the parameter is null a Set view of all AttributedImages in the
085: * Collection will be returned.
086: */
087: public Set getAll(Object attribute) {
088:
089: if (attribute == null) {
090: return (Set) imageCollection;
091: } else {
092: HashSet set = null;
093: Iterator iter = iterator();
094:
095: while (iter.hasNext()) {
096: AttributedImage ai = (AttributedImage) iter.next();
097:
098: if (attribute.equals(ai.getAttribute())) {
099: if (set == null) {
100: set = new HashSet();
101: }
102:
103: set.add(ai);
104: }
105: }
106:
107: return set;
108: }
109: }
110:
111: /**
112: * Returns a Set of all AttributedImages the image of which is equal
113: * to the parameter image. If no match is found null will be returned.
114: * If the parameter is null a Set view of all AttributedImages in the
115: * Collection will be returned.
116: */
117: public Set getAll(PlanarImage image) {
118:
119: if (image == null) {
120: return (Set) imageCollection;
121: } else {
122: HashSet set = null;
123: Iterator iter = iterator();
124:
125: while (iter.hasNext()) {
126: AttributedImage ai = (AttributedImage) iter.next();
127:
128: if (image.equals(ai.getImage())) {
129: if (set == null) {
130: set = new HashSet();
131: }
132:
133: set.add(ai);
134: }
135: }
136:
137: return set;
138: }
139: }
140:
141: /**
142: * Removes all AttributedImages the attribute of which is
143: * equal to the parameter object according to the equals() method of the
144: * parameter object. The returned value contains all AttributedImages
145: * which were removed from the underlying Collection or null if no
146: * match was found. If the parameter is null, null will be returned.
147: */
148: public Set removeAll(Object attribute) {
149:
150: if (attribute == null) {
151: return null;
152: } else {
153: Iterator iter = iterator();
154: Set removed = null;
155:
156: while (iter.hasNext()) {
157: AttributedImage ai = (AttributedImage) iter.next();
158:
159: if (attribute.equals(ai.getAttribute())) {
160: iter.remove();
161: if (removed == null) {
162: removed = new HashSet();
163: }
164: removed.add(ai);
165: }
166: }
167:
168: return (Set) removed;
169: }
170: }
171:
172: /**
173: * Removes all AttributedImages the image of which is equal to the
174: * parameter image. The returned value contains all AttributedImages
175: * which were removed from the underlying Collection or null if no
176: * match was found. If the parameter is null, null will be returned.
177: */
178: public Set removeAll(PlanarImage image) {
179:
180: if (image == null) {
181: return null;
182: } else {
183: Iterator iter = iterator();
184: Set removed = null;
185:
186: while (iter.hasNext()) {
187: AttributedImage ai = (AttributedImage) iter.next();
188:
189: if (image.equals(ai.getImage())) {
190: iter.remove();
191: if (removed == null) {
192: removed = new HashSet();
193: }
194: removed.add(ai);
195: }
196: }
197:
198: return (Set) removed;
199: }
200: }
201:
202: /* -- CollectionImage methods: ensure elements are AttributedImages. -- */
203:
204: /**
205: * Adds the specified object to this <code>Collection</code>. This
206: * method overrides the superclass method in order to perform a
207: * type check on the object being added.
208: *
209: * @throws IllegalArgumentException if <code>o</code> is <code>null</code>
210: * or is not an <code>AttributedImage</code>.
211: *
212: * @return <code>true</code> if and only if the parameter is added to the
213: * <code>Collection</code>.
214: */
215: public boolean add(Object o) {
216:
217: if (o == null || !(o instanceof AttributedImage)) {
218: throw new IllegalArgumentException(JaiI18N
219: .getString("AttributedImageCollection1"));
220: }
221:
222: // don't add an object that's there already
223: if (imageCollection.contains(o)) {
224: return false;
225: }
226:
227: return imageCollection.add(o);
228: }
229:
230: /**
231: * Adds to this <code>Collection</code> all elements in the specified
232: * <code>Collection</code> which are <code>AttributedImage</code>s.
233: *
234: * @return <code>true</code> if this <code>Collection</code> changed
235: * as a result of the call.
236: */
237: public boolean addAll(Collection c) {
238: if (c == null)
239: return false;
240:
241: // iterate over collection
242: Iterator iter = c.iterator();
243: boolean flag = false;
244:
245: while (iter.hasNext()) {
246: Object o = iter.next();
247:
248: // only add AttributedImages which have not yet been added
249: if (o instanceof AttributedImage) {
250: if (!imageCollection.contains(o)
251: && imageCollection.add(o)) {
252: flag = true; // one shot switch
253: }
254: }
255: }
256:
257: return flag;
258: }
259:
260: /**
261: * Returns the first attributed image found in the collection
262: * that contains the planar image argument. If the parameter is
263: * null, null will be returned.
264: */
265: public AttributedImage getAttributedImage(PlanarImage image) {
266:
267: if (image == null) {
268: return null;
269: } else {
270: Iterator iter = iterator();
271:
272: while (iter.hasNext()) {
273: AttributedImage ai = (AttributedImage) iter.next();
274:
275: if (image.equals(ai.getImage())) {
276: return ai;
277: }
278: }
279: }
280:
281: return null;
282: }
283:
284: /**
285: * Returns the first attributed image found in the collection
286: * that contains the attribute. If the parameter is
287: * null, null will be returned.
288: */
289: public AttributedImage getAttributedImage(Object attribute) {
290:
291: if (attribute == null) {
292: return null;
293: } else {
294: Iterator iter = iterator();
295:
296: while (iter.hasNext()) {
297: AttributedImage ai = (AttributedImage) iter.next();
298:
299: if (attribute.equals(ai.getAttribute())) {
300: return ai;
301: }
302: }
303: }
304:
305: return null;
306: }
307: }
|