| 1 | /* |
| 2 | * Copyright 2002-2019 Drew Noakes and contributors |
| 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<Directory>(); |
| 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<T>(); |
| 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 | if (directory == null) { |
| 86 | throw new IllegalArgumentException("Directory may not be null."); |
| 87 | } |
| 88 | |
| 89 | _directories.add(directory); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Gets the first {@link Directory} of the specified type contained within this metadata collection. |
| 94 | * If no instances of this type are present, <code>null</code> is returned. |
| 95 | * |
| 96 | * @param type the Directory type |
| 97 | * @param <T> the Directory type |
| 98 | * @return the first Directory of type T in this metadata collection, or <code>null</code> if none exist |
| 99 | */ |
| 100 | @Nullable |
| 101 | @SuppressWarnings("unchecked") |
| 102 | public <T extends Directory> T getFirstDirectoryOfType(@NotNull Class<T> type) |
| 103 | { |
| 104 | for (Directory dir : _directories) { |
| 105 | if (type.isAssignableFrom(dir.getClass())) |
| 106 | return (T)dir; |
| 107 | } |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Indicates whether an instance of the given directory type exists in this Metadata instance. |
| 113 | * |
| 114 | * @param type the {@link Directory} type |
| 115 | * @return <code>true</code> if a {@link Directory} of the specified type exists, otherwise <code>false</code> |
| 116 | */ |
| 117 | public boolean containsDirectoryOfType(Class<? extends Directory> type) |
| 118 | { |
| 119 | for (Directory dir : _directories) { |
| 120 | if (type.isAssignableFrom(dir.getClass())) |
| 121 | return true; |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Indicates whether any errors were reported during the reading of metadata values. |
| 128 | * This value will be true if Directory.hasErrors() is true for one of the contained {@link Directory} objects. |
| 129 | * |
| 130 | * @return whether one of the contained directories has an error |
| 131 | */ |
| 132 | public boolean hasErrors() |
| 133 | { |
| 134 | for (Directory directory : getDirectories()) { |
| 135 | if (directory.hasErrors()) |
| 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public String toString() |
| 143 | { |
| 144 | int count = getDirectoryCount(); |
| 145 | return String.format("Metadata (%d %s)", |
| 146 | count, |
| 147 | count == 1 |
| 148 | ? "directory" |
| 149 | : "directories"); |
| 150 | } |
| 151 | } |