/** * Sort the given List with a default OrderComparator. * <p>Optimized to skip sorting for lists with size 0 or 1, * in order to avoid unnecessary array extraction. * @param list the List to sort * @see java.util.List#sort(java.util.Comparator) */ public static void sort(List<?> list) { if (list.size() > 1) { list.sort(INSTANCE); } }
/** * Obtain the closest match from the given exception types for the given target exception. * @param exceptionTypes the collection of exception types * @param targetException the target exception to find a match for * @return the closest matching exception type from the given collection */ public static Class<? extends Throwable> findClosestMatch( Collection<Class<? extends Throwable>> exceptionTypes, Throwable targetException) { Assert.notEmpty(exceptionTypes, "Exception types must not be empty"); if (exceptionTypes.size() == 1) { return exceptionTypes.iterator().next(); } List<Class<? extends Throwable>> handledExceptions = new ArrayList<>(exceptionTypes); handledExceptions.sort(new ExceptionDepthComparator(targetException)); return handledExceptions.get(0); }
/** * Return the {@link Method} mapped to the given exception type, or {@code null} if none. */ @Nullable private Method getMappedMethod(Class<? extends Throwable> exceptionType) { List<Class<? extends Throwable>> matches = new ArrayList<>(); for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) { if (mappedException.isAssignableFrom(exceptionType)) { matches.add(mappedException); } } if (!matches.isEmpty()) { matches.sort(new ExceptionDepthComparator(exceptionType)); return this.mappedMethods.get(matches.get(0)); } else { return null; } }
private static HandlerEnhancerDefinition[] flatten(List<HandlerEnhancerDefinition> factories) { List<HandlerEnhancerDefinition> flattened = new ArrayList<>(factories.size()); for (HandlerEnhancerDefinition handlerEnhancer : factories) { if (handlerEnhancer instanceof MultiHandlerEnhancerDefinition) { flattened.addAll(((MultiHandlerEnhancerDefinition) handlerEnhancer).getDelegates()); } else { flattened.add(handlerEnhancer); } } flattened.sort(PriorityAnnotationComparator.getInstance()); return flattened.toArray(new HandlerEnhancerDefinition[flattened.size()]); }
@SuppressWarnings("unchecked") public static List<?> sortStringAsInt(List<?> list, String field, String order) { if (list.size() == 0) { return list; } Comparator c = new GenericStringAsIntComparator(list.get(0).getClass(), field); list.sort("asc".equalsIgnoreCase(order) ? c : Collections.reverseOrder(c)); return list; }
private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescriptors) { List<Method> matches = new ArrayList<>(); for (MethodDescriptor methodDescriptor : methodDescriptors) { Method method = methodDescriptor.getMethod(); if (isCandidateWriteMethod(method)) { matches.add(method); } } // Sort non-void returning write methods to guard against the ill effects of // non-deterministic sorting of methods returned from Class#getDeclaredMethods // under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180 matches.sort((m1, m2) -> m2.toString().compareTo(m1.toString())); return matches; }
/** * Attaches a given {@code value} to the value list. If the list is not empty * the {@code value} is added, and sorted by the given {@link Comparator}. */ private void addValue(V value, @Nullable Comparator<V> comparator) { if (values == null) { values = new ArrayList<>(); } values.add(value); // Sort the values using the given comparator. if (comparator != null && values.size() > 1) { values.sort(comparator); } }
@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; }
for (String pattern : this.patterns) { if (pattern.equals(destination) || this.pathMatcher.match(pattern, destination)) { matches.add(pattern); if (matches.isEmpty()) { return null; matches.sort(this.pathMatcher.getPatternComparator(destination)); return new DestinationPatternsMessageCondition(matches, this.pathMatcher);
public List<JavaPackage> getPackages() { List<JavaClass> classList = getClasses(); if (classList.isEmpty()) { return Collections.emptyList(); } Map<String, List<JavaClass>> map = new HashMap<>(); for (JavaClass javaClass : classList) { String pkg = javaClass.getPackage(); List<JavaClass> clsList = map.computeIfAbsent(pkg, k -> new ArrayList<>()); clsList.add(javaClass); } List<JavaPackage> packages = new ArrayList<>(map.size()); for (Map.Entry<String, List<JavaClass>> entry : map.entrySet()) { packages.add(new JavaPackage(entry.getKey(), entry.getValue())); } Collections.sort(packages); for (JavaPackage pkg : packages) { pkg.getClasses().sort(Comparator.comparing(JavaClass::getName)); } return Collections.unmodifiableList(packages); }
private static final List<List<ArgumentSequence>> getFullConjunctArgumentsHelper(SemanticGraph sg, IndexedWord conjGov, IndexedWord orphanGov) { List<List<ArgumentSequence>> arguments = new LinkedList<>(); for (SemanticGraphEdge edge : sg.outgoingEdgeIterable(conjGov)) { if (isArgument(sg, edge) && edge.getDependent().pseudoPosition() < orphanGov.pseudoPosition()) { List<ArgumentSequence> argumentVariants = new LinkedList<>(); ArgumentSequence seq = new ArgumentSequence(edge.getDependent(), sg.yield(edge.getDependent())); argumentVariants.add(seq); getArgumentSubsequences(sg, edge.getDependent(), argumentVariants); arguments.add(argumentVariants); } } arguments.sort((arg1, arg2) -> arg1.get(0).head.index() - arg2.get(0).head.index()); return arguments; }
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)); }
@Override public List<ActivityImpl> getUserTasksByProcDefKey(String procDefKey) { ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(procDefKey).orderByProcessDefinitionVersion().desc().list().get(0); String procDefId = definition.getId(); List<ActivityImpl> activities = findActivities(procDefId, activity -> "userTask".equals(activity.getProperty("type"))); // if (null != activities) { activities.sort(Comparator.comparing(ProcessElementImpl::getId)); } return activities; }
/** * Collects all interceptors. */ protected void collectActionInterceptors() { final Collection<? extends ActionInterceptor> interceptorValues = interceptorsManager.getAllInterceptors(); interceptors = new ArrayList<>(); interceptors.addAll(interceptorValues); interceptors.sort(Comparator.comparing(a -> a.getClass().getSimpleName())); }
public void stop() { if (this.members.isEmpty()) { return; logger.debug("Stopping beans in phase " + this.phase); this.members.sort(Collections.reverseOrder()); CountDownLatch latch = new CountDownLatch(this.smartMemberCount); Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>());
/** * Return the {@link Method} mapped to the given exception type, or {@code null} if none. */ @Nullable private Method getMappedMethod(Class<? extends Throwable> exceptionType) { List<Class<? extends Throwable>> matches = new ArrayList<>(); for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) { if (mappedException.isAssignableFrom(exceptionType)) { matches.add(mappedException); } } if (!matches.isEmpty()) { matches.sort(new ExceptionDepthComparator(exceptionType)); return this.mappedMethods.get(matches.get(0)); } else { return null; } }
private static ParameterResolverFactory[] flatten(List<ParameterResolverFactory> factories) { List<ParameterResolverFactory> flattened = new ArrayList<>(factories.size()); for (ParameterResolverFactory parameterResolverFactory : factories) { if (parameterResolverFactory instanceof MultiParameterResolverFactory) { flattened.addAll(((MultiParameterResolverFactory) parameterResolverFactory).getDelegates()); } else { flattened.add(parameterResolverFactory); } } flattened.sort(PriorityAnnotationComparator.getInstance()); return flattened.toArray(new ParameterResolverFactory[0]); }
@SuppressWarnings("unchecked") public static List<?> sort(List<?> list, String field, String order) { if (list.size() == 0) { return list; } Comparator c = new GenericComparator(list.get(0).getClass(), field); list.sort("asc".equalsIgnoreCase(order) ? c : Collections.reverseOrder(c)); return list; }