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 */
045package com.github.jaiimageio.plugins.tiff;
046
047// Should implement Set?
048
049import java.util.Collections;
050import java.util.Iterator;
051import java.util.List;
052import java.util.Set;
053import java.util.SortedMap;
054import java.util.SortedSet;
055import java.util.TreeMap;
056import 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 */
071public 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     * Constructs a <code>TIFFTagSet</code>, given a <code>List</code>
084     * of <code>TIFFTag</code> objects.
085     *
086     * @param tags a <code>List</code> object containing
087     * <code>TIFFTag</code> objects to be added to this tag set.
088     *
089     * @throws IllegalArgumentException if <code>tags</code> is
090     * <code>null</code>, or contains objects that are not instances
091     * of the <code>TIFFTag</code> class.
092     */
093    public TIFFTagSet(List tags) {
094        if (tags == null) {
095            throw new IllegalArgumentException("tags == null!");
096        }
097        Iterator iter = tags.iterator();
098        while (iter.hasNext()) {
099            Object o = iter.next();
100            if (!(o instanceof TIFFTag)) {
101                throw new IllegalArgumentException(
102                                               "tags contains a non-TIFFTag!");
103            }
104            TIFFTag tag = (TIFFTag)o;
105
106            allowedTagsByNumber.put(new Integer(tag.getNumber()), tag);
107            allowedTagsByName.put(tag.getName(), tag);
108        }
109    }
110
111    /**
112     * Returns the <code>TIFFTag</code> from this set that is
113     * associated with the given tag number, or <code>null</code> if
114     * no tag exists for that number.
115     *
116     * @param tagNumber the number of the tag to be retrieved.
117     *
118     * @return the numbered <code>TIFFTag</code>, or <code>null</code>.
119     */
120    public TIFFTag getTag(int tagNumber) {
121        return (TIFFTag)allowedTagsByNumber.get(new Integer(tagNumber));
122    }
123
124    /**
125     * Returns the <code>TIFFTag</code> having the given tag name, or
126     * <code>null</code> if the named tag does not belong to this tag set.
127     *
128     * @param tagName the name of the tag to be retrieved, as a
129     * <code>String</code>.
130     *
131     * @return the named <code>TIFFTag</code>, or <code>null</code>.
132     *
133     * @throws IllegalArgumentException if <code>tagName</code> is
134     * <code>null</code>.
135     */
136    public TIFFTag getTag(String tagName) {
137        if (tagName == null) {
138            throw new IllegalArgumentException("tagName == null!");
139        }
140        return (TIFFTag)allowedTagsByName.get(tagName);
141    }
142
143    /**
144     * Retrieves an unmodifiable numerically increasing set of tag numbers.
145     *
146     * <p>The returned object is unmodifiable and contains the tag
147     * numbers of all <code>TIFFTag</code>s in this <code>TIFFTagSet</code>
148     * sorted into ascending order according to
149     * {@link <code>Integer#compareTo(Object)</code>}.</p>
150     *
151     * @return All tag numbers in this set.
152     */
153    public SortedSet getTagNumbers() {
154        Set tagNumbers = allowedTagsByNumber.keySet();
155        SortedSet sortedTagNumbers;
156        if(tagNumbers instanceof SortedSet) {
157            sortedTagNumbers = (SortedSet)tagNumbers;
158        } else {
159            sortedTagNumbers = new TreeSet(tagNumbers);
160        }
161
162        return Collections.unmodifiableSortedSet(sortedTagNumbers);
163    }
164
165    /**
166     * Retrieves an unmodifiable lexicographically increasing set of tag names.
167     *
168     * <p>The returned object is unmodifiable and contains the tag
169     * names of all <code>TIFFTag</code>s in this <code>TIFFTagSet</code>
170     * sorted into ascending order according to
171     * {@link <code>String#compareTo(Object)</code>}.</p>
172     *
173     * @return All tag names in this set.
174     */
175    public SortedSet getTagNames() {
176        Set tagNames = allowedTagsByName.keySet();
177        SortedSet sortedTagNames;
178        if(tagNames instanceof SortedSet) {
179            sortedTagNames = (SortedSet)tagNames;
180        } else {
181            sortedTagNames = new TreeSet(tagNames);
182        }
183
184        return Collections.unmodifiableSortedSet(sortedTagNames);
185    }
186}