Example usage for com.google.common.collect Ordering min

List of usage examples for com.google.common.collect Ordering min

Introduction

In this page you can find the example usage for com.google.common.collect Ordering min.

Prototype

public <E extends T> E min(Iterable<E> iterable) 

Source Link

Document

Returns the least of the specified values according to this ordering.

Usage

From source file:org.metaservice.kryo.QueueContainer.java

private <T extends AbstractMessage> void clean(Collection<Queue<T>> queues, JacksonDBCollection collection) {
    if (queues.size() > 0) {
        Ordering<ObjectId> objectIdOrdering = new Ordering<ObjectId>() {
            @Override// w w w.  j a  v a2  s.  co  m
            public int compare(ObjectId left, ObjectId right) {
                if (left != null)
                    return left.compareTo(right);
                else
                    return -1;
            }
        };
        ArrayList<ObjectId> list = new ArrayList<>();
        for (Queue queue : queues) {
            list.add(queue.getLast());
        }
        ObjectId maxPostProcess = objectIdOrdering.min(list);
        collection.remove(DBQuery.lessThanEquals("_id", maxPostProcess));
    }
}

From source file:org.apache.cassandra.db.commitlog.FlashBulkReplayer.java

public FlashBulkReplayer() {
    this.keyspacesRecovered = new NonBlockingHashSet<Keyspace>();
    this.futures = new ArrayList<Future<?>>();
    buffer = ByteBuffer.allocate(FlashSegmentManager.BLOCKS_IN_SEG * 4096);
    this.invalidMutations = new HashMap<UUID, AtomicInteger>();
    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);
    }//from w w  w.j ava 2 s .  com
    globalPosition = replayPositionOrdering.min(cfPositions.values());
    logger.debug("Global replay position is {} from columnfamilies {}" + globalPosition + "--- "
            + FBUtilities.toString(cfPositions));

    // allocate reader blocks
    readerBuffer = ByteBuffer.allocateDirect((int) (BULK_BLOCKS_TO_READ * 1024 * 4));
}

From source file:org.apache.cassandra.db.commitlog.CommitLogReplayer.java

public static CommitLogReplayer construct(CommitLog commitLog) {
    // compute per-CF and global replay positions
    Map<UUID, ReplayPosition> cfPositions = new HashMap<UUID, ReplayPosition>();
    Ordering<ReplayPosition> replayPositionOrdering = Ordering.from(ReplayPosition.comparator);
    ReplayFilter replayFilter = ReplayFilter.create();
    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 truncated 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) {
            // Point in time restore is taken to mean that the tables need to be recovered even if they were
            // deleted at a later point in time. Any truncation record after that point must thus be cleared prior
            // to recovery (CASSANDRA-9195).
            long restoreTime = commitLog.archiver.restorePointInTime;
            long truncatedTime = SystemKeyspace.getTruncatedAt(cfs.metadata.cfId);
            if (truncatedTime > restoreTime) {
                if (replayFilter.includes(cfs.metadata)) {
                    logger.info(//from w w w  . j  av a 2 s  .  co m
                            "Restore point in time is before latest truncation of table {}.{}. Clearing truncation record.",
                            cfs.metadata.ksName, cfs.metadata.cfName);
                    SystemKeyspace.removeTruncationRecord(cfs.metadata.cfId);
                }
            } else {
                rp = replayPositionOrdering.max(Arrays.asList(rp, truncatedAt));
            }
        }

        cfPositions.put(cfs.metadata.cfId, rp);
    }
    ReplayPosition globalPosition = replayPositionOrdering.min(cfPositions.values());
    logger.trace("Global replay position is {} from columnfamilies {}", globalPosition,
            FBUtilities.toString(cfPositions));
    return new CommitLogReplayer(commitLog, globalPosition, cfPositions, replayFilter);
}

From source file:org.jpmml.evaluator.general_regression.GeneralRegressionModelEvaluator.java

private Map<FieldName, ? extends Double> evaluateCoxRegression(ModelEvaluationContext context) {
    GeneralRegressionModel generalRegressionModel = getModel();

    BaseCumHazardTables baseCumHazardTables = generalRegressionModel.getBaseCumHazardTables();
    if (baseCumHazardTables == null) {
        throw new InvalidFeatureException(generalRegressionModel);
    }//from   w ww . j a va2  s .c o  m

    List<BaselineCell> baselineCells;

    Double maxTime;

    FieldName baselineStrataVariable = generalRegressionModel.getBaselineStrataVariable();

    if (baselineStrataVariable != null) {
        FieldValue value = getVariable(baselineStrataVariable, context);

        BaselineStratum baselineStratum = getBaselineStratum(baseCumHazardTables, value);

        // "If the value does not have a corresponding BaselineStratum element, then the result is a missing value"
        if (baselineStratum == null) {
            return null;
        }

        baselineCells = baselineStratum.getBaselineCells();

        maxTime = baselineStratum.getMaxTime();
    } else

    {
        baselineCells = baseCumHazardTables.getBaselineCells();

        maxTime = baseCumHazardTables.getMaxTime();
        if (maxTime == null) {
            throw new InvalidFeatureException(baseCumHazardTables);
        }
    }

    Comparator<BaselineCell> comparator = new Comparator<BaselineCell>() {

        @Override
        public int compare(BaselineCell left, BaselineCell right) {
            return Double.compare(left.getTime(), right.getTime());
        }
    };

    Ordering<BaselineCell> ordering = Ordering.from(comparator);

    double baselineCumHazard;

    FieldName startTimeVariable = generalRegressionModel.getStartTimeVariable();
    FieldName endTimeVariable = generalRegressionModel.getEndTimeVariable();

    if (endTimeVariable != null) {
        BaselineCell minBaselineCell = ordering.min(baselineCells);

        Double minTime = minBaselineCell.getTime();

        final FieldValue value = getVariable(endTimeVariable, context);

        FieldValue minTimeValue = FieldValueUtil.create(DataType.DOUBLE, OpType.CONTINUOUS, minTime);

        // "If the value is less than the minimum time, then cumulative hazard is 0 and predicted survival is 1"
        if (value.compareToValue(minTimeValue) < 0) {
            return Collections.singletonMap(getTargetFieldName(), Values.DOUBLE_ZERO);
        }

        FieldValue maxTimeValue = FieldValueUtil.create(DataType.DOUBLE, OpType.CONTINUOUS, maxTime);

        // "If the value is greater than the maximum time, then the result is a missing value"
        if (value.compareToValue(maxTimeValue) > 0) {
            return null;
        }

        Predicate<BaselineCell> predicate = new Predicate<BaselineCell>() {

            private double time = (value.asNumber()).doubleValue();

            @Override
            public boolean apply(BaselineCell baselineCell) {
                return (baselineCell.getTime() <= this.time);
            }
        };

        // "Select the BaselineCell element that has the largest time attribute value that is not greater than the value"
        BaselineCell baselineCell = ordering.max(Iterables.filter(baselineCells, predicate));

        baselineCumHazard = baselineCell.getCumHazard();
    } else

    {
        throw new InvalidFeatureException(generalRegressionModel);
    }

    Double r = computeDotProduct(context);

    Double s = computeReferencePoint();

    if (r == null || s == null) {
        return null;
    }

    Double cumHazard = baselineCumHazard * Math.exp(r - s);

    return Collections.singletonMap(getTargetFieldName(), cumHazard);
}

From source file:msi.gama.metamodel.topology.grid.GamaSpatialMatrix.java

@Override
public IAgent firstAtDistance(final IScope scope, final IShape source, final double dist,
        final IAgentFilter f) {
    final double exp = dist * Maths.SQRT2;
    final Envelope3D env = new Envelope3D(source.getEnvelope());
    env.expandBy(exp);/*from  w  w w. j  a  v a  2  s . c o m*/
    final Ordering<IShape> ordering = Ordering.natural().onResultOf(input -> source.euclidianDistanceTo(input));
    final Set<IAgent> shapes = allInEnvelope(scope, source, env, f, false);
    if (shapes.isEmpty()) {
        return null;
    }
    return ordering.min(shapes);
}