source: josm/trunk/src/com/drew/metadata/Metadata.java@ 7299

Last change on this file since 7299 was 6127, checked in by bastiK, 11 years ago

applied #8895 - Upgrade metadata-extractor to v. 2.6.4 (patch by ebourg)

File size: 5.2 KB
Line 
1/*
2 * Copyright 2002-2012 Drew Noakes
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 *
16 * More information about this project is available at:
17 *
18 * http://drewnoakes.com/code/exif/
19 * http://code.google.com/p/metadata-extractor/
20 */
21package com.drew.metadata;
22
23import com.drew.lang.annotations.NotNull;
24import com.drew.lang.annotations.Nullable;
25
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.HashMap;
29import java.util.Map;
30
31/**
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
39 */
40public final class Metadata
41{
42 @NotNull
43 private final Map<Class<? extends Directory>,Directory> _directoryByClass = new HashMap<Class<? extends Directory>, Directory>();
44
45 /**
46 * List of Directory objects set against this object. Keeping a list handy makes
47 * creation of an Iterator and counting tags simple.
48 */
49 @NotNull
50 private final Collection<Directory> _directoryList = new ArrayList<Directory>();
51
52 /**
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 */
57 @NotNull
58 public Iterable<Directory> getDirectories()
59 {
60 return _directoryList;
61 }
62
63 /**
64 * Returns a count of unique directories in this metadata collection.
65 *
66 * @return the number of unique directory types set for this metadata collection
67 */
68 public int getDirectoryCount()
69 {
70 return _directoryList.size();
71 }
72
73 /**
74 * Returns a <code>Directory</code> of specified type. If this <code>Metadata</code> object already contains
75 * such a directory, it is returned. Otherwise a new instance of this directory will be created and stored within
76 * this Metadata object.
77 *
78 * @param type the type of the Directory implementation required.
79 * @return a directory of the specified type.
80 */
81 @NotNull
82 @SuppressWarnings("unchecked")
83 public <T extends Directory> T getOrCreateDirectory(@NotNull Class<T> type)
84 {
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
88 // check if we've already issued this type of directory
89 if (_directoryByClass.containsKey(type))
90 return (T)_directoryByClass.get(type);
91
92 T directory;
93 try {
94 directory = type.newInstance();
95 } catch (Exception e) {
96 throw new RuntimeException("Cannot instantiate provided Directory type: " + type.toString());
97 }
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);
121 }
122
123 /**
124 * Indicates whether a given directory type has been created in this metadata
125 * repository. Directories are created by calling <code>getOrCreateDirectory(Class)</code>.
126 *
127 * @param type the Directory type
128 * @return true if the metadata directory has been created
129 */
130 public boolean containsDirectory(Class<? extends Directory> type)
131 {
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;
148 }
149}
Note: See TracBrowser for help on using the repository browser.