Example usage for java.util Comparator nullsLast

List of usage examples for java.util Comparator nullsLast

Introduction

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

Prototype

public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) 

Source Link

Document

Returns a null-friendly comparator that considers null to be greater than non-null.

Usage

From source file:Main.java

public static void main(String... args) {
    List<String> names2 = Arrays.asList("XML", null, "Java", "HTML", "CSS");
    names2.sort(Comparator.nullsLast(String::compareTo));
    System.out.println(names2);/* www  .j  ava  2 s .co m*/

}

From source file:Main.java

public static void main(final String[] args) {
    List<String> names2 = Arrays.asList("peter", null, "anna", "mike", "xenia");
    names2.sort(Comparator.nullsLast(String::compareTo));
    System.out.println(names2);/*from   w  w  w .j a  v a 2  s  . c o m*/
}

From source file:com.thinkbiganalytics.metadata.cache.FeedHealthSummaryCache.java

public FeedStatus getUserFeeds(Long time, String feedName) {
    RoleSetExposingSecurityExpressionRoot userContext = feedAclCache.userContext();
    //streamline the summaries as they could have multiple
    Map<String, FeedSummary> latestFeeds = new HashMap<>();

    Comparator<FeedSummary> byRunningStatus = Comparator.comparing(FeedSummary::getRunStatus,
            Comparator.nullsLast(Comparator.naturalOrder()));

    Comparator<FeedSummary> byStartTime = Comparator.comparing(FeedSummary::getStartTime,
            Comparator.nullsLast(Comparator.naturalOrder()));

    getFeedSummaryList(time).stream().filter(
            summary -> (StringUtils.isBlank(feedName) || feedName.equalsIgnoreCase(summary.getFeedName()))
                    && feedAclCache.hasAccess(userContext, summary.getFeedId().toString()))
            .sorted(byRunningStatus.thenComparing(byStartTime)).forEach(f -> {
                String feedId = f.getFeedId().toString();
                if (!latestFeeds.containsKey(feedId)) {
                    latestFeeds.put(feedId, f);
                }/*from   w w w.  java 2s. c om*/
            });

    //NOTE it could also populate the last job execution time since the above query gets a union of the running jobs along with the latest finished jobs by feed
    List<FeedHealth> feedSummaryHealth = latestFeeds.values().stream()
            .map(f -> FeedModelTransform.feedHealth(f)).collect(Collectors.toList());
    return FeedModelTransform.feedStatus(feedSummaryHealth);
}

From source file:com.netflix.spinnaker.halyard.core.registry.v1.Versions.java

public static Comparator<String> orderBySemVer() {
    Comparator<SemVer> comparator = Comparator.nullsLast(SemVer.comparator());
    return Comparator.comparing(SemVer::fromString, comparator).thenComparing(Comparator.naturalOrder());
}

From source file:com.evolveum.midpoint.wf.impl.processors.primary.policy.ApprovalSchemaBuilder.java

private void processFragmentGroup(List<Fragment> fragments, ApprovalSchemaType resultingSchemaType,
        SchemaAttachedPolicyRulesType attachedRules, ModelInvocationContext ctx, OperationResult result)
        throws SchemaException {
    Fragment firstFragment = fragments.get(0);
    appendAddOnFragments(fragments);/* w  ww .  j  a  va2  s  . co  m*/
    List<ApprovalStageDefinitionType> fragmentStageDefs = cloneAndMergeStages(fragments);
    if (fragmentStageDefs.isEmpty()) {
        return; // probably shouldn't occur
    }
    fragmentStageDefs.sort(Comparator.comparing(s -> getNumber(s), Comparator.nullsLast(naturalOrder())));
    RelationResolver relationResolver = primaryChangeAspect.createRelationResolver(firstFragment.target,
            result);
    ReferenceResolver referenceResolver = primaryChangeAspect.createReferenceResolver(ctx.modelContext,
            ctx.taskFromModel, result);
    int from = getStages(resultingSchemaType).size() + 1;
    int i = from;
    for (ApprovalStageDefinitionType stageDef : fragmentStageDefs) {
        stageDef.asPrismContainerValue().setId(null); // to avoid ID collision
        stageDef.setOrder(null);
        stageDef.setNumber(i++);
        approvalSchemaHelper.prepareStage(stageDef, relationResolver, referenceResolver);
        resultingSchemaType.getStage().add(stageDef);
    }
    if (firstFragment.policyRule != null) {
        List<EvaluatedPolicyRuleType> rules = new ArrayList<>();
        firstFragment.policyRule.addToEvaluatedPolicyRuleTypes(rules,
                new PolicyRuleExternalizationOptions(FULL, false, true));
        for (EvaluatedPolicyRuleType rule : rules) {
            SchemaAttachedPolicyRuleType attachedRule = new SchemaAttachedPolicyRuleType();
            attachedRule.setStageMin(from);
            attachedRule.setStageMax(i - 1);
            attachedRule.setRule(rule);
            attachedRules.getEntry().add(attachedRule);
        }
    }
}

From source file:com.evolveum.midpoint.schema.util.WfContextUtil.java

@NotNull
private static List<ApprovalStageDefinitionType> getSortedStages(ApprovalSchemaType schema) {
    List<ApprovalStageDefinitionType> stages = new ArrayList<>(getStages(schema));
    stages.sort(/*  www  . j a  v a  2 s .  c om*/
            Comparator.comparing(stage -> getNumber(stage), Comparator.nullsLast(Comparator.naturalOrder())));
    return stages;
}

From source file:com.evolveum.midpoint.wf.impl.processors.primary.policy.ApprovalSchemaBuilder.java

private List<ApprovalStageDefinitionType> cloneAndMergeStages(List<Fragment> fragments) throws SchemaException {
    if (fragments.size() == 1) {
        return CloneUtil.cloneCollectionMembers(getStages(fragments.get(0).schema));
    }//  w w w .j  a v a  2 s  .c  o  m
    PrismContext prismContext = primaryChangeAspect.getChangeProcessor().getPrismContext();
    ApprovalStageDefinitionType resultingStageDef = new ApprovalStageDefinitionType(prismContext);
    fragments.sort((f1, f2) -> Comparator.nullsLast(Comparator.<Integer>naturalOrder())
            .compare(f1.compositionStrategy.getMergePriority(), f2.compositionStrategy.getMergePriority()));
    for (Fragment fragment : fragments) {
        mergeStageDefFromFragment(resultingStageDef, fragment);
    }
    return Collections.singletonList(resultingStageDef);
}

From source file:org.cgiar.ccafs.marlo.action.center.impactpathway.OutcomesAction.java

@Override
public void prepare() throws Exception {
    areaID = -1;//from w  ww.ja v a 2s.  c  om
    crpProgramID = -1;
    topicID = -1;

    loggedCenter = (GlobalUnit) this.getSession().get(APConstants.SESSION_CRP);
    loggedCenter = centerService.getGlobalUnitById(loggedCenter.getId());

    try {
        outcomeID = Long.parseLong(StringUtils.trim(this.getRequest().getParameter(APConstants.OUTCOME_ID)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (this.getRequest().getParameter(APConstants.TRANSACTION_ID) != null) {

        transaction = StringUtils.trim(this.getRequest().getParameter(APConstants.TRANSACTION_ID));
        CenterOutcome history = (CenterOutcome) auditLogService.getHistory(transaction);

        if (history != null) {
            outcome = history;
        } else {
            this.transaction = null;
            this.setTransaction("-1");
        }

    } else {
        outcome = outcomeService.getResearchOutcomeById(outcomeID);
    }

    researchAreas = new ArrayList<>(
            loggedCenter.getCenterAreas().stream().filter(ra -> ra.isActive()).collect(Collectors.toList()));

    Collections.sort(researchAreas, (ra1, ra2) -> ra1.getId().compareTo(ra2.getId()));

    if (researchAreas != null && outcome != null) {

        crpProgramID = outcome.getResearchTopic().getResearchProgram().getId();
        selectedProgram = programService.getCrpProgramById(crpProgramID);
        selectedResearchTopic = outcome.getResearchTopic();
        topicID = selectedResearchTopic.getId();
        selectedResearchArea = selectedProgram.getResearchArea();
        areaID = selectedResearchArea.getId();

        Path path = this.getAutoSaveFilePath();

        if (path.toFile().exists() && this.getCurrentUser().isAutoSave()) {
            BufferedReader reader = null;
            reader = new BufferedReader(new FileReader(path.toFile()));
            Gson gson = new GsonBuilder().create();
            JsonObject jReader = gson.fromJson(reader, JsonObject.class);
            reader.close();

            AutoSaveReader autoSaveReader = new AutoSaveReader();

            outcome = (CenterOutcome) autoSaveReader.readFromJson(jReader);

            this.setDraft(true);
        } else {
            this.setDraft(false);

            outcome.setMilestones(new ArrayList<>(outcome.getResearchMilestones().stream()
                    .filter(rm -> rm.isActive()).collect(Collectors.toList())));

        }
        outcome.getMilestones()
                .sort(Comparator.nullsLast((p1, p2) -> p1.getTargetYear().compareTo(p2.getTargetYear())));

        if (selectedProgram.getResearchTopics() != null) {
            researchTopics = new ArrayList<>(selectedProgram.getResearchTopics().stream()
                    .filter(rt -> rt.isActive() && rt.getResearchTopic().trim().length() > 0)
                    .collect(Collectors.toList()));
        }

        researchPrograms = new ArrayList<>(selectedResearchArea.getResearchPrograms().stream()
                .filter(rp -> rp.isActive()).collect(Collectors.toList()));

        researchImpacts = new ArrayList<>(selectedProgram.getResearchImpacts().stream()
                .filter(ri -> ri.isActive() && ri.getDescription().trim().length() > 0)
                .collect(Collectors.toList()));

        targetUnitList = new HashMap<>();
        if (srfTargetUnitManager.findAll() != null) {

            List<SrfTargetUnit> targetUnits = new ArrayList<>();

            List<CrpTargetUnit> crpTargetUnits = new ArrayList<>(loggedCenter.getCrpTargetUnits().stream()
                    .filter(tu -> tu.isActive()).collect(Collectors.toList()));

            for (CrpTargetUnit crpTargetUnit : crpTargetUnits) {
                targetUnits.add(crpTargetUnit.getSrfTargetUnit());
            }

            Collections.sort(targetUnits, (tu1, tu2) -> tu1.getName().toLowerCase().trim()
                    .compareTo(tu2.getName().toLowerCase().trim()));

            for (SrfTargetUnit srfTargetUnit : targetUnits) {
                targetUnitList.put(srfTargetUnit.getId(), srfTargetUnit.getName());
            }

            targetUnitList = this.sortByComparator(targetUnitList);
        }

    }

    String params[] = { loggedCenter.getAcronym(), selectedResearchArea.getId() + "",
            selectedProgram.getId() + "" };
    this.setBasePermission(this.getText(Permission.RESEARCH_PROGRAM_BASE_PERMISSION, params));

    outcomeDb = outcomeService.getResearchOutcomeById(outcomeID);

    if (this.isHttpPost()) {
        outcome.setTargetUnit(null);

        if (researchImpacts != null) {
            researchImpacts.clear();
        }

        if (researchTopics != null) {
            researchTopics.clear();
        }

        if (outcome.getMilestones() != null) {
            outcome.getMilestones().clear();
        }
    }

}