1 /* 2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.imageio.plugins.tiff; 26 27 import java.util.Collections; 28 import java.util.Iterator; 29 import java.util.List; 30 import java.util.Set; 31 import java.util.SortedMap; 32 import java.util.SortedSet; 33 import java.util.TreeMap; 34 import java.util.TreeSet; 35 36 /** 37 * A class representing a set of TIFF tags. Each tag in the set must 38 * have a unique number (this is a limitation of the TIFF 39 * specification itself). 40 * 41 * <p> This class and its subclasses are responsible for mapping 42 * between raw tag numbers and <code>TIFFTag</code> objects, which 43 * contain additional information about each tag, such as the tag's 44 * name, legal data types, and mnemonic names for some or all of ts 45 * data values. 46 * 47 * @since 1.9 48 * @see TIFFTag 49 */ 50 public class TIFFTagSet { 51 52 private SortedMap<Integer,TIFFTag> allowedTagsByNumber = new TreeMap<Integer,TIFFTag>(); 53 54 private SortedMap<String,TIFFTag> allowedTagsByName = new TreeMap<String,TIFFTag>(); 55 56 /** 57 * Constructs a TIFFTagSet. 58 */ 59 private TIFFTagSet() {} 60 61 /** 62 * Constructs a <code>TIFFTagSet</code>, given a <code>List</code> 63 * of <code>TIFFTag</code> objects. 64 * 65 * @param tags a <code>List</code> object containing 66 * <code>TIFFTag</code> objects to be added to this tag set. 67 * 68 * @throws IllegalArgumentException if <code>tags</code> is 69 * <code>null</code>, or contains objects that are not instances 70 * of the <code>TIFFTag</code> class. 71 */ 72 public TIFFTagSet(List<TIFFTag> tags) { 73 if (tags == null) { 74 throw new IllegalArgumentException("tags == null!"); 75 } 76 Iterator<TIFFTag> iter = tags.iterator(); 77 while (iter.hasNext()) { 78 Object o = iter.next(); 79 if (!(o instanceof TIFFTag)) { 80 throw new IllegalArgumentException( 81 "tags contains a non-TIFFTag!"); 82 } 83 TIFFTag tag = (TIFFTag)o; 84 85 allowedTagsByNumber.put(Integer.valueOf(tag.getNumber()), tag); 86 allowedTagsByName.put(tag.getName(), tag); 87 } 88 } 89 90 /** 91 * Returns the <code>TIFFTag</code> from this set that is 92 * associated with the given tag number, or <code>null</code> if 93 * no tag exists for that number. 94 * 95 * @param tagNumber the number of the tag to be retrieved. 96 * 97 * @return the numbered <code>TIFFTag</code>, or <code>null</code>. 98 */ 99 public TIFFTag getTag(int tagNumber) { 100 return allowedTagsByNumber.get(Integer.valueOf(tagNumber)); 101 } 102 103 /** 104 * Returns the <code>TIFFTag</code> having the given tag name, or 105 * <code>null</code> if the named tag does not belong to this tag set. 106 * 107 * @param tagName the name of the tag to be retrieved, as a 108 * <code>String</code>. 109 * 110 * @return the named <code>TIFFTag</code>, or <code>null</code>. 111 * 112 * @throws IllegalArgumentException if <code>tagName</code> is 113 * <code>null</code>. 114 */ 115 public TIFFTag getTag(String tagName) { 116 if (tagName == null) { 117 throw new IllegalArgumentException("tagName == null!"); 118 } 119 return allowedTagsByName.get(tagName); 120 } 121 122 /** 123 * Retrieves an unmodifiable numerically increasing set of tag numbers. 124 * 125 * <p>The returned object is unmodifiable and contains the tag 126 * numbers of all <code>TIFFTag</code>s in this <code>TIFFTagSet</code> 127 * sorted into ascending order according to 128 * {@link Integer#compareTo(Object)}.</p> 129 * 130 * @return All tag numbers in this set. 131 */ 132 public SortedSet<Integer> getTagNumbers() { 133 Set<Integer> tagNumbers = allowedTagsByNumber.keySet(); 134 SortedSet<Integer> sortedTagNumbers; 135 if(tagNumbers instanceof SortedSet) { 136 sortedTagNumbers = (SortedSet<Integer>)tagNumbers; 137 } else { 138 sortedTagNumbers = new TreeSet<Integer>(tagNumbers); 139 } 140 141 return Collections.unmodifiableSortedSet(sortedTagNumbers); 142 } 143 144 /** 145 * Retrieves an unmodifiable lexicographically increasing set of tag names. 146 * 147 * <p>The returned object is unmodifiable and contains the tag 148 * names of all <code>TIFFTag</code>s in this <code>TIFFTagSet</code> 149 * sorted into ascending order according to 150 * {@link String#compareTo(Object)}.</p> 151 * 152 * @return All tag names in this set. 153 */ 154 public SortedSet<String> getTagNames() { 155 Set<String> tagNames = allowedTagsByName.keySet(); 156 SortedSet<String> sortedTagNames; 157 if(tagNames instanceof SortedSet) { 158 sortedTagNames = (SortedSet<String>)tagNames; 159 } else { 160 sortedTagNames = new TreeSet<String>(tagNames); 161 } 162 163 return Collections.unmodifiableSortedSet(sortedTagNames); 164 } 165 }