A collection class that automatically groups Sizes by their AspectRatios.
| 27 | * A collection class that automatically groups {@link Size}s by their {@link AspectRatio}s. |
| 28 | */ |
| 29 | class SizeMap { |
| 30 | |
| 31 | private final ArrayMap<AspectRatio, SortedSet<Size>> mRatios = new ArrayMap<>(); |
| 32 | |
| 33 | /** |
| 34 | * Add a new {@link Size} to this collection. |
| 35 | * |
| 36 | * @param size The size to add. |
| 37 | * @return {@code true} if it is added, {@code false} if it already exists and is not added. |
| 38 | */ |
| 39 | public boolean add(Size size) { |
| 40 | for (AspectRatio ratio : mRatios.keySet()) { |
| 41 | if (ratio.matches(size)) { |
| 42 | final SortedSet<Size> sizes = mRatios.get(ratio); |
| 43 | if (sizes.contains(size)) { |
| 44 | return false; |
| 45 | } else { |
| 46 | sizes.add(size); |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | // None of the existing ratio matches the provided size; add a new key |
| 52 | SortedSet<Size> sizes = new TreeSet<>(); |
| 53 | sizes.add(size); |
| 54 | mRatios.put(AspectRatio.of(size.getWidth(), size.getHeight()), sizes); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Removes the specified aspect ratio and all sizes associated with it. |
| 60 | * |
| 61 | * @param ratio The aspect ratio to be removed. |
| 62 | */ |
| 63 | public void remove(AspectRatio ratio) { |
| 64 | mRatios.remove(ratio); |
| 65 | } |
| 66 | |
| 67 | Set<AspectRatio> ratios() { |
| 68 | return mRatios.keySet(); |
| 69 | } |
| 70 | |
| 71 | SortedSet<Size> sizes(AspectRatio ratio) { |
| 72 | return mRatios.get(ratio); |
| 73 | } |
| 74 | |
| 75 | Size defaultSize() { |
| 76 | return mRatios.get(AspectRatio.parse("4:3")).last(); |
| 77 | } |
| 78 | |
| 79 | void clear() { |
| 80 | mRatios.clear(); |
| 81 | } |
| 82 | |
| 83 | boolean isEmpty() { |
| 84 | return mRatios.isEmpty(); |
| 85 | } |
| 86 |
nothing calls this directly
no outgoing calls
no test coverage detected