Changeset 6127 in josm for trunk/src/com/drew/metadata/Metadata.java
- Timestamp:
- 2013-08-09T18:05:11+02:00 (12 years ago)
- File:
-
- 1 edited
-
trunk/src/com/drew/metadata/Metadata.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/metadata/Metadata.java
r4231 r6127 1 1 /* 2 * Metadata.java2 * Copyright 2002-2012 Drew Noakes 3 3 * 4 * This class is public domain software - that is, you can do whatever you want 5 * with it, and include it software that is licensed under the GNU or the 6 * BSD license, or whatever other licence you choose, including proprietary 7 * closed source licenses. Similarly, I release this Java version under the 8 * same license, though I do ask that you leave this header in tact. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 9 7 * 10 * If you make modifications to this code that you think would benefit the 11 * wider community, please send me a copy and I'll post it on my site. 8 * http://www.apache.org/licenses/LICENSE-2.0 12 9 * 13 * If you make use of this code, I'd appreciate hearing about it. 14 * drew.noakes@drewnoakes.com 15 * Latest version of this software kept at 16 * http://drewnoakes.com/ 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 17 15 * 18 * Created on 28 April 2002, 17:40 19 * Modified 04 Aug 2002 20 * - Adjusted javadoc 21 * - Added 22 * Modified 29 Oct 2002 (v1.2) 23 * - Stored IFD directories in separate tag-spaces 24 * - iterator() now returns an Iterator over a list of TagValue objects 25 * - More get*Description() methods to detail GPS tags, among others 26 * - Put spaces between words of tag name for presentation reasons (they had no 27 * significance in compound form) 16 * More information about this project is available at: 17 * 18 * http://drewnoakes.com/code/exif/ 19 * http://code.google.com/p/metadata-extractor/ 28 20 */ 29 21 package com.drew.metadata; 30 22 31 import java.io.Serializable; 23 import com.drew.lang.annotations.NotNull; 24 import com.drew.lang.annotations.Nullable; 25 32 26 import java.util.ArrayList; 27 import java.util.Collection; 33 28 import java.util.HashMap; 34 import java.util. Iterator;29 import java.util.Map; 35 30 36 31 /** 37 * Result from an exif extraction operation, containing all tags, their 38 * values and support for retrieving them. 39 * @author Drew Noakes http://drewnoakes.com 32 * A top-level object to hold the various types of metadata (Exif/IPTC/etc) related to one entity (such as a file 33 * or stream). 34 * <p/> 35 * Metadata objects may contain zero or more directories. Each directory may contain zero or more tags with 36 * corresponding values. 37 * 38 * @author Drew Noakes http://drewnoakes.com 40 39 */ 41 public final class Metadata implements Serializable40 public final class Metadata 42 41 { 43 /** 44 * 45 */ 46 private final HashMap directoryMap; 47 42 @NotNull 43 private final Map<Class<? extends Directory>,Directory> _directoryByClass = new HashMap<Class<? extends Directory>, Directory>(); 44 48 45 /** 49 46 * List of Directory objects set against this object. Keeping a list handy makes 50 47 * creation of an Iterator and counting tags simple. 51 48 */ 52 private final ArrayList directoryList; 49 @NotNull 50 private final Collection<Directory> _directoryList = new ArrayList<Directory>(); 53 51 54 52 /** 55 * Creates a new instance of Metadata. Package private. 53 * Returns an objects for iterating over Directory objects in the order in which they were added. 54 * 55 * @return an iterable collection of directories 56 56 */ 57 public Metadata() 57 @NotNull 58 public Iterable<Directory> getDirectories() 58 59 { 59 directoryMap = new HashMap(); 60 directoryList = new ArrayList(); 61 } 62 63 64 // OTHER METHODS 65 66 /** 67 * Creates an Iterator over the tag types set against this image, preserving the order 68 * in which they were set. Should the same tag have been set more than once, it's first 69 * position is maintained, even though the final value is used. 70 * @return an Iterator of tag types set for this image 71 */ 72 public Iterator getDirectoryIterator() 73 { 74 return directoryList.iterator(); 60 return _directoryList; 75 61 } 76 62 77 63 /** 78 64 * Returns a count of unique directories in this metadata collection. 65 * 79 66 * @return the number of unique directory types set for this metadata collection 80 67 */ 81 68 public int getDirectoryCount() 82 69 { 83 return directoryList.size(); 70 return _directoryList.size(); 84 71 } 85 72 … … 88 75 * such a directory, it is returned. Otherwise a new instance of this directory will be created and stored within 89 76 * this Metadata object. 77 * 90 78 * @param type the type of the Directory implementation required. 91 79 * @return a directory of the specified type. 92 80 */ 93 public Directory getDirectory(Class type) 81 @NotNull 82 @SuppressWarnings("unchecked") 83 public <T extends Directory> T getOrCreateDirectory(@NotNull Class<T> type) 94 84 { 95 if (!Directory.class.isAssignableFrom(type)) {96 throw new RuntimeException("Class type passed to getDirectory must be an implementation of com.drew.metadata.Directory");97 } 85 // We suppress the warning here as the code asserts a map signature of Class<T>,T. 86 // So after get(Class<T>) it is for sure the result is from type T. 87 98 88 // check if we've already issued this type of directory 99 if (directory Map.containsKey(type)){100 return ( Directory)directoryMap.get(type);101 } 102 Objectdirectory;89 if (_directoryByClass.containsKey(type)) 90 return (T)_directoryByClass.get(type); 91 92 T directory; 103 93 try { 104 94 directory = type.newInstance(); … … 106 96 throw new RuntimeException("Cannot instantiate provided Directory type: " + type.toString()); 107 97 } 108 // store the directory in case it's requested later 109 directoryMap.put(type, directory); 110 directoryList.add(directory); 111 return (Directory)directory; 98 // store the directory 99 _directoryByClass.put(type, directory); 100 _directoryList.add(directory); 101 102 return directory; 103 } 104 105 /** 106 * If this <code>Metadata</code> object contains a <code>Directory</code> of the specified type, it is returned. 107 * Otherwise <code>null</code> is returned. 108 * 109 * @param type the Directory type 110 * @param <T> the Directory type 111 * @return a Directory of type T if it exists in this Metadata object, otherwise <code>null</code>. 112 */ 113 @Nullable 114 @SuppressWarnings("unchecked") 115 public <T extends Directory> T getDirectory(@NotNull Class<T> type) 116 { 117 // We suppress the warning here as the code asserts a map signature of Class<T>,T. 118 // So after get(Class<T>) it is for sure the result is from type T. 119 120 return (T)_directoryByClass.get(type); 112 121 } 113 122 114 123 /** 115 124 * Indicates whether a given directory type has been created in this metadata 116 * repository. Directories are created by calling getDirectory(Class). 125 * repository. Directories are created by calling <code>getOrCreateDirectory(Class)</code>. 126 * 117 127 * @param type the Directory type 118 128 * @return true if the metadata directory has been created 119 129 */ 120 public boolean containsDirectory(Class type) 130 public boolean containsDirectory(Class<? extends Directory> type) 121 131 { 122 return directoryMap.containsKey(type); 132 return _directoryByClass.containsKey(type); 133 } 134 135 /** 136 * Indicates whether any errors were reported during the reading of metadata values. 137 * This value will be true if Directory.hasErrors() is true for one of the contained Directory objects. 138 * 139 * @return whether one of the contained directories has an error 140 */ 141 public boolean hasErrors() 142 { 143 for (Directory directory : _directoryList) { 144 if (directory.hasErrors()) 145 return true; 146 } 147 return false; 123 148 } 124 149 }
Note:
See TracChangeset
for help on using the changeset viewer.
