List of usage examples for java.util Comparator compare
int compare(T o1, T o2);
From source file:at.gridtec.lambda4j.operator.binary.IntBinaryOperator2.java
/** * Returns a {@link IntBinaryOperator2} which returns the lesser of two elements according to the specified {@code * Comparator}.//from ww w. j av a2s . com * * @param comparator A {@code Comparator} for comparing the two values * @return A {@code IntBinaryOperator2} which returns the lesser of its operands, according to the supplied {@code * Comparator}. * @throws NullPointerException If given argument is {@code null} * @see BinaryOperator#minBy(Comparator) */ @Nonnull static IntBinaryOperator2 minBy(@Nonnull final Comparator<Integer> comparator) { Objects.requireNonNull(comparator); return (value1, value2) -> comparator.compare(value1, value2) <= 0 ? value1 : value2; }
From source file:at.gridtec.lambda4j.operator.binary.IntBinaryOperator2.java
/** * Returns a {@link IntBinaryOperator2} which returns the greater of two elements according to the specified {@code * Comparator}./*from w ww .java2 s .co m*/ * * @param comparator A {@code Comparator} for comparing the two values * @return A {@code IntBinaryOperator2} which returns the greater of its operands, according to the supplied {@code * Comparator}. * @throws NullPointerException If given argument is {@code null} * @see BinaryOperator#maxBy(Comparator) */ @Nonnull static IntBinaryOperator2 maxBy(@Nonnull final Comparator<Integer> comparator) { Objects.requireNonNull(comparator); return (value1, value2) -> comparator.compare(value1, value2) >= 0 ? value1 : value2; }
From source file:org.drugis.mtc.graph.AbsoluteOneCenter.java
/** * Sort the vertices of the graph according to non-increasing distance from a vertex v. * @param distance Distance function for the graph. * @param v The vertex./*from ww w.j a va 2s . c o m*/ * @param comparator Comparator to break ties between vertices of equal distance. * Used to make the results fully deterministic, can be null if this is not required. */ static <V> List<V> distanceOrderedVertices(final Distance<V> distance, final V v, final Comparator<V> comparator) { final Map<V, Number> map = distance.getDistanceMap(v); List<V> list = new ArrayList<V>(map.keySet()); Collections.sort(list, new Comparator<V>() { public int compare(V a, V b) { if (map.get(a).equals(map.get(b))) { return comparator == null ? 0 : comparator.compare(a, b); } else { return ((Double) map.get(b).doubleValue()).compareTo(map.get(a).doubleValue()); } } }); return list; }
From source file:mondrian.olap.fun.PartialSortTest.java
private static <T> boolean isPartiallySorted(T[] vec, int limit, Comparator<? super T> order, boolean descending, Comparator<? super T> tieBreaker) { // elements {0 <= i < limit} are sorted for (int i = 1; i < limit; i++) { int delta = order.compare(vec[i], vec[i - 1]); if (delta == 0) { if (tieBreaker != null && tieBreaker.compare(vec[i], vec[i - 1]) < 0) { return false; }//from w w w . j a va 2 s .co m } else if (descending) { if (delta > 0) { return false; } } else { if (delta < 0) { return false; } } } // elements {limit <= i} are just bigger or smaller than the // bound vec[limit - 1]; T bound = vec[limit - 1]; for (int i = limit; i < vec.length; i++) { int delta = order.compare(vec[i], bound); if (descending) { if (delta > 0) { return false; } } else { if (delta < 0) { return false; } } } return true; }
From source file:smodelkit.util.Helper.java
public static <T> int indexOfMaxElement(List<T> list, Comparator<T> comparator) { if (list.isEmpty()) { throw new IllegalArgumentException("There is no maximum element of an empty list."); }/*w w w . j a v a2 s.c o m*/ int maxIndex = 0; for (int i : new Range(1, list.size())) { if (comparator.compare(list.get(i), list.get(maxIndex)) > 0) { maxIndex = i; } } return maxIndex; }
From source file:net.sourceforge.fenixedu.presentationTier.validator.form.ValidateCompareTwoFields.java
/** * Compares the two fields using the given comparator * // w w w . j av a 2 s. c om * @param bean * @param va * @param field * @param errors * @param request * @param comparator * @return */ private static boolean validate(Object bean, ValidatorAction va, Field field, ActionMessages errors, HttpServletRequest request, Comparator comparator) { String greaterInputString = ValidatorUtils.getValueAsString(bean, field.getProperty()); String secondProperty = field.getVarValue("secondProperty"); String lowerInputString = ValidatorUtils.getValueAsString(bean, secondProperty); if (!GenericValidator.isBlankOrNull(lowerInputString) && !GenericValidator.isBlankOrNull(greaterInputString)) { try { Double lowerInput = new Double(lowerInputString); Double greaterInput = new Double(greaterInputString); // if comparator result != VALUE then the condition is false if (comparator.compare(lowerInput, greaterInput) != VALUE) { errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); return false; } return true; } catch (NumberFormatException e) { errors.add(field.getKey(), new ActionMessage(va.getMsg())); return false; } } return true; }
From source file:org.omnaest.utils.structure.collection.list.sorted.TreeList.java
/** * @param comparator/*from w w w . ja v a2 s . c o m*/ * @return */ private static <E> Comparator<AccessorReadable<E>> newComparatorForAccessor(final Comparator<E> comparator) { return new Comparator<AccessorReadable<E>>() { @Override public int compare(AccessorReadable<E> accessor1, AccessorReadable<E> accessor2) { return comparator.compare(accessor1.getElement(), accessor2.getElement()); } }; }
From source file:at.gridtec.lambda4j.operator.binary.ThrowableBooleanBinaryOperator.java
/** * Returns a {@link ThrowableBooleanBinaryOperator} which returns the lesser of two elements according to the * specified {@code Comparator}.//from ww w. j av a2 s . c o m * * @param <X> The type of the throwable to be thrown by this operator * @param comparator A {@code Comparator} for comparing the two values * @return A {@code ThrowableBooleanBinaryOperator} which returns the lesser of its operands, according to the * supplied {@code Comparator}. * @throws NullPointerException If given argument is {@code null} * @see BinaryOperator#minBy(Comparator) */ @Nonnull static <X extends Throwable> ThrowableBooleanBinaryOperator<X> minBy( @Nonnull final Comparator<Boolean> comparator) { Objects.requireNonNull(comparator); return (value1, value2) -> comparator.compare(value1, value2) <= 0 ? value1 : value2; }
From source file:at.gridtec.lambda4j.operator.binary.ThrowableBooleanBinaryOperator.java
/** * Returns a {@link ThrowableBooleanBinaryOperator} which returns the greater of two elements according to the * specified {@code Comparator}.//from w w w . jav a2 s.c o m * * @param <X> The type of the throwable to be thrown by this operator * @param comparator A {@code Comparator} for comparing the two values * @return A {@code ThrowableBooleanBinaryOperator} which returns the greater of its operands, according to the * supplied {@code Comparator}. * @throws NullPointerException If given argument is {@code null} * @see BinaryOperator#maxBy(Comparator) */ @Nonnull static <X extends Throwable> ThrowableBooleanBinaryOperator<X> maxBy( @Nonnull final Comparator<Boolean> comparator) { Objects.requireNonNull(comparator); return (value1, value2) -> comparator.compare(value1, value2) >= 0 ? value1 : value2; }
From source file:at.gridtec.lambda4j.operator.binary.ThrowableByteBinaryOperator.java
/** * Returns a {@link ThrowableByteBinaryOperator} which returns the lesser of two elements according to the specified * {@code Comparator}.//from w w w.j a va2 s . c o m * * @param <X> The type of the throwable to be thrown by this operator * @param comparator A {@code Comparator} for comparing the two values * @return A {@code ThrowableByteBinaryOperator} which returns the lesser of its operands, according to the supplied * {@code Comparator}. * @throws NullPointerException If given argument is {@code null} * @see BinaryOperator#minBy(Comparator) */ @Nonnull static <X extends Throwable> ThrowableByteBinaryOperator<X> minBy(@Nonnull final Comparator<Byte> comparator) { Objects.requireNonNull(comparator); return (value1, value2) -> comparator.compare(value1, value2) <= 0 ? value1 : value2; }