Ignore:
Timestamp:
2016-08-20T20:58:03+02:00 (10 years ago)
Author:
Don-vip
Message:

update to metadata-extractor 2.9.1

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/com/drew/metadata/Metadata.java

    r8243 r10862  
    11/*
    2  * Copyright 2002-2015 Drew Noakes
     2 * Copyright 2002-2016 Drew Noakes
    33 *
    44 *    Licensed under the Apache License, Version 2.0 (the "License");
     
    3636public final class Metadata
    3737{
     38    /**
     39     * The list of {@link Directory} instances in this container, in the order they were added.
     40     */
    3841    @NotNull
    39     private final Map<Class<? extends Directory>,Collection<Directory>> _directoryListByClass = new HashMap<Class<? extends Directory>, Collection<Directory>>();
     42    private final List<Directory> _directories = new ArrayList<>();
    4043
    4144    /**
     
    4750    public Iterable<Directory> getDirectories()
    4851    {
    49         return new DirectoryIterable(_directoryListByClass);
     52        return _directories;
    5053    }
    5154
    5255    @Nullable
     56    @SuppressWarnings("unchecked")
    5357    public <T extends Directory> Collection<T> getDirectoriesOfType(Class<T> type)
    5458    {
    55         return (Collection<T>)_directoryListByClass.get(type);
     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;
    5666    }
    5767
     
    6373    public int getDirectoryCount()
    6474    {
    65         int count = 0;
    66         for (Map.Entry<Class<? extends Directory>,Collection<Directory>> pair : _directoryListByClass.entrySet())
    67             count += pair.getValue().size();
    68         return count;
     75        return _directories.size();
    6976    }
    7077
     
    7683    public <T extends Directory> void addDirectory(@NotNull T directory)
    7784    {
    78         getOrCreateDirectoryList(directory.getClass()).add(directory);
     85        _directories.add(directory);
    7986    }
    8087
     
    9198    public <T extends Directory> T getFirstDirectoryOfType(@NotNull Class<T> type)
    9299    {
    93         // We suppress the warning here as the code asserts a map signature of Class<T>,T.
    94         // So after get(Class<T>) it is for sure the result is from type T.
    95 
    96         Collection<Directory> list = getDirectoryList(type);
    97 
    98         if (list == null || list.isEmpty())
    99             return null;
    100 
    101         return (T)list.iterator().next();
     100        for (Directory dir : _directories) {
     101            if (type.isAssignableFrom(dir.getClass()))
     102                return (T)dir;
     103        }
     104        return null;
    102105    }
    103106
     
    110113    public boolean containsDirectoryOfType(Class<? extends Directory> type)
    111114    {
    112         Collection<Directory> list = getDirectoryList(type);
    113         return list != null && !list.isEmpty();
     115        for (Directory dir : _directories) {
     116            if (type.isAssignableFrom(dir.getClass()))
     117                return true;
     118        }
     119        return false;
    114120    }
    115121
     
    139145                : "directories");
    140146    }
    141 
    142     @Nullable
    143     private <T extends Directory> Collection<Directory> getDirectoryList(@NotNull Class<T> type)
    144     {
    145         return _directoryListByClass.get(type);
    146     }
    147 
    148     @NotNull
    149     private <T extends Directory> Collection<Directory> getOrCreateDirectoryList(@NotNull Class<T> type)
    150     {
    151         Collection<Directory> collection = getDirectoryList(type);
    152         if (collection != null)
    153             return collection;
    154         collection = new ArrayList<Directory>();
    155         _directoryListByClass.put(type, collection);
    156         return collection;
    157     }
    158 
    159     private static class DirectoryIterable implements Iterable<Directory>
    160     {
    161         private final Map<Class<? extends Directory>, Collection<Directory>> _map;
    162 
    163         public DirectoryIterable(Map<Class<? extends Directory>, Collection<Directory>> map)
    164         {
    165             _map = map;
    166         }
    167 
    168         public Iterator<Directory> iterator()
    169         {
    170             return new DirectoryIterator(_map);
    171         }
    172 
    173         private static class DirectoryIterator implements Iterator<Directory>
    174         {
    175             @NotNull
    176             private final Iterator<Map.Entry<Class<? extends Directory>, Collection<Directory>>> _mapIterator;
    177             @Nullable
    178             private Iterator<Directory> _listIterator;
    179 
    180             public DirectoryIterator(Map<Class<? extends Directory>, Collection<Directory>> map)
    181             {
    182                 _mapIterator = map.entrySet().iterator();
    183 
    184                 if (_mapIterator.hasNext())
    185                     _listIterator = _mapIterator.next().getValue().iterator();
    186             }
    187 
    188             public boolean hasNext()
    189             {
    190                 return _listIterator != null && (_listIterator.hasNext() || _mapIterator.hasNext());
    191             }
    192 
    193             public Directory next()
    194             {
    195                 if (_listIterator == null || (!_listIterator.hasNext() && !_mapIterator.hasNext()))
    196                     throw new NoSuchElementException();
    197 
    198                 while (!_listIterator.hasNext())
    199                     _listIterator = _mapIterator.next().getValue().iterator();
    200 
    201                 return _listIterator.next();
    202             }
    203 
    204             public void remove()
    205             {
    206                 throw new UnsupportedOperationException();
    207             }
    208         }
    209     }
    210147}
Note: See TracChangeset for help on using the changeset viewer.