Example usage for java.util Comparator comparingDouble

List of usage examples for java.util Comparator comparingDouble

Introduction

In this page you can find the example usage for java.util Comparator comparingDouble.

Prototype

public static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) 

Source Link

Document

Accepts a function that extracts a double sort key from a type T , and returns a Comparator that compares by that sort key.

Usage

From source file:io.pravega.controller.store.stream.tables.TableHelper.java

/**
 * Helper method to compute list of continuous ranges. For example, two neighbouring key ranges where,
 * range1.high == range2.low then they are considered neighbours.
 * This method reduces input range into distinct continuous blocks.
 * @param input list of key ranges.//from  w ww  . jav a2  s.co m
 * @return reduced list of key ranges.
 */
private static List<AbstractMap.SimpleEntry<Double, Double>> reduce(
        List<AbstractMap.SimpleEntry<Double, Double>> input) {
    List<AbstractMap.SimpleEntry<Double, Double>> ranges = new ArrayList<>(input);
    ranges.sort(Comparator.comparingDouble(AbstractMap.SimpleEntry::getKey));
    List<AbstractMap.SimpleEntry<Double, Double>> result = new ArrayList<>();
    double low = -1.0;
    double high = -1.0;
    for (AbstractMap.SimpleEntry<Double, Double> range : ranges) {
        if (high < range.getKey()) {
            // add previous result and start a new result if prev.high is less than next.low
            if (low != -1.0 && high != -1.0) {
                result.add(new AbstractMap.SimpleEntry<>(low, high));
            }
            low = range.getKey();
            high = range.getValue();
        } else if (high == range.getKey()) {
            // if adjacent (prev.high == next.low) then update only high
            high = range.getValue();
        } else {
            // if prev.high > next.low.
            // [Note: next.low cannot be less than 0] which means prev.high > 0
            assert low >= 0;
            assert high > 0;
            result.add(new AbstractMap.SimpleEntry<>(low, high));
            low = range.getKey();
            high = range.getValue();
        }
    }
    // add the last range
    if (low != -1.0 && high != -1.0) {
        result.add(new AbstractMap.SimpleEntry<>(low, high));
    }
    return result;
}

From source file:org.apache.druid.server.coordinator.CostBalancerStrategy.java

@Override
public Iterator<ServerHolder> pickServersToDrop(DataSegment toDrop, NavigableSet<ServerHolder> serverHolders) {
    List<ListenableFuture<Pair<Double, ServerHolder>>> futures = Lists.newArrayList();

    for (final ServerHolder server : serverHolders) {
        futures.add(exec.submit(() -> Pair.of(computeCost(toDrop, server, true), server)));
    }//from   www  .j av a2  s  . c om

    final ListenableFuture<List<Pair<Double, ServerHolder>>> resultsFuture = Futures.allAsList(futures);

    try {
        // results is an un-ordered list of a pair consisting of the 'cost' of a segment being on a server and the server
        List<Pair<Double, ServerHolder>> results = resultsFuture.get();
        return results.stream()
                // Comparator.comapringDouble will order by lowest cost...
                // reverse it because we want to drop from the highest cost servers first
                .sorted(Comparator.comparingDouble((Pair<Double, ServerHolder> o) -> o.lhs).reversed())
                .map(x -> x.rhs).collect(Collectors.toList()).iterator();
    } catch (Exception e) {
        log.makeAlert(e, "Cost Balancer Multithread strategy wasn't able to complete cost computation.").emit();
    }
    return Collections.emptyIterator();
}

From source file:org.cloudsimplus.migration.VmMigrationWhenCpuMetricIsViolatedExampleWithSelectedVm.java

/**
 * Selects a VM to run a Cloudlet that will minimize the Cloudlet response
 * time./*from ww  w  .ja va2  s .c  o m*/
 *
 * @param cloudlet the Cloudlet to select a VM to
 * @return the selected Vm
 */
private Vm selectVmForCloudlet(Cloudlet cloudlet) {
    List<Vm> createdVms = cloudlet.getBroker().getVmsCreatedList();
    Log.printLine("\t\tCreated VMs: " + createdVms);
    Comparator<Vm> sortByNumberOfFreePes = Comparator.comparingLong(vm -> getExpectedNumberOfFreeVmPes(vm));
    Comparator<Vm> sortByExpectedCloudletResponseTime = Comparator
            .comparingDouble(vm -> getExpectedCloudletResponseTime(cloudlet, vm));
    createdVms.sort(sortByNumberOfFreePes.thenComparing(sortByExpectedCloudletResponseTime).reversed());
    Vm mostFreePesVm = createdVms.stream().findFirst().orElse(Vm.NULL);

    Vm selectedVm = createdVms.stream()
            .filter(vm -> getExpectedNumberOfFreeVmPes(vm) >= cloudlet.getNumberOfPes())
            .filter(vm -> getExpectedCloudletResponseTime(cloudlet, vm) <= responseTimeSlaContract).findFirst()
            .orElse(mostFreePesVm);

    return selectedVm;
}

From source file:org.cloudsimplus.sla.responsetime.CloudletResponseTimeMinimizationExperiment.java

private void sortCloudletListByExpectedResponseTime() {
    //sort the cloudlet list by expected response time
    Comparator<Cloudlet> sortByExpectedCloudletResponseTime = null;
    for (Vm vm : getVmList()) {
        sortByExpectedCloudletResponseTime = Comparator
                .comparingDouble(cloudlet -> getExpectedCloudletResponseTime(cloudlet, vm));
    }//from ww  w.j  a  v a2  s.com
    cloudletList.sort(sortByExpectedCloudletResponseTime.reversed());
    System.out.println("\t\tCreated Cloudlets: " + getCloudletList());
}

From source file:org.cloudsimplus.sla.responsetime.CloudletResponseTimeMinimizationExperiment.java

/**
* Selects a VM to run a Cloudlet that will minimize the Cloudlet response
* time.//  ww  w.  ja  v  a2  s .c  o  m
*
* @param cloudlet the Cloudlet to select a VM to
* @return the selected Vm
*/
private Vm selectVmForCloudlet(Cloudlet cloudlet) {
    List<Vm> createdVms = cloudlet.getBroker().getVmsCreatedList();
    Log.printLine("\t\tCreated VMs: " + createdVms);
    Comparator<Vm> sortByNumberOfFreePes = Comparator.comparingInt(vm -> getExpectedNumberOfFreeVmPes(vm));
    Comparator<Vm> sortByExpectedCloudletResponseTime = Comparator
            .comparingDouble(vm -> getExpectedCloudletResponseTime(cloudlet, vm));
    createdVms.sort(sortByNumberOfFreePes.thenComparing(sortByExpectedCloudletResponseTime).reversed());
    Vm mostFreePesVm = createdVms.stream().findFirst().orElse(Vm.NULL);

    Vm selectedVm = createdVms.stream()
            .filter(vm -> getExpectedNumberOfFreeVmPes(vm) >= cloudlet.getNumberOfPes())
            .filter(vm -> getExpectedCloudletResponseTime(cloudlet, vm) <= responseTimeSlaContract).findFirst()
            .orElse(mostFreePesVm);

    return selectedVm;
}

From source file:org.cloudsimplus.sla.responsetime.CloudletResponseTimeWorkLoadExperimet.java

/**
 * Selects a VM to run a Cloudlet that will minimize the Cloudlet response
 * time.//from   w  ww  . j a  va2  s .  c  o m
 *
 * @param cloudlet the Cloudlet to select a VM to
 * @return the selected Vm
 */
private Vm selectVmForCloudlet(Cloudlet cloudlet) {
    List<Vm> createdVms = cloudlet.getBroker().getVmsCreatedList();
    //    Log.printLine("\t\tCreated VMs: " + createdVms);
    Comparator<Vm> sortByNumberOfFreePes = Comparator.comparingLong(vm -> getExpectedNumberOfFreeVmPes(vm));
    Comparator<Vm> sortByExpectedCloudletResponseTime = Comparator
            .comparingDouble(vm -> getExpectedCloudletResponseTime(cloudlet, vm));
    createdVms.sort(sortByNumberOfFreePes.thenComparing(sortByExpectedCloudletResponseTime).reversed());
    Vm mostFreePesVm = createdVms.stream().findFirst().orElse(Vm.NULL);

    Vm selectedVm = createdVms.stream()
            .filter(vm -> getExpectedNumberOfFreeVmPes(vm) >= cloudlet.getNumberOfPes())
            .filter(vm -> getExpectedCloudletResponseTime(cloudlet, vm) <= responseTimeSlaContract).findFirst()
            .orElse(mostFreePesVm);

    return selectedVm;
}

From source file:org.specvis.logic.Functions.java

/**
 * Get sorted data from Basic procedure.
 * @return Sorted data from Basic procedure.
 *//*from   w  w w  .  j  a  va2s.c  om*/
public ArrayList<ArrayList<ProcedureBasicStimulus>> getSortedDataFromFromBasicProcedure() {

    ArrayList<ArrayList<ProcedureBasicStimulus>> sortedData = new ArrayList<>();

    // Get ProcedureBasicData.
    ProcedureBasicData procedureBasicData = StartApplication.getSpecvisData().getProcedureBasicData();
    ArrayList<ProcedureBasicStimulus> data = procedureBasicData.getArrayListProcedureBasicStimulus();

    // Sort data by positionOnTheScreenInPixelsY.
    Collections.sort(data, Comparator.comparingDouble(ProcedureBasicStimulus::getPositionOnTheScreenInPixelsY));

    // Sort data by positionOnTheScreenInPixelsX.
    double temp = 0;
    for (int i = 0; i < data.size(); i++) {

        if (temp != data.get(i).getPositionOnTheScreenInPixelsY()) {
            ArrayList<ProcedureBasicStimulus> arrayList = new ArrayList<>(); // getAllStimuliWithSpecifiYPosition(data.get(i).getPositionOnTheScreenInPixelsY(), data);

            for (int j = 0; j < data.size(); j++) {
                if (data.get(i).getPositionOnTheScreenInPixelsY() == data.get(j)
                        .getPositionOnTheScreenInPixelsY()) {
                    arrayList.add(data.get(j));
                }
            }

            Collections.sort(arrayList,
                    Comparator.comparingDouble(ProcedureBasicStimulus::getPositionOnTheScreenInPixelsX));
            sortedData.add(arrayList);
        }

        temp = data.get(i).getPositionOnTheScreenInPixelsY();
    }

    return sortedData;
}