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