@Override public Comparator<Integer> getComparator() { return Comparator.naturalOrder(); }
@Override public Comparator<Double> getComparator() { return Comparator.naturalOrder(); }
@Override public Comparator<Integer> getComparator() { return Comparator.naturalOrder(); } }
@Override public Comparator<String> getComparator() { return Comparator.nullsFirst(Comparator.naturalOrder()); }
@Override public int compare(EndpointLogLine endpointA, EndpointLogLine endpointB) { return Comparator.<EndpointLogLine, String>comparing(endpoint -> endpoint.basePath) .thenComparing(endpoint -> endpoint.httpMethod, Comparator.nullsLast(Comparator.naturalOrder())) .compare(endpointA, endpointB); } }
/** * Converts this to a {@link Map}. * * @param f A function that maps an element to a key/value pair represented by Tuple2 * @param <K> The key type * @param <V> The value type * @return A new {@link TreeMap}. */ default <K extends Comparable<? super K>, V> SortedMap<K, V> toSortedMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) { Objects.requireNonNull(f, "f is null"); return toSortedMap(Comparator.naturalOrder(), f); }
private static List<Long> selectVersions(Connection connection) throws SQLException { try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select version from " + SCHEMA_MIGRATIONS_TABLE)) { List<Long> res = new ArrayList<>(); while (resultSet.next()) { res.add(resultSet.getLong(1)); } return res.stream() .sorted(Comparator.naturalOrder()) .collect(toList()); } } }
private <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { return Comparator.comparing(keyExtractor, nullsLast(naturalOrder())); }
public MergingPageIterator( Collection<Iterator<Page>> iterators, List<Type> types, List<Integer> sortFields, List<SortOrder> sortOrders) { requireNonNull(sortFields, "sortFields is null"); requireNonNull(sortOrders, "sortOrders is null"); checkArgument(sortFields.size() == sortOrders.size(), "sortFields and sortOrders size must match"); this.types = ImmutableList.copyOf(requireNonNull(types, "types is null")); this.sortFields = ImmutableList.copyOf(sortFields); this.sortOrders = ImmutableList.copyOf(sortOrders); this.pageBuilder = new PageBuilder(types); this.pagePositions = mergeSorted( iterators.stream() .map(pages -> concat(transform(pages, PagePositionIterator::new))) .collect(toList()), naturalOrder()); }
private static String formatLockString(Set<GuardedByExpression> locks) { ImmutableList<String> sortedUnhandled = FluentIterable.from(locks) .transform(Functions.toStringFunction()) .toSortedList(Comparator.naturalOrder()); return Joiner.on(", ").join(sortedUnhandled); }
@Override public Optional<ReadError> read(DbFileSources.Line.Builder lineBuilder) { Predicate<Map.Entry<TextBlock, Integer>> containsLine = new TextBlockContainsLine(lineBuilder.getLine()); // list is sorted to cope with the non-guaranteed order of Map entries which would trigger false detection of changes // in {@link DbFileSources.Line#getDuplicationList()} duplicatedTextBlockIndexByTextBlock.entrySet().stream() .filter(containsLine) .map(Map.Entry::getValue) .sorted(Comparator.naturalOrder()) .forEach(lineBuilder::addDuplication); return Optional.empty(); }
Comparator<? super E> comparator = spliterator.getComparator(); if (comparator == null) { comparator = (Comparator) Comparator.naturalOrder();
public void testEmptiesFirst() { Optional<String> empty = Optional.empty(); Optional<String> abc = Optional.of("abc"); Optional<String> z = Optional.of("z"); Comparator<Optional<String>> comparator = Comparators.emptiesFirst(comparing(String::length)); Helpers.testComparator(comparator, empty, z, abc); // Just demonstrate that no explicit type parameter is required comparator = Comparators.emptiesFirst(naturalOrder()); }
public void testEmptiesLast() { Optional<String> empty = Optional.empty(); Optional<String> abc = Optional.of("abc"); Optional<String> z = Optional.of("z"); Comparator<Optional<String>> comparator = Comparators.emptiesLast(comparing(String::length)); Helpers.testComparator(comparator, z, abc, empty); // Just demonstrate that no explicit type parameter is required comparator = Comparators.emptiesLast(naturalOrder()); } }
@Test public void testStringArrayToResourceArray() { conversionService.addConverter(new MyStringArrayToResourceArrayConverter()); Resource[] converted = conversionService.convert(new String[] { "x1", "z3" }, Resource[].class); List<String> descriptions = Arrays.stream(converted).map(Resource::getDescription).sorted(naturalOrder()).collect(toList()); assertEquals(Arrays.asList("1", "3"), descriptions); }
public void testGreatestCollector() { CollectorTester.of(Comparators.<Integer>greatest(2, Comparator.naturalOrder())) .expectCollects(Arrays.asList(6, 5), 1, 2, 3, 4, 5, 6) .expectCollects(Arrays.asList(1), 1) .expectCollects(Collections.emptyList()); }
public void testLeastCollector() { CollectorTester.of(Comparators.<Integer>least(2, Comparator.naturalOrder())) .expectCollects(Arrays.asList(1, 2), 1, 2, 3, 4, 5, 6) .expectCollects(Arrays.asList(1), 1) .expectCollects(Collections.emptyList()); }
@Test(dataProvider = "snapshot") public void snapshot(boolean ascending, int limit, long nanos, Function<Long, Long> transformer) { int count = 21; timerWheel.nanos = nanos; int expected = Math.min(limit, count); Comparator<Long> order = ascending ? Comparator.naturalOrder() : Comparator.reverseOrder(); List<Long> times = IntStream.range(0, count).mapToLong(i -> { long time = nanos + TimeUnit.SECONDS.toNanos(2 << i); timerWheel.schedule(new Timer(time)); return time; }).boxed().sorted(order).collect(toList()).subList(0, expected); when(transformer.apply(anyLong())).thenAnswer(invocation -> invocation.getArgument(0)); assertThat(snapshot(ascending, limit, transformer), is(times)); verify(transformer, times(expected)).apply(anyLong()); }
@Override public int compare(Operator<?> o1, Operator<?> o2) { Long id1 = Long.valueOf(o1.getIdentifier()); Long id2 = Long.valueOf(o2.getIdentifier()); int c0 = Objects.compare(o1.getOperatorName(), o2.getOperatorName(), Comparator.naturalOrder()); if (c0 != 0) { return c0; } return Long.compare(id1, id2); } };
public void testToImmutableSortedMapMerging() { Collector<Entry<String, Integer>, ?, ImmutableSortedMap<String, Integer>> collector = ImmutableSortedMap.toImmutableSortedMap( Comparator.naturalOrder(), Entry::getKey, Entry::getValue, Integer::sum); Equivalence<ImmutableMap<String, Integer>> equivalence = Equivalence.equals() .<Entry<String, Integer>>pairwise() .onResultOf(ImmutableMap::entrySet); CollectorTester.of(collector, equivalence) .expectCollects( ImmutableSortedMap.of("one", 1, "three", 3, "two", 4), mapEntry("one", 1), mapEntry("two", 2), mapEntry("three", 3), mapEntry("two", 2)); } }