List of usage examples for java.util Comparator compare
int compare(T o1, T o2);
From source file:pl.edu.icm.cermine.evaluation.tools.EvaluationUtils.java
public static Comparator<String> headerComparator(final Comparator<String> comp) { return new Comparator<String>() { @Override/*from www . j av a2 s . c o m*/ public int compare(String t1, String t2) { List<String> t1Lines = Lists.newArrayList(t1.split("\n")); List<String> t2Lines = Lists.newArrayList(t2.split("\n")); if (t1Lines.size() != t2Lines.size()) { return -1; } for (int i = 0; i < t1Lines.size(); i++) { if (t1Lines.get(i).equals(t2Lines.get(i))) { continue; } String trimmed1 = t1Lines.get(i).trim(); String trimmed2 = t2Lines.get(i).trim(); if (t1Lines.get(i).length() - trimmed1.length() != t2Lines.get(i).length() - trimmed2.length()) { return -1; } if (comp.compare(trimmed1, trimmed2) != 0) { return -1; } } return 0; } }; }
From source file:org.uma.jmetal.util.comparator.RankingAndCrowdingDistanceComparatorTest.java
@Test public void shouldCompareWithRankingYieldingANonZeroValueReturnThatValue() { Comparator<Solution<?>> rankComparator = mock(Comparator.class); when(rankComparator.compare(any(Solution.class), any(Solution.class))).thenReturn(1); ReflectionTestUtils.setField(comparator, "rankComparator", rankComparator); Solution<?> solution1 = mock(Solution.class); Solution<?> solution2 = mock(Solution.class); assertEquals(1, comparator.compare(solution1, solution2)); verify(rankComparator).compare(solution1, solution2); }
From source file:org.apache.james.mailbox.store.search.comparator.CombinedComparator.java
@Override public int compare(MailboxMessage o1, MailboxMessage o2) { int i = 0;/*from w w w. j av a 2 s.c o m*/ for (Comparator<MailboxMessage> comparator : comparators) { i = comparator.compare(o1, o2); if (i != 0) { break; } } return i; }
From source file:org.uma.jmetal.util.comparator.RankingAndCrowdingDistanceComparatorTest.java
@Test public void shouldCompareWhenRankingYieldingAZeroReturnTheCrowdingDistanceValue() { Comparator<Solution<?>> rankComparator = mock(Comparator.class); when(rankComparator.compare(any(Solution.class), any(Solution.class))).thenReturn(0); Comparator<Solution<?>> crowdingDistanceComparator = mock(CrowdingDistanceComparator.class); when(crowdingDistanceComparator.compare(any(Solution.class), any(Solution.class))).thenReturn(-1); ReflectionTestUtils.setField(comparator, "rankComparator", rankComparator); ReflectionTestUtils.setField(comparator, "crowdingDistanceComparator", crowdingDistanceComparator); Solution<?> solution1 = mock(Solution.class); Solution<?> solution2 = mock(Solution.class); assertEquals(-1, comparator.compare(solution1, solution2)); verify(rankComparator).compare(solution1, solution2); verify(crowdingDistanceComparator).compare(solution1, solution2); }
From source file:com.chinamobile.bcbsp.ml.math.DenseDoubleVector.java
/** * Some crazy sort function.//from w w w. j a va 2s. c om */ public static List<Tuple<Double, Integer>> sort(DoubleVector vector, final Comparator<Double> scoreComparator) { List<Tuple<Double, Integer>> list = new ArrayList<Tuple<Double, Integer>>(vector.getLength()); for (int i = 0; i < vector.getLength(); i++) { list.add(new Tuple<Double, Integer>(vector.get(i), i)); } Collections.sort(list, new Comparator<Tuple<Double, Integer>>() { @Override public int compare(Tuple<Double, Integer> o1, Tuple<Double, Integer> o2) { return scoreComparator.compare(o1.getFirst(), o2.getFirst()); } }); return list; }
From source file:org.candlepin.util.Util.java
/** * Compares two collections for equality without using the collection's equals method. This is * primarily only useful when working with collections that may actually be Hibernate bags, as * bags and proxies do not properly implement the equals method, which tends to lead to * incorrect results and unnecessary work. * <p></p>/*from w w w. j ava2s .c o m*/ * WARNING: This method will not work with collections which use iterators that return its * elements in an inconsistent order. The order does not need to be known, but it must be * consistent and repeatable for a given collection state. * * @param c1 * A collection to compare to c2 * * @param c2 * A collection to compare to c1 * * @param comp * A comparator to use to compare elements of c1 and c2 for equality * * @throws IllegalArgumentException * if the provided compator is null * * @return * true if both collections are the same instance, are both null or contain the same elements; * false otherwise */ public static <T> boolean collectionsAreEqual(Collection<T> c1, Collection<T> c2, Comparator<T> comp) { if (comp == null) { throw new IllegalArgumentException("comp is null"); } if (c1 == c2) { return true; } if (c1 == null || c2 == null || c1.size() != c2.size()) { return false; } Set<Integer> indexes = new HashSet<Integer>(); for (T lhs : c1) { boolean found = false; int offset = -1; for (T rhs : c2) { if (indexes.contains(++offset)) { continue; } if (lhs == rhs || (lhs != null && comp.compare(lhs, rhs) == 0)) { indexes.add(offset); found = true; break; } } if (!found) { return false; } } return true; }
From source file:org.apache.kylin.dict.lookup.LookupTable.java
private boolean between(T beginValue, T v, T endValue, Comparator<T> comp) { return (beginValue == null || comp.compare(beginValue, v) <= 0) && (endValue == null || comp.compare(v, endValue) <= 0); }
From source file:org.drugis.mtc.parameterization.InconsistencyParameter.java
/** * Compare two lists by pair-wise comparison of their elements. * All other things being equal, the shorter list is considered less than the longer list. * The lists are compared lexicographically, thus if l1[0] > l2[0], l1 > l2 etc. * @param <E> Element type./*from w w w . j a v a 2 s . com*/ * @param l1 First list. * @param l2 Second list. * @param cmp Element comparator. * @return An integer < 0 if l1 < l2, > 0 if l1 > l2 and == 0 if l1 == l2. */ private <E> int compare(List<? extends E> l1, List<? extends E> l2, Comparator<? super E> cmp) { int n = Math.min(l1.size(), l2.size()); for (int i = 0; i < n; ++i) { int c = cmp.compare(l1.get(i), l2.get(i)); if (c != 0) { return c; } } return l1.size() - l2.size(); }
From source file:org.jactr.core.utils.ChainedComparator.java
/** * Description of the Method// w ww. j a v a2 s . c o m * * @param one * Description of the Parameter * @param two * Description of the Parameter * @return Description of the Return Value */ public int compare(T one, T two) { if (one == two) return 0; for (Comparator<T> comparator : _comparators) { int rtnValue = comparator.compare(one, two); if (rtnValue != 0) return rtnValue; } if (_permitEqualities) return 0; /* * this is strange. If we're in the situation where all the comparators say * the two are equal, it is likely that their hashCodes are consistent such * that some relationship between the two hashcodes will be constant. So, we * change it up with an instance specific mask */ int h1 = one.hashCode() & _comparatorSeed; int h2 = two.hashCode() & _comparatorSeed; int rtn = Integer.compare(h1, h2); if (rtn == 0) { // one last attempt h1 *= hashCode(); h2 *= hashCode(); rtn = Integer.compare(h1, h2); if (rtn == 0) LOGGER.error(String.format("functional equality %s.%d = %s.%d ", one, one.hashCode(), two, two.hashCode())); } return rtn; }
From source file:com.redhat.rhn.domain.channel.ReleaseChannelMap.java
/** * compare to ReleaseChannelMap/*from ww w . j av a2 s .co m*/ * @param o the other object * @return the compare return */ public int compareTo(ReleaseChannelMap o) { List<Comparator> compar = new ArrayList<Comparator>(); compar.add(new DynamicComparator("channel", true)); compar.add(new DynamicComparator("channelArch", true)); compar.add(new DynamicComparator("product", true)); compar.add(new DynamicComparator("version", true)); compar.add(new DynamicComparator("release", true)); Comparator com = ComparatorUtils.chainedComparator((Comparator[]) compar.toArray()); return com.compare(this, o); }