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

Last change on this file since 8132 was 8132, checked in by Don-vip, 10 years ago

fix #11162 - update to metadata-extractor 2.7.2

File size: 5.3 KB
Line 
1/*
2 * Copyright 2002-2015 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 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21package com.drew.metadata;
22
23import com.drew.lang.annotations.NotNull;
24import com.drew.lang.annotations.Nullable;
25
26import java.util.*;
27
28/**
29 * A top-level object that holds the metadata values extracted from an image.
30 * <p>
31 * Metadata objects may contain zero or more {@link Directory} objects. Each directory may contain zero or more tags
32 * with corresponding values.
33 *
34 * @author Drew Noakes https://drewnoakes.com
35 */
36public final class Metadata
37{
38 @NotNull
39 private final Map<Class<? extends Directory>,Directory> _directoryByClass = new HashMap<Class<? extends Directory>, Directory>();
40
41 /**
42 * List of Directory objects set against this object. Keeping a list handy makes
43 * creation of an Iterator and counting tags simple.
44 */
45 @NotNull
46 private final Collection<Directory> _directoryList = new ArrayList<Directory>();
47
48 /**
49 * Returns an objects for iterating over Directory objects in the order in which they were added.
50 *
51 * @return an iterable collection of directories
52 */
53 @NotNull
54 public Iterable<Directory> getDirectories()
55 {
56 return Collections.unmodifiableCollection(_directoryList);
57 }
58
59 /**
60 * Returns a count of unique directories in this metadata collection.
61 *
62 * @return the number of unique directory types set for this metadata collection
63 */
64 public int getDirectoryCount()
65 {
66 return _directoryList.size();
67 }
68
69 /**
70 * Returns a {@link Directory} of specified type. If this {@link Metadata} object already contains
71 * such a directory, it is returned. Otherwise a new instance of this directory will be created and stored within
72 * this {@link Metadata} object.
73 *
74 * @param type the type of the Directory implementation required.
75 * @return a directory of the specified type.
76 */
77 @NotNull
78 @SuppressWarnings("unchecked")
79 public <T extends Directory> T getOrCreateDirectory(@NotNull Class<T> type)
80 {
81 // We suppress the warning here as the code asserts a map signature of Class<T>,T.
82 // So after get(Class<T>) it is for sure the result is from type T.
83
84 // check if we've already issued this type of directory
85 if (_directoryByClass.containsKey(type))
86 return (T)_directoryByClass.get(type);
87
88 T directory;
89 try {
90 directory = type.newInstance();
91 } catch (Exception e) {
92 throw new RuntimeException("Cannot instantiate provided Directory type: " + type.toString());
93 }
94 // store the directory
95 _directoryByClass.put(type, directory);
96 _directoryList.add(directory);
97
98 return directory;
99 }
100
101 /**
102 * If this {@link Metadata} object contains a {@link Directory} of the specified type, it is returned.
103 * Otherwise <code>null</code> is returned.
104 *
105 * @param type the Directory type
106 * @param <T> the Directory type
107 * @return a Directory of type T if it exists in this {@link Metadata} object, otherwise <code>null</code>.
108 */
109 @Nullable
110 @SuppressWarnings("unchecked")
111 public <T extends Directory> T getDirectory(@NotNull Class<T> type)
112 {
113 // We suppress the warning here as the code asserts a map signature of Class<T>,T.
114 // So after get(Class<T>) it is for sure the result is from type T.
115
116 return (T)_directoryByClass.get(type);
117 }
118
119 /**
120 * Indicates whether a given directory type has been created in this metadata
121 * repository. Directories are created by calling {@link Metadata#getOrCreateDirectory(Class)}.
122 *
123 * @param type the {@link Directory} type
124 * @return true if the {@link Directory} has been created
125 */
126 public boolean containsDirectory(Class<? extends Directory> type)
127 {
128 return _directoryByClass.containsKey(type);
129 }
130
131 /**
132 * Indicates whether any errors were reported during the reading of metadata values.
133 * This value will be true if Directory.hasErrors() is true for one of the contained {@link Directory} objects.
134 *
135 * @return whether one of the contained directories has an error
136 */
137 public boolean hasErrors()
138 {
139 for (Directory directory : _directoryList) {
140 if (directory.hasErrors())
141 return true;
142 }
143 return false;
144 }
145
146 @Override
147 public String toString()
148 {
149 return String.format("Metadata (%d %s)",
150 _directoryList.size(),
151 _directoryList.size() == 1
152 ? "directory"
153 : "directories");
154 }
155}
Note: See TracBrowser for help on using the repository browser.