Changeset 10862 in josm for trunk/src/com/drew/metadata/Metadata.java
- Timestamp:
- 2016-08-20T20:58:03+02:00 (10 years ago)
- File:
-
- 1 edited
-
trunk/src/com/drew/metadata/Metadata.java (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/metadata/Metadata.java
r8243 r10862 1 1 /* 2 * Copyright 2002-201 5Drew Noakes2 * Copyright 2002-2016 Drew Noakes 3 3 * 4 4 * Licensed under the Apache License, Version 2.0 (the "License"); … … 36 36 public final class Metadata 37 37 { 38 /** 39 * The list of {@link Directory} instances in this container, in the order they were added. 40 */ 38 41 @NotNull 39 private final Map<Class<? extends Directory>,Collection<Directory>>_directoryListByClass = newHashMap<Class<? extends Directory>, Collection<Directory>>();42 private final List<Directory> _directories = new ArrayList<>(); 40 43 41 44 /** … … 47 50 public Iterable<Directory> getDirectories() 48 51 { 49 return new DirectoryIterable(_directoryListByClass);52 return _directories; 50 53 } 51 54 52 55 @Nullable 56 @SuppressWarnings("unchecked") 53 57 public <T extends Directory> Collection<T> getDirectoriesOfType(Class<T> type) 54 58 { 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; 56 66 } 57 67 … … 63 73 public int getDirectoryCount() 64 74 { 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(); 69 76 } 70 77 … … 76 83 public <T extends Directory> void addDirectory(@NotNull T directory) 77 84 { 78 getOrCreateDirectoryList(directory.getClass()).add(directory);85 _directories.add(directory); 79 86 } 80 87 … … 91 98 public <T extends Directory> T getFirstDirectoryOfType(@NotNull Class<T> type) 92 99 { 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; 102 105 } 103 106 … … 110 113 public boolean containsDirectoryOfType(Class<? extends Directory> type) 111 114 { 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; 114 120 } 115 121 … … 139 145 : "directories"); 140 146 } 141 142 @Nullable143 private <T extends Directory> Collection<Directory> getDirectoryList(@NotNull Class<T> type)144 {145 return _directoryListByClass.get(type);146 }147 148 @NotNull149 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 @NotNull176 private final Iterator<Map.Entry<Class<? extends Directory>, Collection<Directory>>> _mapIterator;177 @Nullable178 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 }210 147 }
Note:
See TracChangeset
for help on using the changeset viewer.
