All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.drew.metadata.Metadata Maven / Gradle / Ivy

Go to download

Java library for extracting EXIF, IPTC, XMP, ICC and other metadata from image and video files.

There is a newer version: 2.19.0
Show newest version
/*
 * Copyright 2002-2015 Drew Noakes
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 * More information about this project is available at:
 *
 *    https://drewnoakes.com/code/exif/
 *    https://github.com/drewnoakes/metadata-extractor
 */
package com.drew.metadata;

import com.drew.lang.annotations.NotNull;
import com.drew.lang.annotations.Nullable;

import java.util.*;

/**
 * A top-level object that holds the metadata values extracted from an image.
 * 

* Metadata objects may contain zero or more {@link Directory} objects. Each directory may contain zero or more tags * with corresponding values. * * @author Drew Noakes https://drewnoakes.com */ public final class Metadata { @NotNull private final Map,Collection> _directoryListByClass = new HashMap, Collection>(); /** * Returns an iterable set of the {@link Directory} instances contained in this metadata collection. * * @return an iterable set of directories */ @NotNull public Iterable getDirectories() { return new DirectoryIterable(_directoryListByClass); } @Nullable public Collection getDirectoriesOfType(Class type) { return (Collection)_directoryListByClass.get(type); } /** * Returns the count of directories in this metadata collection. * * @return the number of unique directory types set for this metadata collection */ public int getDirectoryCount() { int count = 0; for (Map.Entry,Collection> pair : _directoryListByClass.entrySet()) count += pair.getValue().size(); return count; } /** * Adds a directory to this metadata collection. * * @param directory the {@link Directory} to add into this metadata collection. */ public void addDirectory(@NotNull T directory) { getOrCreateDirectoryList(directory.getClass()).add(directory); } /** * Gets the first {@link Directory} of the specified type contained within this metadata collection. * If no instances of this type are present, null is returned. * * @param type the Directory type * @param the Directory type * @return the first Directory of type T in this metadata collection, or null if none exist */ @Nullable @SuppressWarnings("unchecked") public T getFirstDirectoryOfType(@NotNull Class type) { // We suppress the warning here as the code asserts a map signature of Class,T. // So after get(Class) it is for sure the result is from type T. Collection list = getDirectoryList(type); if (list == null || list.isEmpty()) return null; return (T)list.iterator().next(); } /** * Indicates whether an instance of the given directory type exists in this Metadata instance. * * @param type the {@link Directory} type * @return true if a {@link Directory} of the specified type exists, otherwise false */ public boolean containsDirectoryOfType(Class type) { Collection list = getDirectoryList(type); return list != null && !list.isEmpty(); } /** * Indicates whether any errors were reported during the reading of metadata values. * This value will be true if Directory.hasErrors() is true for one of the contained {@link Directory} objects. * * @return whether one of the contained directories has an error */ public boolean hasErrors() { for (Directory directory : getDirectories()) { if (directory.hasErrors()) return true; } return false; } @Override public String toString() { int count = getDirectoryCount(); return String.format("Metadata (%d %s)", count, count == 1 ? "directory" : "directories"); } @Nullable private Collection getDirectoryList(@NotNull Class type) { return _directoryListByClass.get(type); } @NotNull private Collection getOrCreateDirectoryList(@NotNull Class type) { Collection collection = getDirectoryList(type); if (collection != null) return collection; collection = new ArrayList(); _directoryListByClass.put(type, collection); return collection; } private static class DirectoryIterable implements Iterable { private final Map, Collection> _map; public DirectoryIterable(Map, Collection> map) { _map = map; } public Iterator iterator() { return new DirectoryIterator(_map); } private static class DirectoryIterator implements Iterator { @NotNull private final Iterator, Collection>> _mapIterator; @Nullable private Iterator _listIterator; public DirectoryIterator(Map, Collection> map) { _mapIterator = map.entrySet().iterator(); if (_mapIterator.hasNext()) _listIterator = _mapIterator.next().getValue().iterator(); } public boolean hasNext() { return _listIterator != null && (_listIterator.hasNext() || _mapIterator.hasNext()); } public Directory next() { if (_listIterator == null || (!_listIterator.hasNext() && !_mapIterator.hasNext())) throw new NoSuchElementException(); while (!_listIterator.hasNext()) _listIterator = _mapIterator.next().getValue().iterator(); return _listIterator.next(); } public void remove() { throw new UnsupportedOperationException(); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy