001/* 002 * Copyright (C) 2016 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import static com.google.common.base.Preconditions.checkNotNull; 020 021import com.google.common.annotations.GwtCompatible; 022import java.util.Comparator; 023import java.util.Iterator; 024import org.checkerframework.checker.nullness.qual.Nullable; 025 026/** 027 * Provides static methods for working with {@link Comparator} instances. For many other helpful 028 * comparator utilities, see either {@code Comparator} itself (for Java 8 or later), or {@code 029 * com.google.common.collect.Ordering} (otherwise). 030 * 031 * <h3>Relationship to {@code Ordering}</h3> 032 * 033 * <p>In light of the significant enhancements to {@code Comparator} in Java 8, the overwhelming 034 * majority of usages of {@code Ordering} can be written using only built-in JDK APIs. This class is 035 * intended to "fill the gap" and provide those features of {@code Ordering} not already provided by 036 * the JDK. 037 * 038 * @since 21.0 039 * @author Louis Wasserman 040 */ 041@GwtCompatible 042@ElementTypesAreNonnullByDefault 043public final class Comparators { 044 private Comparators() {} 045 046 /** 047 * Returns a new comparator which sorts iterables by comparing corresponding elements pairwise 048 * until a nonzero result is found; imposes "dictionary order." If the end of one iterable is 049 * reached, but not the other, the shorter iterable is considered to be less than the longer one. 050 * For example, a lexicographical natural ordering over integers considers {@code [] < [1] < [1, 051 * 1] < [1, 2] < [2]}. 052 * 053 * <p>Note that {@code Collections.reverseOrder(lexicographical(comparator))} is not equivalent to 054 * {@code lexicographical(Collections.reverseOrder(comparator))} (consider how each would order 055 * {@code [1]} and {@code [1, 1]}). 056 */ 057 // Note: 90% of the time we don't add type parameters or wildcards that serve only to "tweak" the 058 // desired return type. However, *nested* generics introduce a special class of problems that we 059 // think tip it over into being worthwhile. 060 public static <T extends @Nullable Object, S extends T> Comparator<Iterable<S>> lexicographical( 061 Comparator<T> comparator) { 062 return new LexicographicalOrdering<S>(checkNotNull(comparator)); 063 } 064 065 /** 066 * Returns {@code true} if each element in {@code iterable} after the first is greater than or 067 * equal to the element that preceded it, according to the specified comparator. Note that this is 068 * always true when the iterable has fewer than two elements. 069 */ 070 public static <T extends @Nullable Object> boolean isInOrder( 071 Iterable<? extends T> iterable, Comparator<T> comparator) { 072 checkNotNull(comparator); 073 Iterator<? extends T> it = iterable.iterator(); 074 if (it.hasNext()) { 075 T prev = it.next(); 076 while (it.hasNext()) { 077 T next = it.next(); 078 if (comparator.compare(prev, next) > 0) { 079 return false; 080 } 081 prev = next; 082 } 083 } 084 return true; 085 } 086 087 /** 088 * Returns {@code true} if each element in {@code iterable} after the first is <i>strictly</i> 089 * greater than the element that preceded it, according to the specified comparator. Note that 090 * this is always true when the iterable has fewer than two elements. 091 */ 092 public static <T extends @Nullable Object> boolean isInStrictOrder( 093 Iterable<? extends T> iterable, Comparator<T> comparator) { 094 checkNotNull(comparator); 095 Iterator<? extends T> it = iterable.iterator(); 096 if (it.hasNext()) { 097 T prev = it.next(); 098 while (it.hasNext()) { 099 T next = it.next(); 100 if (comparator.compare(prev, next) >= 0) { 101 return false; 102 } 103 prev = next; 104 } 105 } 106 return true; 107 } 108 109 /** 110 * Returns the minimum of the two values. If the values compare as 0, the first is returned. 111 * 112 * <p>The recommended solution for finding the {@code minimum} of some values depends on the type 113 * of your data and the number of elements you have. Read more in the Guava User Guide article on 114 * <a href="https://github.com/google/guava/wiki/CollectionUtilitiesExplained#comparators">{@code 115 * Comparators}</a>. 116 * 117 * @param a first value to compare, returned if less than or equal to b. 118 * @param b second value to compare. 119 * @throws ClassCastException if the parameters are not <i>mutually comparable</i>. 120 * @since 30.0 121 */ 122 public static <T extends Comparable<? super T>> T min(T a, T b) { 123 return (a.compareTo(b) <= 0) ? a : b; 124 } 125 126 /** 127 * Returns the minimum of the two values, according to the given comparator. If the values compare 128 * as equal, the first is returned. 129 * 130 * <p>The recommended solution for finding the {@code minimum} of some values depends on the type 131 * of your data and the number of elements you have. Read more in the Guava User Guide article on 132 * <a href="https://github.com/google/guava/wiki/CollectionUtilitiesExplained#comparators">{@code 133 * Comparators}</a>. 134 * 135 * @param a first value to compare, returned if less than or equal to b 136 * @param b second value to compare. 137 * @throws ClassCastException if the parameters are not <i>mutually comparable</i> using the given 138 * comparator. 139 * @since 30.0 140 */ 141 @ParametricNullness 142 public static <T extends @Nullable Object> T min( 143 @ParametricNullness T a, @ParametricNullness T b, Comparator<T> comparator) { 144 return (comparator.compare(a, b) <= 0) ? a : b; 145 } 146 147 /** 148 * Returns the maximum of the two values. If the values compare as 0, the first is returned. 149 * 150 * <p>The recommended solution for finding the {@code maximum} of some values depends on the type 151 * of your data and the number of elements you have. Read more in the Guava User Guide article on 152 * <a href="https://github.com/google/guava/wiki/CollectionUtilitiesExplained#comparators">{@code 153 * Comparators}</a>. 154 * 155 * @param a first value to compare, returned if greater than or equal to b. 156 * @param b second value to compare. 157 * @throws ClassCastException if the parameters are not <i>mutually comparable</i>. 158 * @since 30.0 159 */ 160 public static <T extends Comparable<? super T>> T max(T a, T b) { 161 return (a.compareTo(b) >= 0) ? a : b; 162 } 163 164 /** 165 * Returns the maximum of the two values, according to the given comparator. If the values compare 166 * as equal, the first is returned. 167 * 168 * <p>The recommended solution for finding the {@code maximum} of some values depends on the type 169 * of your data and the number of elements you have. Read more in the Guava User Guide article on 170 * <a href="https://github.com/google/guava/wiki/CollectionUtilitiesExplained#comparators">{@code 171 * Comparators}</a>. 172 * 173 * @param a first value to compare, returned if greater than or equal to b. 174 * @param b second value to compare. 175 * @throws ClassCastException if the parameters are not <i>mutually comparable</i> using the given 176 * comparator. 177 * @since 30.0 178 */ 179 @ParametricNullness 180 public static <T extends @Nullable Object> T max( 181 @ParametricNullness T a, @ParametricNullness T b, Comparator<T> comparator) { 182 return (comparator.compare(a, b) >= 0) ? a : b; 183 } 184}