@Override public <E extends T> E min(E a, E b) { return forwardOrder.max(a, b); }
@Override public <E extends T> E min(Iterable<E> iterable) { return forwardOrder.max(iterable); }
@Override public <E extends T> E min(Iterator<E> iterator) { return forwardOrder.max(iterator); }
@Override public <E extends T> E min(E a, E b, E c, E... rest) { return forwardOrder.max(a, b, c, rest); }
@Override public <E extends T> E min(E a, E b) { return forwardOrder.max(a, b); }
@Override public <E extends T> E min(Iterator<E> iterator) { return forwardOrder.max(iterator); }
@Override public <E extends T> E min(Iterable<E> iterable) { return forwardOrder.max(iterable); }
/** * Returns the greatest of the specified values according to this ordering. If there are multiple * greatest values, the first of those is returned. * * <p><b>Java 8 users:</b> If {@code iterable} is a {@link Collection}, use {@code * Collections.max(collection, thisComparator)} instead. Otherwise, continue to use this method * for now. After the next release of Guava, use {@code * Streams.stream(iterable).max(thisComparator).get()} instead. Note that these alternatives do * not guarantee which tied maximum element is returned) * * @param iterable the iterable whose maximum element is to be determined * @throws NoSuchElementException if {@code iterable} is empty * @throws ClassCastException if the parameters are not <i>mutually comparable</i> under this * ordering. */ @CanIgnoreReturnValue // TODO(kak): Consider removing this public <E extends T> E max(Iterable<E> iterable) { return max(iterable.iterator()); }
/** * Returns the greatest of the specified values according to this ordering. If there are multiple * greatest values, the first of those is returned. The iterator will be left exhausted: its * {@code hasNext()} method will return {@code false}. * * <p><b>Java 8 users:</b> Continue to use this method for now. After the next release of Guava, * use {@code Streams.stream(iterator).max(thisComparator).get()} instead (but note that it does * not guarantee which tied maximum element is returned). * * @param iterator the iterator whose maximum element is to be determined * @throws NoSuchElementException if {@code iterator} is empty * @throws ClassCastException if the parameters are not <i>mutually comparable</i> under this * ordering. * @since 11.0 */ @CanIgnoreReturnValue // TODO(kak): Consider removing this public <E extends T> E max(Iterator<E> iterator) { // let this throw NoSuchElementException as necessary E maxSoFar = iterator.next(); while (iterator.hasNext()) { maxSoFar = max(maxSoFar, iterator.next()); } return maxSoFar; }
/** * Returns the minimal range that {@linkplain Range#contains(Comparable) contains} all of the * given values. The returned range is {@linkplain BoundType#CLOSED closed} on both ends. * * @throws ClassCastException if the values are not mutually comparable * @throws NoSuchElementException if {@code values} is empty * @throws NullPointerException if any of {@code values} is null * @since 14.0 */ public static <C extends Comparable<?>> Range<C> encloseAll(Iterable<C> values) { checkNotNull(values); if (values instanceof SortedSet) { SortedSet<? extends C> set = cast(values); Comparator<?> comparator = set.comparator(); if (Ordering.natural().equals(comparator) || comparator == null) { return closed(set.first(), set.last()); } } Iterator<C> valueIterator = values.iterator(); C min = checkNotNull(valueIterator.next()); C max = min; while (valueIterator.hasNext()) { C value = checkNotNull(valueIterator.next()); min = Ordering.natural().min(min, value); max = Ordering.natural().max(max, value); } return closed(min, max); }
/** * Returns the greatest of the specified values according to this ordering. If there are multiple * greatest values, the first of those is returned. * * <p><b>Java 8 users:</b> Use {@code Collections.max(Arrays.asList(a, b, c...), thisComparator)} * instead (but note that it does not guarantee which tied maximum element is returned). * * @param a value to compare, returned if greater than or equal to the rest. * @param b value to compare * @param c value to compare * @param rest values to compare * @throws ClassCastException if the parameters are not <i>mutually comparable</i> under this * ordering. */ @CanIgnoreReturnValue // TODO(kak): Consider removing this public <E extends T> E max(@Nullable E a, @Nullable E b, @Nullable E c, E... rest) { E maxSoFar = max(max(a, b), c); for (E r : rest) { maxSoFar = max(maxSoFar, r); } return maxSoFar; }
@Override public ContiguousSet<C> intersection(ContiguousSet<C> other) { checkNotNull(other); checkArgument(this.domain.equals(other.domain)); if (other.isEmpty()) { return other; } else { C lowerEndpoint = Ordering.natural().max(this.first(), other.first()); C upperEndpoint = Ordering.natural().min(this.last(), other.last()); return (lowerEndpoint.compareTo(upperEndpoint) <= 0) ? ContiguousSet.create(Range.closed(lowerEndpoint, upperEndpoint), domain) : new EmptyContiguousSet<C>(domain); } }
public void testIteratorMaxExhaustsIterator() { List<Integer> ints = Lists.newArrayList(9, 0, 3, 5); Iterator<Integer> iterator = ints.iterator(); assertEquals(9, (int) numberOrdering.max(iterator)); assertFalse(iterator.hasNext()); }
@SuppressWarnings("unchecked") // generic arrays and unchecked cast void testMinAndMax() { List<T> shuffledList = Lists.newArrayList(strictlyOrderedList); shuffledList = shuffledCopy(shuffledList, new Random(5)); T min = strictlyOrderedList.get(0); T max = strictlyOrderedList.get(strictlyOrderedList.size() - 1); T first = shuffledList.get(0); T second = shuffledList.get(1); T third = shuffledList.get(2); T[] rest = shuffledList.subList(3, shuffledList.size()).toArray(emptyArray); assertEquals(min, ordering.min(shuffledList)); assertEquals(min, ordering.min(shuffledList.iterator())); assertEquals(min, ordering.min(first, second, third, rest)); assertEquals(min, ordering.min(min, max)); assertEquals(min, ordering.min(max, min)); assertEquals(max, ordering.max(shuffledList)); assertEquals(max, ordering.max(shuffledList.iterator())); assertEquals(max, ordering.max(first, second, third, rest)); assertEquals(max, ordering.max(min, max)); assertEquals(max, ordering.max(max, min)); }
public void testIteratorMinAndMax() { List<Integer> ints = Lists.newArrayList(5, 3, 0, 9); assertEquals(9, (int) numberOrdering.max(ints.iterator())); assertEquals(0, (int) numberOrdering.min(ints.iterator())); // when the values are the same, the first argument should be returned Integer a = new Integer(4); Integer b = new Integer(4); ints = Lists.newArrayList(a, b, b); assertSame(a, numberOrdering.max(ints.iterator())); assertSame(a, numberOrdering.min(ints.iterator())); }
public void testIterableMinAndMax() { List<Integer> ints = Lists.newArrayList(5, 3, 0, 9); assertEquals(9, (int) numberOrdering.max(ints)); assertEquals(0, (int) numberOrdering.min(ints)); // when the values are the same, the first argument should be returned Integer a = new Integer(4); Integer b = new Integer(4); ints = Lists.newArrayList(a, b, b); assertSame(a, numberOrdering.max(ints)); assertSame(a, numberOrdering.min(ints)); }
public void testLast() { for (List<Integer> contents : SAMPLE_INPUTS) { C filtered = filter(createUnfiltered(contents), EVEN); try { Integer first = filtered.last(); assertFalse(filtered.isEmpty()); assertEquals(Ordering.natural().max(filtered), first); } catch (NoSuchElementException e) { assertTrue(filtered.isEmpty()); } } }
public void testParameterMinAndMax() { assertEquals(5, (int) numberOrdering.max(3, 5)); assertEquals(5, (int) numberOrdering.max(5, 3)); assertEquals(3, (int) numberOrdering.min(3, 5)); assertEquals(3, (int) numberOrdering.min(5, 3)); // when the values are the same, the first argument should be returned Integer a = new Integer(4); Integer b = new Integer(4); assertSame(a, numberOrdering.max(a, b)); assertSame(a, numberOrdering.min(a, b)); }
@Override public Object getExpectedValue(int start, int length) { if (length == 0) { return null; } Slice max = null; for (int i = 0; i < length; i++) { Slice slice = Slices.wrappedBuffer(Ints.toByteArray(i)); max = (max == null) ? slice : Ordering.natural().max(max, slice); } return max.toStringUtf8(); }
public void testVarargsMinAndMax() { // try the min and max values in all positions, since some values are proper // parameters and others are from the varargs array assertEquals(9, (int) numberOrdering.max(9, 3, 0, 5, 8)); assertEquals(9, (int) numberOrdering.max(5, 9, 0, 3, 8)); assertEquals(9, (int) numberOrdering.max(5, 3, 9, 0, 8)); assertEquals(9, (int) numberOrdering.max(5, 3, 0, 9, 8)); assertEquals(9, (int) numberOrdering.max(5, 3, 0, 8, 9)); assertEquals(0, (int) numberOrdering.min(0, 3, 5, 9, 8)); assertEquals(0, (int) numberOrdering.min(5, 0, 3, 9, 8)); assertEquals(0, (int) numberOrdering.min(5, 3, 0, 9, 8)); assertEquals(0, (int) numberOrdering.min(5, 3, 9, 0, 8)); assertEquals(0, (int) numberOrdering.min(5, 3, 0, 9, 0)); // when the values are the same, the first argument should be returned Integer a = new Integer(4); Integer b = new Integer(4); assertSame(a, numberOrdering.max(a, b, b)); assertSame(a, numberOrdering.min(a, b, b)); }