Example usage for java.util Comparator comparing

List of usage examples for java.util Comparator comparing

Introduction

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

Prototype

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor) 

Source Link

Document

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

Usage

From source file:project.combiner.P2ProfileGenerator.java

private static List<String> readLastProfile(Path root) throws IOException {
    Path registryFolder = root.resolve("p2/org.eclipse.equinox.p2.engine/profileRegistry");
    IOStream<Path> sorted = Files.walk(registryFolder).filter(p -> p.getFileName().toString().endsWith(".gz"))
            .sorted(Comparator.comparing(Path::getFileName).reversed());
    Path path = sorted.findFirst().get();
    System.out.println("Reading " + path);
    byte[] bs = Streams.readFully(new GZIPInputStream(Files.newInputStream(path)));
    String string = new String(bs, StandardCharsets.UTF_8).replace("\r", "");
    return List.of(string.split("\n"));
}

From source file:org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.java

/**
 * //from  w  ww .  j av  a  2s . co m
 * @param artifactDefinition
 * @return
 */
public Either<ArtifactDefinition, ResponseFormat> generateHeatEnvArtifact(ArtifactDefinition artifactDefinition,
        org.openecomp.sdc.be.model.Component component, String resourceInstanceName, User modifier,
        boolean shouldLock) {
    List<HeatParameterDefinition> heatParameters = artifactDefinition.getHeatParameters();
    heatParameters.sort(Comparator.comparing(e -> e.getName()));
    StringBuilder sb = new StringBuilder("parameters:\n");
    if (heatParameters != null) {

        for (HeatParameterDefinition heatParameterDefinition : heatParameters) {
            if (heatParameterDefinition.getCurrentValue() != null) {
                HeatParameterType type = HeatParameterType.isValidType(heatParameterDefinition.getType());
                if (type != null) {
                    switch (type) {
                    case BOOLEAN:
                        sb.append("  ").append(heatParameterDefinition.getName()).append(":").append(" ")
                                .append(Boolean.parseBoolean(heatParameterDefinition.getCurrentValue()))
                                .append("\n");
                        break;
                    case NUMBER:
                        // if
                        // (ValidationUtils.isFloatNumber(heatParameterDefinition.getCurrentValue()))
                        // {
                        // sb.append("
                        // ").append(heatParameterDefinition.getName()).append(":").append("
                        // ").append(Float.parseFloat(heatParameterDefinition.getCurrentValue())).append("\n");
                        // } else {
                        // sb.append("
                        // ").append(heatParameterDefinition.getName()).append(":").append("
                        // ").append(Integer.parseInt(heatParameterDefinition.getCurrentValue())).append("\n");
                        // }
                        sb.append("  ").append(heatParameterDefinition.getName()).append(":").append(" ")
                                .append(new BigDecimal(heatParameterDefinition.getCurrentValue())
                                        .toPlainString())
                                .append("\n");
                        break;
                    case COMMA_DELIMITED_LIST:
                    case JSON:
                        sb.append("  ").append(heatParameterDefinition.getName()).append(":").append(" ")
                                .append(heatParameterDefinition.getCurrentValue()).append("\n");
                        break;
                    default:
                        String value = heatParameterDefinition.getCurrentValue();
                        boolean starts = value.startsWith("\"");
                        boolean ends = value.endsWith("\"");
                        if (!(starts && ends)) {
                            starts = value.startsWith("'");
                            ends = value.endsWith("'");
                            if (!(starts && ends)) {
                                value = "\"" + value + "\"";
                            }
                        }
                        sb.append("  ").append(heatParameterDefinition.getName()).append(":").append(" ")
                                .append(value);
                        sb.append("\n");
                        break;

                    }
                }
            }
        }
    }
    return generateAndSaveHeatEnvArtifact(artifactDefinition, sb.toString(), component, resourceInstanceName,
            modifier, shouldLock);

}