/** * Sorts the given list of {@code MediaType} objects by specificity as the * primary criteria and quality value the secondary. * @see MediaType#sortBySpecificity(List) * @see MediaType#sortByQualityValue(List) */ public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) { Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); if (mediaTypes.size() > 1) { mediaTypes.sort(MediaType.SPECIFICITY_COMPARATOR.thenComparing(MediaType.QUALITY_VALUE_COMPARATOR)); } }
@Override public int compareTo(TotalRankSolverRankingWeight other) { return Comparator .comparingInt(TotalRankSolverRankingWeight::getBetterCount) .thenComparingInt(TotalRankSolverRankingWeight::getEqualCount) .thenComparingInt(TotalRankSolverRankingWeight::getLowerCount) .thenComparing(TotalRankSolverRankingWeight::getSolverBenchmarkResult, totalScoreSolverRankingComparator) // Tie-breaker .compare(this, other); }
/** * Sorts the given list of {@code MediaType} objects by specificity as the * primary criteria and quality value the secondary. * @see MediaType#sortBySpecificity(List) * @see MediaType#sortByQualityValue(List) */ public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) { Assert.notNull(mediaTypes, "'mediaTypes' must not be null"); if (mediaTypes.size() > 1) { mediaTypes.sort(MediaType.SPECIFICITY_COMPARATOR.thenComparing(MediaType.QUALITY_VALUE_COMPARATOR)); } }
private ListDefinitionsWsResponse doHandle(Request request) { ListDefinitionsRequest wsRequest = toWsRequest(request); Optional<ComponentDto> component = loadComponent(wsRequest); Optional<String> qualifier = getQualifier(component); ListDefinitionsWsResponse.Builder wsResponse = ListDefinitionsWsResponse.newBuilder(); propertyDefinitions.getAll().stream() .filter(definition -> qualifier.map(s -> definition.qualifiers().contains(s)).orElseGet(definition::global)) .filter(definition -> wsRequest.getBranch() == null || SETTING_ON_BRANCHES.contains(definition.key())) .filter(definition -> settingsWsSupport.isVisible(definition.key(), definition, component)) .sorted(comparing(PropertyDefinition::category, String::compareToIgnoreCase) .thenComparingInt(PropertyDefinition::index) .thenComparing(PropertyDefinition::name, String::compareToIgnoreCase)) .forEach(definition -> addDefinition(definition, wsResponse)); return wsResponse.build(); }
@Nonnull private <T> List<Class<? extends T>> prioritize(Iterable<Class<? extends T>> iterable) { List<Class<? extends T>> serviceClasses = new ArrayList<>(); for (Class<? extends T> serviceClass : iterable) { serviceClasses.add(serviceClass); } Comparator<Class<? extends T>> c = reverseOrder(comparing(PluginFinder::priority)); c = c.thenComparing(Class::getName); serviceClasses.sort(c); return serviceClasses; }
@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); } }
@Override public int compare(SolverBenchmarkResult a, SolverBenchmarkResult b) { return Comparator .comparing(SolverBenchmarkResult::getFailureCount, Comparator.reverseOrder()) .thenComparing(SolverBenchmarkResult::getTotalScore, resilientScoreComparator) .thenComparing(worstScoreSolverRankingComparator) .compare(a, b); }
@Override public <U> Comparator<ENTITY> thenComparing( Function<? super ENTITY, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { return asCombined().thenComparing(keyExtractor, keyComparator); }
@Override public <U extends Comparable<? super U>> Comparator<ENTITY> thenComparing(Function<? super ENTITY, ? extends U> keyExtractor) { return asCombined().thenComparing(keyExtractor); }
@Override public Comparator<ENTITY> thenComparing(Comparator<? super ENTITY> other) { return asCombined().thenComparing(other); }
@Override public int compare(SubSingleBenchmarkResult a, SubSingleBenchmarkResult b) { return Comparator // Reverse, less is better (redundant: failed benchmarks don't get ranked at all) .comparing(SubSingleBenchmarkResult::hasAnyFailure, Comparator.reverseOrder()) .thenComparing(SubSingleBenchmarkResult::getRanking, Comparator.naturalOrder()) .compare(a, b); }
@Override public int compare(SubSingleBenchmarkResult a, SubSingleBenchmarkResult b) { return Comparator // Reverse, less is better (redundant: failed benchmarks don't get ranked at all) .comparing(SubSingleBenchmarkResult::hasAnyFailure, Comparator.reverseOrder()) .thenComparing(SubSingleBenchmarkResult::getScore, resilientScoreComparator) .compare(a, b); }
@Override public int compare(SingleBenchmarkResult a, SingleBenchmarkResult b) { return Comparator // Reverse, less is better (redundant: failed benchmarks don't get ranked at all) .comparing(SingleBenchmarkResult::hasAnyFailure, Comparator.reverseOrder()) .thenComparing(SingleBenchmarkResult::getTotalScore, resilientScoreComparator) .compare(a, b); }
@VisibleForTesting static String[] getFrequentLocations(final Stream<String> locations) { final Map<String, Long> locationCountMap = locations.collect( Collectors.groupingBy(location -> location, Collectors.counting()) ); final Comparator<Map.Entry<String, Long>> valueComparator = Map.Entry.comparingByValue(Comparator.reverseOrder()); final Comparator<Map.Entry<String, Long>> keyComparator = Map.Entry.comparingByKey(); return locationCountMap .entrySet().stream() .sorted(valueComparator.thenComparing(keyComparator)) .limit(3) .map(Map.Entry::getKey) .toArray(String[]::new); }
private List<Parameter> getParameters(final TestResult result) { final TreeSet<Parameter> parametersSet = new TreeSet<>( comparing(Parameter::getName, nullsFirst(naturalOrder())) .thenComparing(Parameter::getValue, nullsFirst(naturalOrder())) ); parametersSet.addAll(convert(result.getParameters(), this::convert)); return new ArrayList<>(parametersSet); }
private List<Parameter> getParameters(final TestCaseResult source) { final TreeSet<Parameter> parametersSet = new TreeSet<>( comparing(Parameter::getName, nullsFirst(naturalOrder())) .thenComparing(Parameter::getValue, nullsFirst(naturalOrder())) ); parametersSet.addAll(convert(source.getParameters(), this::hasArgumentType, this::convert)); return new ArrayList<>(parametersSet); }
private void parseSolutionsAndAddToResponse(List<List<Label.Transition>> solutions, PointList waypoints) { for (List<Label.Transition> solution : solutions) { final List<Trip.Leg> legs = tripFromLabel.getTrip(translation, graphExplorer, accessEgressWeighting, solution); final PathWrapper pathWrapper = tripFromLabel.createPathWrapper(translation, waypoints, legs); pathWrapper.setImpossible(solution.stream().anyMatch(t -> t.label.impossible)); pathWrapper.setTime((solution.get(solution.size()-1).label.currentTime - solution.get(0).label.currentTime)); response.add(pathWrapper); } Comparator<PathWrapper> c = Comparator.comparingInt(p -> (p.isImpossible() ? 1 : 0)); Comparator<PathWrapper> d = Comparator.comparingDouble(PathWrapper::getTime); response.getAll().sort(c.thenComparing(d)); }
/** Compares two {@link MethodParamModel}s based on name and annotations only. */ public static Comparator<MethodParamModel> shallowParamComparator() { return Comparator.nullsLast( Comparator.comparing(MethodParamModel::getName) .thenComparing( (a, b) -> (a.getTypeName() == null && a.getTypeName() == b.getTypeName()) || a.getTypeName().equals(b.getTypeName()) ? 0 : -1) .thenComparing((a, b) -> a.getAnnotations().equals(b.getAnnotations()) ? 0 : -1)); } }
private static ImmutableList<MethodTree> sortedByArity(Iterable<MethodTree> methodTrees) { return sortedCopyOf(comparingArity().thenComparing(comparingPositions()), methodTrees); }
@Test public void testChainedComparators() { Comparator<Dog> c = new PropertyComparator<>("lastName", false, true); Dog dog1 = new Dog(); dog1.setFirstName("macy"); dog1.setLastName("grayspots"); Dog dog2 = new Dog(); dog2.setFirstName("biscuit"); dog2.setLastName("grayspots"); assertTrue(c.compare(dog1, dog2) == 0); c = c.thenComparing(new PropertyComparator<>("firstName", false, true)); assertTrue(c.compare(dog1, dog2) > 0); dog2.setLastName("konikk dog"); assertTrue(c.compare(dog2, dog1) > 0); }