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

Last change on this file since 6209 was 6127, checked in by bastiK, 12 years ago

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

File size: 5.2 KB
RevLine 
[4231]1/*
[6127]2 * Copyright 2002-2012 Drew Noakes
[4231]3 *
[6127]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
[4231]7 *
[6127]8 * http://www.apache.org/licenses/LICENSE-2.0
[4231]9 *
[6127]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.
[4231]15 *
[6127]16 * More information about this project is available at:
17 *
18 * http://drewnoakes.com/code/exif/
19 * http://code.google.com/p/metadata-extractor/
[4231]20 */
21package com.drew.metadata;
22
[6127]23import com.drew.lang.annotations.NotNull;
24import com.drew.lang.annotations.Nullable;
25
[4231]26import java.util.ArrayList;
[6127]27import java.util.Collection;
[4231]28import java.util.HashMap;
[6127]29import java.util.Map;
[4231]30
31/**
[6127]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
[4231]39 */
[6127]40public final class Metadata
[4231]41{
[6127]42 @NotNull
43 private final Map<Class<? extends Directory>,Directory> _directoryByClass = new HashMap<Class<? extends Directory>, Directory>();
44
[4231]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 */
[6127]49 @NotNull
50 private final Collection<Directory> _directoryList = new ArrayList<Directory>();
[4231]51
52 /**
[6127]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
[4231]56 */
[6127]57 @NotNull
58 public Iterable<Directory> getDirectories()
[4231]59 {
[6127]60 return _directoryList;
[4231]61 }
62
63 /**
64 * Returns a count of unique directories in this metadata collection.
[6127]65 *
[4231]66 * @return the number of unique directory types set for this metadata collection
67 */
68 public int getDirectoryCount()
69 {
[6127]70 return _directoryList.size();
[4231]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.
[6127]77 *
[4231]78 * @param type the type of the Directory implementation required.
79 * @return a directory of the specified type.
80 */
[6127]81 @NotNull
82 @SuppressWarnings("unchecked")
83 public <T extends Directory> T getOrCreateDirectory(@NotNull Class<T> type)
[4231]84 {
[6127]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
[4231]88 // check if we've already issued this type of directory
[6127]89 if (_directoryByClass.containsKey(type))
90 return (T)_directoryByClass.get(type);
91
92 T directory;
[4231]93 try {
94 directory = type.newInstance();
95 } catch (Exception e) {
96 throw new RuntimeException("Cannot instantiate provided Directory type: " + type.toString());
97 }
[6127]98 // store the directory
99 _directoryByClass.put(type, directory);
100 _directoryList.add(directory);
101
102 return directory;
[4231]103 }
104
105 /**
[6127]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 /**
[4231]124 * Indicates whether a given directory type has been created in this metadata
[6127]125 * repository. Directories are created by calling <code>getOrCreateDirectory(Class)</code>.
126 *
[4231]127 * @param type the Directory type
128 * @return true if the metadata directory has been created
129 */
[6127]130 public boolean containsDirectory(Class<? extends Directory> type)
[4231]131 {
[6127]132 return _directoryByClass.containsKey(type);
[4231]133 }
[6127]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 }
[4231]149}
Note: See TracBrowser for help on using the repository browser.