1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17package android.hardware.camera2.legacy; 18 19import android.hardware.Camera; 20 21import java.util.Collections; 22import java.util.Comparator; 23import java.util.List; 24 25import static com.android.internal.util.Preconditions.*; 26 27/** 28 * Comparator for api1 {@link Camera.Size} objects by the area. 29 * 30 * <p>This comparator totally orders by rectangle area. Tie-breaks on width.</p> 31 */ 32@SuppressWarnings("deprecation") 33public class SizeAreaComparator implements Comparator<Camera.Size> { 34 /** 35 * {@inheritDoc} 36 */ 37 @Override 38 public int compare(Camera.Size size, Camera.Size size2) { 39 checkNotNull(size, "size must not be null"); 40 checkNotNull(size2, "size2 must not be null"); 41 42 if (size.equals(size2)) { 43 return 0; 44 } 45 46 long width = size.width; 47 long width2 = size2.width; 48 long area = width * size.height; 49 long area2 = width2 * size2.height; 50 51 if (area == area2) { 52 return (width > width2) ? 1 : -1; 53 } 54 55 return (area > area2) ? 1 : -1; 56 } 57 58 /** 59 * Get the largest api1 {@code Camera.Size} from the list by comparing each size's area 60 * by each other using {@link SizeAreaComparator}. 61 * 62 * @param sizes a non-{@code null} list of non-{@code null} sizes 63 * @return a non-{@code null} size 64 * 65 * @throws NullPointerException if {@code sizes} or any elements in it were {@code null} 66 */ 67 public static Camera.Size findLargestByArea(List<Camera.Size> sizes) { 68 checkNotNull(sizes, "sizes must not be null"); 69 70 return Collections.max(sizes, new SizeAreaComparator()); 71 } 72} 73