List of usage examples for java.util Comparator compare
int compare(T o1, T o2);
From source file:org.apache.kylin.dict.lookup.LookupTable.java
public Pair<T, T> mapRange(String col, T beginValue, T endValue, String returnCol) { int colIdx = tableDesc.findColumnByName(col).getZeroBasedIndex(); int returnIdx = tableDesc.findColumnByName(returnCol).getZeroBasedIndex(); Comparator<T> colComp = getComparator(colIdx); Comparator<T> returnComp = getComparator(returnIdx); T returnBegin = null;// ww w .jav a 2 s .co m T returnEnd = null; for (T[] row : data.values()) { if (between(beginValue, row[colIdx], endValue, colComp)) { T returnValue = row[returnIdx]; if (returnBegin == null || returnComp.compare(returnValue, returnBegin) < 0) { returnBegin = returnValue; } if (returnEnd == null || returnComp.compare(returnValue, returnEnd) > 0) { returnEnd = returnValue; } } } if (returnBegin == null && returnEnd == null) return null; else return Pair.newPair(returnBegin, returnEnd); }
From source file:ch.rasc.wampspring.method.DestinationPatternsMessageCondition.java
/** * Compare the two conditions based on the destination patterns they contain. Patterns * are compared one at a time, from top to bottom via * {@link org.springframework.util.PathMatcher#getPatternComparator(String)}. If all * compared patterns match equally, but one instance has more patterns, it is * considered a closer match./*from ww w .ja v a 2 s . c om*/ * <p> * It is assumed that both instances have been obtained via * {@link #getMatchingCondition(Message)} to ensure they contain only patterns that * match the request and are sorted with the best matches on top. */ @Override public int compareTo(DestinationPatternsMessageCondition other, Message<?> message) { WampMessage wampMessage = (WampMessage) message; String destination = wampMessage.getDestination(); Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(destination); Iterator<String> iterator = this.patterns.iterator(); Iterator<String> iteratorOther = other.patterns.iterator(); while (iterator.hasNext() && iteratorOther.hasNext()) { int result = patternComparator.compare(iterator.next(), iteratorOther.next()); if (result != 0) { return result; } } if (iterator.hasNext()) { return -1; } else if (iteratorOther.hasNext()) { return 1; } else { return 0; } }
From source file:cn.edu.zjnu.acm.judge.contest.ContestController.java
@SuppressWarnings("ValueOfIncrementOrDecrementUsed") private <T> void setIndexes(T[] standings, Comparator<T> c, ObjIntConsumer<T> consumer) { Objects.requireNonNull(standings, "standings"); Objects.requireNonNull(c, "c"); Objects.requireNonNull(consumer, "consumer"); int i = 0, len = standings.length, lastIndex = 0; for (T last = null, standing; i < len; last = standing) { standing = standings[i++];//w w w. ja va2 s.c o m if (c.compare(standing, last) != 0) { lastIndex = i; } consumer.accept(standing, lastIndex); } }
From source file:com.epam.catgenome.manager.protein.ProteinSequenceManager.java
private List<List<ImmutablePair<Gene, List<Sequence>>>> combineData(final Map<Gene, List<List<Sequence>>> data, final Comparator<Gene> comparator) { List<List<ImmutablePair<Gene, List<Sequence>>>> source = data.entrySet().stream() .sorted((e1, e2) -> comparator.compare(e1.getKey(), e2.getKey())).map(e -> e.getValue().stream() .map(s -> new ImmutablePair<>(e.getKey(), s)).collect(Collectors.toList())) .collect(Collectors.toList()); if (CollectionUtils.isEmpty(source)) { return Collections.emptyList(); }/*from w ww .ja va 2 s . co m*/ List<List<ImmutablePair<Gene, List<Sequence>>>> start = new ArrayList<>(); for (ImmutablePair<Gene, List<Sequence>> p : source.remove(0)) { List<ImmutablePair<Gene, List<Sequence>>> ll = new ArrayList<>(); ll.add(p); start.add(ll); } return recursiveCombine(start, source); }
From source file:UnifyHash.java
public Object unify(Object o, int hash, Comparator comparator) { ///#ifdef JDK12 cleanUp();//from www.j a va 2 s. c om ///#endif int slot = Math.abs(hash % buckets.length); for (Bucket b = buckets[slot]; b != null; b = b.next) { Object old = b.get(); if (old != null && comparator.compare(o, old) == 0) return old; } put(hash, o); return o; }
From source file:io.github.karols.hocr4j.Paragraph.java
/** * Finds a line that satisfies given predicate and according to the given comparator is the "largest". * If not found, returns <code>null</code>. * * @param comparatorForMaximizing comparator to choose the "largest" line * @param predicate predicate the found line has to satisfy * @return a line that satisfies the predicate, or <code>null</code> if there are none */// www.j a v a 2s. c o m @Nullable public Line findLine(@Nonnull Comparator<Line> comparatorForMaximizing, @Nonnull Predicate<Line> predicate) { Line result = null; for (Line l : lines) { if (predicate.apply(l)) { if (result == null || comparatorForMaximizing.compare(l, result) > 0) { result = l; } } } return result; }
From source file:com.lloydtorres.stately.census.CensusRecyclerAdapter.java
/** * Comparators for CensusDetailedRank./* w w w . j av a2 s. c om*/ */ private Comparator<CensusDetailedRank> sortDescending(final Comparator<CensusDetailedRank> other) { return new Comparator<CensusDetailedRank>() { public int compare(CensusDetailedRank lhs, CensusDetailedRank rhs) { return -1 * other.compare(lhs, rhs); } }; }
From source file:com.facebook.presto.accumulo.tools.TimestampCheckTask.java
private void getDataCount(Connector connector, AccumuloTable table, AccumuloColumnHandle column, long timestamp) throws Exception { LOG.info("Getting data count"); BatchScanner scanner = connector.createBatchScanner(table.getFullTableName(), auths, 10); scanner.setRanges(connector.tableOperations().splitRangeByTablets(table.getFullTableName(), new Range(), Integer.MAX_VALUE));/*www . j a v a2 s .co m*/ scanner.fetchColumn(new Text(column.getFamily().get()), new Text(column.getQualifier().get())); IteratorSetting iteratorSetting = new IteratorSetting(Integer.MAX_VALUE, TimestampFilter.class); TimestampFilter.setEnd(iteratorSetting, timestamp, true); scanner.addScanIterator(iteratorSetting); Comparator<byte[]> comparator = UnsignedBytes.lexicographicalComparator(); long numRows = 0; for (Entry<Key, Value> entry : scanner) { byte[] timestampValue = entry.getValue().get(); if (comparator.compare(timestampValue, startBytes) >= 0 && comparator.compare(timestampValue, endBytes) <= 0) { ++numRows; } } scanner.close(); LOG.info("Number of rows from data table is " + numRows); }
From source file:io.github.karols.hocr4j.Area.java
/** * Finds a line that satisfies given predicate and according to the given comparator is the "largest". * If not found, returns <code>null</code>. * @param comparatorForMaximizing comparator to choose the "largest" line * @param predicate predicate the found line has to satisfy * @return a line that satisfies the predicate, or <code>null</code> if there are none *///from ww w . j a v a 2s. c om @Nullable public Line findLine(@Nonnull Comparator<Line> comparatorForMaximizing, @Nonnull Predicate<Line> predicate) { Line result = null; for (Paragraph p : paragraphs) { Line l = p.findLine(comparatorForMaximizing, predicate); if (l != null) { if (result == null || comparatorForMaximizing.compare(l, result) > 0) { result = l; } } } return result; }
From source file:de.johni0702.minecraft.gui.container.AbstractGuiContainer.java
@Override public T sortElements(final Comparator<GuiElement> comparator) { Ordering<Map.Entry<GuiElement, LayoutData>> ordering = new Ordering<Map.Entry<GuiElement, LayoutData>>() { @Override//w w w . ja va2s. c o m public int compare(Map.Entry<GuiElement, LayoutData> left, Map.Entry<GuiElement, LayoutData> right) { return comparator.compare(left.getKey(), right.getKey()); } }; if (!ordering.isOrdered(elements.entrySet())) { ImmutableList<Map.Entry<GuiElement, LayoutData>> sorted = ordering .immutableSortedCopy(elements.entrySet()); elements.clear(); for (Map.Entry<GuiElement, LayoutData> entry : sorted) { elements.put(entry.getKey(), entry.getValue()); } } return getThis(); }