1 | /*
|
---|
2 | * Copyright 2002-2017 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 | */
|
---|
21 | package com.drew.metadata;
|
---|
22 |
|
---|
23 | import com.drew.lang.annotations.NotNull;
|
---|
24 | import com.drew.lang.annotations.Nullable;
|
---|
25 |
|
---|
26 | import 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 | */
|
---|
36 | public final class Metadata
|
---|
37 | {
|
---|
38 | /**
|
---|
39 | * The list of {@link Directory} instances in this container, in the order they were added.
|
---|
40 | */
|
---|
41 | @NotNull
|
---|
42 | private final List<Directory> _directories = new ArrayList<>();
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Returns an iterable set of the {@link Directory} instances contained in this metadata collection.
|
---|
46 | *
|
---|
47 | * @return an iterable set of directories
|
---|
48 | */
|
---|
49 | @NotNull
|
---|
50 | public Iterable<Directory> getDirectories()
|
---|
51 | {
|
---|
52 | return _directories;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @NotNull
|
---|
56 | @SuppressWarnings("unchecked")
|
---|
57 | public <T extends Directory> Collection<T> getDirectoriesOfType(Class<T> type)
|
---|
58 | {
|
---|
59 | List<T> directories = new ArrayList<>();
|
---|
60 | for (Directory dir : _directories) {
|
---|
61 | if (type.isAssignableFrom(dir.getClass())) {
|
---|
62 | directories.add((T)dir);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | return directories;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Returns the count of directories in this metadata collection.
|
---|
70 | *
|
---|
71 | * @return the number of unique directory types set for this metadata collection
|
---|
72 | */
|
---|
73 | public int getDirectoryCount()
|
---|
74 | {
|
---|
75 | return _directories.size();
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Adds a directory to this metadata collection.
|
---|
80 | *
|
---|
81 | * @param directory the {@link Directory} to add into this metadata collection.
|
---|
82 | */
|
---|
83 | public <T extends Directory> void addDirectory(@NotNull T directory)
|
---|
84 | {
|
---|
85 | _directories.add(directory);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Gets the first {@link Directory} of the specified type contained within this metadata collection.
|
---|
90 | * If no instances of this type are present, <code>null</code> is returned.
|
---|
91 | *
|
---|
92 | * @param type the Directory type
|
---|
93 | * @param <T> the Directory type
|
---|
94 | * @return the first Directory of type T in this metadata collection, or <code>null</code> if none exist
|
---|
95 | */
|
---|
96 | @Nullable
|
---|
97 | @SuppressWarnings("unchecked")
|
---|
98 | public <T extends Directory> T getFirstDirectoryOfType(@NotNull Class<T> type)
|
---|
99 | {
|
---|
100 | for (Directory dir : _directories) {
|
---|
101 | if (type.isAssignableFrom(dir.getClass()))
|
---|
102 | return (T)dir;
|
---|
103 | }
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Indicates whether an instance of the given directory type exists in this Metadata instance.
|
---|
109 | *
|
---|
110 | * @param type the {@link Directory} type
|
---|
111 | * @return <code>true</code> if a {@link Directory} of the specified type exists, otherwise <code>false</code>
|
---|
112 | */
|
---|
113 | public boolean containsDirectoryOfType(Class<? extends Directory> type)
|
---|
114 | {
|
---|
115 | for (Directory dir : _directories) {
|
---|
116 | if (type.isAssignableFrom(dir.getClass()))
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Indicates whether any errors were reported during the reading of metadata values.
|
---|
124 | * This value will be true if Directory.hasErrors() is true for one of the contained {@link Directory} objects.
|
---|
125 | *
|
---|
126 | * @return whether one of the contained directories has an error
|
---|
127 | */
|
---|
128 | public boolean hasErrors()
|
---|
129 | {
|
---|
130 | for (Directory directory : getDirectories()) {
|
---|
131 | if (directory.hasErrors())
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public String toString()
|
---|
139 | {
|
---|
140 | int count = getDirectoryCount();
|
---|
141 | return String.format("Metadata (%d %s)",
|
---|
142 | count,
|
---|
143 | count == 1
|
---|
144 | ? "directory"
|
---|
145 | : "directories");
|
---|
146 | }
|
---|
147 | }
|
---|