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

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:org.jclouds.compute.domain.internal.TemplateBuilderImpl.java

protected Hardware resolveHardware(Set<? extends Hardware> hardwarel, final Iterable<? extends Image> images) {
    Ordering<Hardware> hardwareOrdering = hardwareSorter();

    Iterable<Predicate<Image>> supportsImagePredicates = Iterables.transform(hardwarel,
            new Function<Hardware, Predicate<Image>>() {

                @Override// w w w  .ja v  a 2s .  com
                public Predicate<Image> apply(Hardware input) {
                    return input.supportsImage();
                }

            });

    Predicate<Image> supportsImagePredicate = Iterables.size(supportsImagePredicates) == 1
            ? Iterables.getOnlyElement(supportsImagePredicates)
            : Predicates.<Image>or(supportsImagePredicates);

    if (!Iterables.any(images, supportsImagePredicate)) {
        String message = format("no hardware profiles support images matching params: %s",
                supportsImagePredicate);
        throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwarel);
    }

    Iterable<? extends Hardware> hardwareCompatibleWithOurImages = filter(hardwarel,
            supportsImagesPredicate(images));
    Predicate<Hardware> hardwarePredicate = buildHardwarePredicate();
    Hardware hardware;
    try {
        hardware = hardwareOrdering.max(filter(hardwareCompatibleWithOurImages, hardwarePredicate));
    } catch (NoSuchElementException exception) {
        String message = format("no hardware profiles match params: %s", hardwarePredicate);
        throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwareCompatibleWithOurImages);
    }
    logger.trace("<<   matched hardware(%s)", hardware.getId());
    return hardware;
}

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  ava2s.  co  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);
}