content: /*
   * Copyright (C) 2016 The Android Open Source Project
   *
   * 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.
   */
  
  package com.google.android.cameraview;
  
  import androidx.collection.ArrayMap;
  
  import java.util.Set;
  import java.util.SortedSet;
  import java.util.TreeSet;
  
  /**
   * A collection class that automatically groups {@link Size}s by their {@link AspectRatio}s.
   */
  class SizeMap {
  
      private final ArrayMap<AspectRatio, SortedSet<Size>> mRatios = new ArrayMap<>();
  
      /**
       * Add a new {@link Size} to this collection.
       *
       * @param size The size to add.
       * @return {@code true} if it is added, {@code false} if it already exists and is not added.
       */
      public boolean add(Size size) {
          for (AspectRatio ratio : mRatios.keySet()) {
              if (ratio.matches(size)) {
                  final SortedSet<Size> sizes = mRatios.get(ratio);
                  if (sizes.contains(size)) {
                      return false;
                  } else {
                      sizes.add(size);
                      return true;
                  }
              }
          }
          // None of the existing ratio matches the provided size; add a new key
          SortedSet<Size> sizes = new TreeSet<>();
          sizes.add(size);
          mRatios.put(AspectRatio.of(size.getWidth(), size.getHeight()), sizes);
          return true;
      }
  
      /**
       * Removes the specified aspect ratio and all sizes associated with it.
       *
       * @param ratio The aspect ratio to be removed.
       */
      public void remove(AspectRatio ratio) {
          mRatios.remove(ratio);
      }
  
      Set<AspectRatio> ratios() {
          return mRatios.keySet();
      }
  
      SortedSet<Size> sizes(AspectRatio ratio) {
          return mRatios.get(ratio);
      }
  
      void clear() {
          mRatios.clear();
      }
  
      boolean isEmpty() {
          return mRatios.isEmpty();
      }
  
  }
  