Index: trunk/src/com/drew/metadata/Metadata.java
===================================================================
--- trunk/src/com/drew/metadata/Metadata.java	(revision 4231)
+++ trunk/src/com/drew/metadata/Metadata.java	(revision 6127)
@@ -1,85 +1,72 @@
 /*
- * Metadata.java
+ * Copyright 2002-2012 Drew Noakes
  *
- * This class is public domain software - that is, you can do whatever you want
- * with it, and include it software that is licensed under the GNU or the
- * BSD license, or whatever other licence you choose, including proprietary
- * closed source licenses.  Similarly, I release this Java version under the
- * same license, though I do ask that you leave this header in tact.
+ *    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
  *
- * If you make modifications to this code that you think would benefit the
- * wider community, please send me a copy and I'll post it on my site.
+ *        http://www.apache.org/licenses/LICENSE-2.0
  *
- * If you make use of this code, I'd appreciate hearing about it.
- *   drew.noakes@drewnoakes.com
- * Latest version of this software kept at
- *   http://drewnoakes.com/
+ *    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.
  *
- * Created on 28 April 2002, 17:40
- * Modified 04 Aug 2002
- * - Adjusted javadoc
- * - Added
- * Modified 29 Oct 2002 (v1.2)
- * - Stored IFD directories in separate tag-spaces
- * - iterator() now returns an Iterator over a list of TagValue objects
- * - More get*Description() methods to detail GPS tags, among others
- * - Put spaces between words of tag name for presentation reasons (they had no
- *   significance in compound form)
+ * More information about this project is available at:
+ *
+ *    http://drewnoakes.com/code/exif/
+ *    http://code.google.com/p/metadata-extractor/
  */
 package com.drew.metadata;
 
-import java.io.Serializable;
+import com.drew.lang.annotations.NotNull;
+import com.drew.lang.annotations.Nullable;
+
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
-import java.util.Iterator;
+import java.util.Map;
 
 /**
- * Result from an exif extraction operation, containing all tags, their
- * values and support for retrieving them.
- * @author  Drew Noakes http://drewnoakes.com
+ * A top-level object to hold the various types of metadata (Exif/IPTC/etc) related to one entity (such as a file
+ * or stream).
+ * <p/>
+ * Metadata objects may contain zero or more directories.  Each directory may contain zero or more tags with
+ * corresponding values.
+ *
+ * @author Drew Noakes http://drewnoakes.com
  */
-public final class Metadata implements Serializable
+public final class Metadata
 {
-    /**
-     *
-     */
-    private final HashMap directoryMap;
-
+    @NotNull
+    private final Map<Class<? extends Directory>,Directory> _directoryByClass = new HashMap<Class<? extends Directory>, Directory>();
+    
     /**
      * List of Directory objects set against this object.  Keeping a list handy makes
      * creation of an Iterator and counting tags simple.
      */
-    private final ArrayList directoryList;
+    @NotNull
+    private final Collection<Directory> _directoryList = new ArrayList<Directory>();
 
     /**
-     * Creates a new instance of Metadata.  Package private.
+     * Returns an objects for iterating over Directory objects in the order in which they were added.
+     *
+     * @return an iterable collection of directories
      */
-    public Metadata()
+    @NotNull
+    public Iterable<Directory> getDirectories()
     {
-        directoryMap = new HashMap();
-        directoryList = new ArrayList();
-    }
-
-
-// OTHER METHODS
-
-    /**
-     * Creates an Iterator over the tag types set against this image, preserving the order
-     * in which they were set.  Should the same tag have been set more than once, it's first
-     * position is maintained, even though the final value is used.
-     * @return an Iterator of tag types set for this image
-     */
-    public Iterator getDirectoryIterator()
-    {
-        return directoryList.iterator();
+        return _directoryList;
     }
 
     /**
      * Returns a count of unique directories in this metadata collection.
+     *
      * @return the number of unique directory types set for this metadata collection
      */
     public int getDirectoryCount()
     {
-        return directoryList.size();
+        return _directoryList.size();
     }
 
@@ -88,17 +75,20 @@
      * such a directory, it is returned.  Otherwise a new instance of this directory will be created and stored within
      * this Metadata object.
+     *
      * @param type the type of the Directory implementation required.
      * @return a directory of the specified type.
      */
-    public Directory getDirectory(Class type)
+    @NotNull
+    @SuppressWarnings("unchecked")
+    public <T extends Directory> T getOrCreateDirectory(@NotNull Class<T> type)
     {
-        if (!Directory.class.isAssignableFrom(type)) {
-            throw new RuntimeException("Class type passed to getDirectory must be an implementation of com.drew.metadata.Directory");
-        }
+        // We suppress the warning here as the code asserts a map signature of Class<T>,T.
+        // So after get(Class<T>) it is for sure the result is from type T.
+
         // check if we've already issued this type of directory
-        if (directoryMap.containsKey(type)) {
-            return (Directory)directoryMap.get(type);
-        }
-        Object directory;
+        if (_directoryByClass.containsKey(type))
+            return (T)_directoryByClass.get(type);
+
+        T directory;
         try {
             directory = type.newInstance();
@@ -106,19 +96,54 @@
             throw new RuntimeException("Cannot instantiate provided Directory type: " + type.toString());
         }
-        // store the directory in case it's requested later
-        directoryMap.put(type, directory);
-        directoryList.add(directory);
-        return (Directory)directory;
+        // store the directory
+        _directoryByClass.put(type, directory);
+        _directoryList.add(directory);
+
+        return directory;
+    }
+
+    /**
+     * If this <code>Metadata</code> object contains a <code>Directory</code> of the specified type, it is returned.
+     * Otherwise <code>null</code> is returned.
+     *
+     * @param type the Directory type
+     * @param <T> the Directory type
+     * @return a Directory of type T if it exists in this Metadata object, otherwise <code>null</code>.
+     */
+    @Nullable
+    @SuppressWarnings("unchecked")
+    public <T extends Directory> T getDirectory(@NotNull Class<T> type)
+    {
+        // We suppress the warning here as the code asserts a map signature of Class<T>,T.
+        // So after get(Class<T>) it is for sure the result is from type T.
+
+        return (T)_directoryByClass.get(type);
     }
 
     /**
      * Indicates whether a given directory type has been created in this metadata
-     * repository.  Directories are created by calling getDirectory(Class).
+     * repository.  Directories are created by calling <code>getOrCreateDirectory(Class)</code>.
+     *
      * @param type the Directory type
      * @return true if the metadata directory has been created
      */
-    public boolean containsDirectory(Class type)
+    public boolean containsDirectory(Class<? extends Directory> type)
     {
-        return directoryMap.containsKey(type);
+        return _directoryByClass.containsKey(type);
+    }
+
+    /**
+     * 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 Directory objects.
+     *
+     * @return whether one of the contained directories has an error
+     */
+    public boolean hasErrors()
+    {
+        for (Directory directory : _directoryList) {
+            if (directory.hasErrors())
+                return true;
+        }
+        return false;
     }
 }
