[8132] | 1 | /*
|
---|
[13061] | 2 | * Copyright 2002-2017 Drew Noakes
|
---|
[8132] | 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 | {
|
---|
[10862] | 38 | /**
|
---|
| 39 | * The list of {@link Directory} instances in this container, in the order they were added.
|
---|
| 40 | */
|
---|
[8132] | 41 | @NotNull
|
---|
[10862] | 42 | private final List<Directory> _directories = new ArrayList<>();
|
---|
[8132] | 43 |
|
---|
| 44 | /**
|
---|
[8243] | 45 | * Returns an iterable set of the {@link Directory} instances contained in this metadata collection.
|
---|
[8132] | 46 | *
|
---|
[8243] | 47 | * @return an iterable set of directories
|
---|
[8132] | 48 | */
|
---|
| 49 | @NotNull
|
---|
| 50 | public Iterable<Directory> getDirectories()
|
---|
| 51 | {
|
---|
[10862] | 52 | return _directories;
|
---|
[8132] | 53 | }
|
---|
| 54 |
|
---|
[13061] | 55 | @NotNull
|
---|
[10862] | 56 | @SuppressWarnings("unchecked")
|
---|
[8243] | 57 | public <T extends Directory> Collection<T> getDirectoriesOfType(Class<T> type)
|
---|
| 58 | {
|
---|
[10862] | 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;
|
---|
[8243] | 66 | }
|
---|
| 67 |
|
---|
[8132] | 68 | /**
|
---|
[8243] | 69 | * Returns the count of directories in this metadata collection.
|
---|
[8132] | 70 | *
|
---|
| 71 | * @return the number of unique directory types set for this metadata collection
|
---|
| 72 | */
|
---|
| 73 | public int getDirectoryCount()
|
---|
| 74 | {
|
---|
[10862] | 75 | return _directories.size();
|
---|
[8132] | 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
[8243] | 79 | * Adds a directory to this metadata collection.
|
---|
[8132] | 80 | *
|
---|
[8243] | 81 | * @param directory the {@link Directory} to add into this metadata collection.
|
---|
[8132] | 82 | */
|
---|
[8243] | 83 | public <T extends Directory> void addDirectory(@NotNull T directory)
|
---|
[8132] | 84 | {
|
---|
[10862] | 85 | _directories.add(directory);
|
---|
[8132] | 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
[8243] | 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.
|
---|
[8132] | 91 | *
|
---|
| 92 | * @param type the Directory type
|
---|
| 93 | * @param <T> the Directory type
|
---|
[8243] | 94 | * @return the first Directory of type T in this metadata collection, or <code>null</code> if none exist
|
---|
[8132] | 95 | */
|
---|
| 96 | @Nullable
|
---|
| 97 | @SuppressWarnings("unchecked")
|
---|
[8243] | 98 | public <T extends Directory> T getFirstDirectoryOfType(@NotNull Class<T> type)
|
---|
[8132] | 99 | {
|
---|
[10862] | 100 | for (Directory dir : _directories) {
|
---|
| 101 | if (type.isAssignableFrom(dir.getClass()))
|
---|
| 102 | return (T)dir;
|
---|
| 103 | }
|
---|
| 104 | return null;
|
---|
[8132] | 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
[8243] | 108 | * Indicates whether an instance of the given directory type exists in this Metadata instance.
|
---|
[8132] | 109 | *
|
---|
| 110 | * @param type the {@link Directory} type
|
---|
[8243] | 111 | * @return <code>true</code> if a {@link Directory} of the specified type exists, otherwise <code>false</code>
|
---|
[8132] | 112 | */
|
---|
[8243] | 113 | public boolean containsDirectoryOfType(Class<? extends Directory> type)
|
---|
[8132] | 114 | {
|
---|
[10862] | 115 | for (Directory dir : _directories) {
|
---|
| 116 | if (type.isAssignableFrom(dir.getClass()))
|
---|
| 117 | return true;
|
---|
| 118 | }
|
---|
| 119 | return false;
|
---|
[8132] | 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 | {
|
---|
[8243] | 130 | for (Directory directory : getDirectories()) {
|
---|
[8132] | 131 | if (directory.hasErrors())
|
---|
| 132 | return true;
|
---|
| 133 | }
|
---|
| 134 | return false;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | @Override
|
---|
| 138 | public String toString()
|
---|
| 139 | {
|
---|
[8243] | 140 | int count = getDirectoryCount();
|
---|
[8132] | 141 | return String.format("Metadata (%d %s)",
|
---|
[8243] | 142 | count,
|
---|
| 143 | count == 1
|
---|
[8132] | 144 | ? "directory"
|
---|
| 145 | : "directories");
|
---|
| 146 | }
|
---|
| 147 | }
|
---|