Java Code Examples for com.google.common.collect.Ordering#max()

The following examples show how to use com.google.common.collect.Ordering#max() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReplayPosition.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Convenience method to compute the replay position for a group of SSTables.
 * @param sstables
 * @return the most recent (highest) replay position
 */
public static ReplayPosition getReplayPosition(Iterable<? extends SSTableReader> sstables)
{
    if (Iterables.isEmpty(sstables))
        return NONE;

    Function<SSTableReader, ReplayPosition> f = new Function<SSTableReader, ReplayPosition>()
    {
        public ReplayPosition apply(SSTableReader sstable)
        {
            return sstable.getReplayPosition();
        }
    };
    Ordering<ReplayPosition> ordering = Ordering.from(ReplayPosition.comparator);
    return ordering.max(Iterables.transform(sstables, f));
}
 
Example 2
Source File: LocationBoundingVisitor.java    From closure-stylesheets with Apache License 2.0 6 votes vote down vote up
@Override
public void enter(CssNode n) {
  SourceCodeLocation loc = n.getSourceCodeLocation();
  if (loc == null || loc.isUnknown()) {
    return;
  }
  if (result == null || result.isUnknown()) {
    result = loc;
  } else {
    Ordering<SourceCodePoint> o = Ordering.natural();
    SourceCodePoint lowerBound = o.min(result.getBegin(), loc.getBegin());
    SourceCodePoint upperBound = o.max(result.getEnd(), loc.getEnd());
    result = new SourceCodeLocationBuilder()
        .setSourceCode(result.getSourceCode())
        .setBeginLocation(lowerBound)
        .setEndLocation(upperBound)
        .getSourceCodeLocation();
  }
}
 
Example 3
Source File: CommitLogReplayer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public CommitLogReplayer()
{
    this.keyspacesRecovered = new NonBlockingHashSet<Keyspace>();
    this.futures = new ArrayList<Future<?>>();
    this.buffer = new byte[4096];
    this.invalidMutations = new HashMap<UUID, AtomicInteger>();
    // count the number of replayed mutation. We don't really care about atomicity, but we need it to be a reference.
    this.replayedCount = new AtomicInteger();
    this.checksum = new PureJavaCrc32();

    // compute per-CF and global replay positions
    cfPositions = new HashMap<UUID, ReplayPosition>();
    Ordering<ReplayPosition> replayPositionOrdering = Ordering.from(ReplayPosition.comparator);
    for (ColumnFamilyStore cfs : ColumnFamilyStore.all())
    {
        // it's important to call RP.gRP per-cf, before aggregating all the positions w/ the Ordering.min call
        // below: gRP will return NONE if there are no flushed sstables, which is important to have in the
        // list (otherwise we'll just start replay from the first flush position that we do have, which is not correct).
        ReplayPosition rp = ReplayPosition.getReplayPosition(cfs.getSSTables());

        // but, if we've truncted the cf in question, then we need to need to start replay after the truncation
        ReplayPosition truncatedAt = SystemKeyspace.getTruncatedPosition(cfs.metadata.cfId);
        if (truncatedAt != null)
            rp = replayPositionOrdering.max(Arrays.asList(rp, truncatedAt));

        cfPositions.put(cfs.metadata.cfId, rp);
    }
    globalPosition = replayPositionOrdering.min(cfPositions.values());
    logger.debug("Global replay position is {} from columnfamilies {}", globalPosition, FBUtilities.toString(cfPositions));
}
 
Example 4
Source File: MethodLibrary.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** Returns the maximum element from this list, as determined by maxOrdering. */
private static Object findExtreme(Sequence<?> args, Ordering<Object> maxOrdering)
    throws EvalException {
  // Args can either be a list of items to compare, or a singleton list whose element is an
  // iterable of items to compare. In either case, there must be at least one item to compare.
  try {
    Iterable<?> items = (args.size() == 1) ? Starlark.toIterable(args.get(0)) : args;
    return maxOrdering.max(items);
  } catch (NoSuchElementException ex) {
    throw new EvalException(null, "expected at least one item", ex);
  }
}
 
Example 5
Source File: Classification.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public <K, V extends Number> Map.Entry<K, Value<V>> getWinner(Type type, Collection<Map.Entry<K, Value<V>>> entries){
	Ordering<Map.Entry<K, Value<V>>> ordering = Classification.<K, V>createOrdering(type);

	try {
		return ordering.max(entries);
	} catch(NoSuchElementException nsee){
		return null;
	}
}
 
Example 6
Source File: OrderingExample.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void order_by_object_field () {
	
	List<GlassWare> beerGlasses = Lists.newArrayList(
			new GlassWare("Flute Glass", "Enhances and showcases..."),
			new GlassWare("Pilsner Glass (or Pokal)", "showcases color, ..."),
			new GlassWare("Pint Glass", "cheap to make..."),
			new GlassWare("Goblet (or Chalice)", "Eye candy..."),
			new GlassWare("Mug (or Seidel, Stein)", "Easy to drink..."),
			new GlassWare(null, null)
		);
	
	Ordering<GlassWare> byGlassWareName = Ordering.natural().nullsFirst()
			.onResultOf(new Function<GlassWare, String>() {
				public String apply(GlassWare glassWare) {
					return glassWare.getName();
				}
			});
	
	GlassWare firstBeerGlass = byGlassWareName.min(beerGlasses);
	
	// first element will be null
	assertNull(firstBeerGlass.getName());

	GlassWare lastBeerGlass = byGlassWareName.max(beerGlasses);
	assertEquals("Pint Glass", lastBeerGlass.getName());
}