/** * Creates a new image set which will contain the same keys, images and * outside values as the input set. If the input set is {@code null} or * empty, an empty image set is created. * * @param <K> the key type * @param set the image set to copy * * @return the new copy */ public static <K> ImageSet<K> copy(ImageSet<K> set) { ImageSet<K> newSet = new ImageSet<K>(); if (!(set == null || set.isEmpty())) { for (K key : set.keySet()) { RenderedImage image = set.get(key); Number outsideValue = set.getOutsideValue(key); newSet.add(key, image, outsideValue); } } return newSet; }
/** * Gets the value that an image set iterator will return for the * image associated with the given key when it is positioned * outside the bounds of that image. * * @param key the key * @return the value returned for out-of-bounds value requests * (may be {@code null}) */ public Number getOutsideValue(K key) { assertKey(key); return elements.get(key).getOutsideValue(); }
/** * Gets the bounds of the image associated with the given key. * * @param key the image key * @return image bounds as a new Rectangle or {@code null} if the key * could not be found */ public Rectangle getBounds(K key) { Element e = elements.get(key); return e == null ? null : e.getBounds(); }
/** * Private constructor. * * @param set the target image set * @param bounds the bounds for this iterator */ private Iterator(ImageSet set, Rectangle bounds) { if (set == null || set.isEmpty()) { throw new IllegalArgumentException("The ImageSet must not be null or empty"); } this.keys = CollectionFactory.list(); this.delegates = CollectionFactory.list(); Set<K> keySet = set.keySet(); for (K key : keySet) { keys.add(key); delegates.add(new SimpleIterator(set.get(key), bounds, set.getOutsideValue(key))); } }
/** * Gets a new iterator based on the bounds of the image with the * specified key value. * * @param referenceImageKey the key of the image to use as the reference * for the iterator * * @return the new iterator * * @throws IllegalArgumentException if this image set is empty or if no * image corresponds to the key value */ public Iterator<K> getIterator(K referenceImageKey) { if (elements.isEmpty()) { throw new IllegalArgumentException("This image set is empty"); } Rectangle bounds = getBounds(referenceImageKey); return getIterator(bounds); }
/** * Gets a new iterator based on the bounds of the first image added * to this set or, if that has been removed, the image that has been * in this set for the longest period. * * @return the new iterator * * @throws IllegalArgumentException if this image set is empty */ public Iterator<K> getIterator() { return getIterator(elements.keySet().iterator().next()); }
/** * Gets the enclosing bounds of all images in this set. This is * the union of the individual image bounds. If the set is empty * an empty {@code Rectangle} will be returned. * * @return enclosing bounds for this image set */ public Rectangle getUnionBounds() { Rectangle r = new Rectangle(); for (Element e : elements.values()) { r = r.union(e.getBounds()); } return r; }
/** * Gets the common bounds of images in this set This is the intersection * of the individual iamge bounds. An empty {@code Rectangle} will be * returned if ths set is empty, or if there is no area over which all * images overlap. * * @return common bounds for this image set */ public Rectangle getIntersectionBounds() { Rectangle r = null; for (Element e : elements.values()) { r = (r == null ? e.getBounds() : r.intersection(e.getBounds())); if (r.isEmpty()) { break; } } return r; }