001: /*
002: * $RCSfile: TIFFTagSet.java,v $
003: *
004: *
005: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * - Redistribution of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * - Redistribution in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * Neither the name of Sun Microsystems, Inc. or the names of
020: * contributors may be used to endorse or promote products derived
021: * from this software without specific prior written permission.
022: *
023: * This software is provided "AS IS," without a warranty of any
024: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
025: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
026: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027: * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
028: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
029: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
031: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035: * POSSIBILITY OF SUCH DAMAGES.
036: *
037: * You acknowledge that this software is not designed or intended for
038: * use in the design, construction, operation or maintenance of any
039: * nuclear facility.
040: *
041: * $Revision: 1.1 $
042: * $Date: 2005/02/11 05:01:19 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageio.plugins.tiff;
046:
047: // Should implement Set?
048:
049: import java.util.Collections;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Set;
053: import java.util.SortedMap;
054: import java.util.SortedSet;
055: import java.util.TreeMap;
056: import java.util.TreeSet;
057:
058: /**
059: * A class representing a set of TIFF tags. Each tag in the set must
060: * have a unique number (this is a limitation of the TIFF
061: * specification itself).
062: *
063: * <p> This class and its subclasses are responsible for mapping
064: * between raw tag numbers and <code>TIFFTag</code> objects, which
065: * contain additional information about each tag, such as the tag's
066: * name, legal data types, and mnemonic names for some or all of ts
067: * data values.
068: *
069: * @see TIFFTag
070: */
071: public class TIFFTagSet {
072:
073: private SortedMap allowedTagsByNumber = new TreeMap();
074:
075: private SortedMap allowedTagsByName = new TreeMap();
076:
077: /**
078: * Constructs a TIFFTagSet.
079: */
080: private TIFFTagSet() {
081: }
082:
083: /**
084: * Constructs a <code>TIFFTagSet</code>, given a <code>List</code>
085: * of <code>TIFFTag</code> objects.
086: *
087: * @param tags a <code>List</code> object containing
088: * <code>TIFFTag</code> objects to be added to this tag set.
089: *
090: * @throws IllegalArgumentException if <code>tags</code> is
091: * <code>null</code>, or contains objects that are not instances
092: * of the <code>TIFFTag</code> class.
093: */
094: public TIFFTagSet(List tags) {
095: if (tags == null) {
096: throw new IllegalArgumentException("tags == null!");
097: }
098: Iterator iter = tags.iterator();
099: while (iter.hasNext()) {
100: Object o = iter.next();
101: if (!(o instanceof TIFFTag)) {
102: throw new IllegalArgumentException(
103: "tags contains a non-TIFFTag!");
104: }
105: TIFFTag tag = (TIFFTag) o;
106:
107: allowedTagsByNumber.put(new Integer(tag.getNumber()), tag);
108: allowedTagsByName.put(tag.getName(), tag);
109: }
110: }
111:
112: /**
113: * Returns the <code>TIFFTag</code> from this set that is
114: * associated with the given tag number, or <code>null</code> if
115: * no tag exists for that number.
116: *
117: * @param tagNumber the number of the tag to be retrieved.
118: *
119: * @return the numbered <code>TIFFTag</code>, or <code>null</code>.
120: */
121: public TIFFTag getTag(int tagNumber) {
122: return (TIFFTag) allowedTagsByNumber
123: .get(new Integer(tagNumber));
124: }
125:
126: /**
127: * Returns the <code>TIFFTag</code> having the given tag name, or
128: * <code>null</code> if the named tag does not belong to this tag set.
129: *
130: * @param tagName the name of the tag to be retrieved, as a
131: * <code>String</code>.
132: *
133: * @return the named <code>TIFFTag</code>, or <code>null</code>.
134: *
135: * @throws IllegalArgumentException if <code>tagName</code> is
136: * <code>null</code>.
137: */
138: public TIFFTag getTag(String tagName) {
139: if (tagName == null) {
140: throw new IllegalArgumentException("tagName == null!");
141: }
142: return (TIFFTag) allowedTagsByName.get(tagName);
143: }
144:
145: /**
146: * Retrieves an unmodifiable numerically increasing set of tag numbers.
147: *
148: * <p>The returned object is unmodifiable and contains the tag
149: * numbers of all <code>TIFFTag</code>s in this <code>TIFFTagSet</code>
150: * sorted into ascending order according to
151: * {@link <code>Integer#compareTo(Object)</code>}.</p>
152: *
153: * @return All tag numbers in this set.
154: */
155: public SortedSet getTagNumbers() {
156: Set tagNumbers = allowedTagsByNumber.keySet();
157: SortedSet sortedTagNumbers;
158: if (tagNumbers instanceof SortedSet) {
159: sortedTagNumbers = (SortedSet) tagNumbers;
160: } else {
161: sortedTagNumbers = new TreeSet(tagNumbers);
162: }
163:
164: return Collections.unmodifiableSortedSet(sortedTagNumbers);
165: }
166:
167: /**
168: * Retrieves an unmodifiable lexicographically increasing set of tag names.
169: *
170: * <p>The returned object is unmodifiable and contains the tag
171: * names of all <code>TIFFTag</code>s in this <code>TIFFTagSet</code>
172: * sorted into ascending order according to
173: * {@link <code>String#compareTo(Object)</code>}.</p>
174: *
175: * @return All tag names in this set.
176: */
177: public SortedSet getTagNames() {
178: Set tagNames = allowedTagsByName.keySet();
179: SortedSet sortedTagNames;
180: if (tagNames instanceof SortedSet) {
181: sortedTagNames = (SortedSet) tagNames;
182: } else {
183: sortedTagNames = new TreeSet(tagNames);
184: }
185:
186: return Collections.unmodifiableSortedSet(sortedTagNames);
187: }
188: }
|