Example usage for java.lang Integer compare

List of usage examples for java.lang Integer compare

Introduction

In this page you can find the example usage for java.lang Integer compare.

Prototype

public static int compare(int x, int y) 

Source Link

Document

Compares two int values numerically.

Usage

From source file:com.anrisoftware.globalpom.version.Version.java

@Override
public int compareTo(Version o) {
    if (this.major != o.major) {
        return Integer.compare(this.major, o.major);
    }/*from   w  ww  .  j a va2s  .c  o m*/
    if (this.minor != o.minor) {
        return Integer.compare(this.minor, o.minor);
    }
    if (this.rev != o.rev) {
        return Integer.compare(this.rev, o.rev);
    }
    return 0;
}

From source file:org.openhab.binding.plex.internal.PlexApiLevel.java

private boolean isEqualOrBeforeVersion(String version) {
    String[] v1 = fromVersion.split("\\.");
    String[] v2 = version.split("\\.");

    int length = Math.min(v1.length, v2.length);
    for (int i = 0; i < length; i++) {
        int result = new Integer(v1[i]).compareTo(Integer.parseInt(v2[i]));
        if (result != 0) {
            return result <= 0;
        }/*ww w  . j  a va 2s .c om*/
    }

    return Integer.compare(v1.length, v2.length) <= 0;
}

From source file:io.gravitee.management.service.impl.PageServiceImpl.java

@Override
public List<PageListItem> findByApi(String apiId) {
    try {//from   ww  w .  j a v a  2s  .co  m
        final Collection<Page> pages = pageRepository.findByApi(apiId);

        if (pages == null) {
            return emptyList();
        }

        return pages.stream().map(this::reduce)
                .sorted((o1, o2) -> Integer.compare(o1.getOrder(), o2.getOrder())).collect(Collectors.toList());

    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to get API pages using api ID {}", apiId, ex);
        throw new TechnicalManagementException(
                "An error occurs while trying to get API pages using api ID " + apiId, ex);
    }
}

From source file:com.joyent.manta.client.multipart.MantaMultipartUploadTuple.java

@Override
public int compareTo(final MantaMultipartUploadTuple that) {
    return Integer.compare(this.getPartNumber(), that.getPartNumber());
}

From source file:io.kamax.mxisd.lookup.strategy.RecursivePriorityLookupStrategy.java

@PostConstruct
private void build() throws UnknownHostException {
    try {//ww w.j a  v a2  s  .  c om
        log.info("Found {} providers", providers.size());
        providers.forEach(p -> log.info("\t- {}", p.getClass().getName()));
        providers.sort((o1, o2) -> Integer.compare(o2.getPriority(), o1.getPriority()));

        log.info("Recursive lookup enabled: {}", cfg.isEnabled());
        for (String cidr : cfg.getAllowedCidr()) {
            log.info("{} is allowed for recursion", cidr);
            allowedCidr.add(new CIDRUtils(cidr));
        }
    } catch (UnknownHostException e) {
        throw new ConfigurationException("lookup.recursive.allowedCidrs", "Allowed CIDRs");
    }
}

From source file:de.dhke.projects.cutil.collections.frozen.FrozenSortedList.java

@Override
public int compareTo(FrozenSortedList<T> o) {
    for (Pair<T, T> pair : PairIterable.wrap(this, o)) {
        int compare = pair.getFirst().compareTo(pair.getSecond());
        if (compare != 0) {
            return compare;
        }//  w w w.j  a  v a  2  s.c om
    }
    return Integer.compare(size(), o.size());
}

From source file:org.ecloudmanager.monitoring.HaproxyStatsService.java

public HaproxyStatsData loadHaproxyStats(Date startDate, DeploymentObject deploymentObject,
        HaproxyStatsField timeSeriesField) {
    HaproxyStats latestStats = haproxyStatsRepository.getStats(deploymentObject.getId());
    if (latestStats == null) {
        return null;
    }/* w w  w  .ja  va  2  s  .c o m*/

    HaproxyStatsData data = new HaproxyStatsData();
    data.setTimestamp(latestStats.getTimestamp());
    Map<HaproxyStatsField, String> latestData = Stream.of(HaproxyStatsField.values())
            .filter(sf -> !StringUtils.isEmpty(latestStats.getData().get(sf.getKey())))
            .sorted((o1, o2) -> Integer.compare(o2.ordinal(), o1.ordinal())).collect(Collectors.toMap(sf -> sf,
                    sf -> latestStats.getData().get(sf.getKey()), (k1, k2) -> k1, TreeMap::new));
    data.setLatestData(latestData);

    data.setTimeSeriesField(timeSeriesField);

    Map<Object, Number> timeSeries = createTimeSeries(startDate, deploymentObject, timeSeriesField);
    data.setTimeSeriesData(timeSeries);

    return data;
}

From source file:dk.fambagge.recipes.web.views.RecipeView.java

public List<RecipeStep> getStepsSorted() {
    List<RecipeStep> sortedSteps = new LinkedList<>();

    Set<RecipeStep> steps = getSelectedRecipe().getSteps();

    sortedSteps.addAll(steps);/*ww  w  . j  av a  2s . co  m*/

    Collections.sort(sortedSteps, (RecipeStep step1, RecipeStep step2) -> Integer.compare(step1.getSortOrder(),
            step2.getSortOrder()));

    return sortedSteps;
}

From source file:cf.adriantodt.utils.HTML2Discord.java

public static String toPlainText(String discordFormatMessage) {
    String strippedContent;/*from   www . j  a v  a 2  s .c o m*/
    //all the formatting keys to keep track of
    String[] keys = new String[] { "*", "_", "`", "~~" };

    //find all tokens (formatting strings described above)
    TreeSet<FormatToken> tokens = new TreeSet<>((t1, t2) -> Integer.compare(t1.start, t2.start));
    for (String key : keys) {
        Matcher matcher = Pattern.compile(Pattern.quote(key)).matcher(discordFormatMessage);
        while (matcher.find()) {
            tokens.add(new FormatToken(key, matcher.start()));
        }
    }

    //iterate over all tokens, find all matching pairs, and add them to the list toRemove
    Stack<FormatToken> stack = new Stack<>();
    List<FormatToken> toRemove = new ArrayList<>();
    boolean inBlock = false;
    for (FormatToken token : tokens) {
        if (stack.empty() || !stack.peek().format.equals(token.format)
                || stack.peek().start + token.format.length() == token.start) {
            //we are at opening tag
            if (!inBlock) {
                //we are outside of block -> handle normally
                if (token.format.equals("`")) {
                    //block start... invalidate all previous tags
                    stack.clear();
                    inBlock = true;
                }
                stack.push(token);
            } else if (token.format.equals("`")) {
                //we are inside of a block -> handle only block tag
                stack.push(token);
            }
        } else if (!stack.empty()) {
            //we found a matching close-tag
            toRemove.add(stack.pop());
            toRemove.add(token);
            if (token.format.equals("`") && stack.empty()) {
                //close tag closed the block
                inBlock = false;
            }
        }
    }

    //sort tags to remove by their start-index and iteratively build the remaining string
    Collections.sort(toRemove, (t1, t2) -> Integer.compare(t1.start, t2.start));
    StringBuilder out = new StringBuilder();
    int currIndex = 0;
    for (FormatToken formatToken : toRemove) {
        if (currIndex < formatToken.start) {
            out.append(discordFormatMessage.substring(currIndex, formatToken.start));
        }
        currIndex = formatToken.start + formatToken.format.length();
    }
    if (currIndex < discordFormatMessage.length()) {
        out.append(discordFormatMessage.substring(currIndex));
    }
    //return the stripped text, escape all remaining formatting characters (did not have matching open/close before or were left/right of block
    strippedContent = out.toString().replace("*", "\\*").replace("_", "\\_").replace("~", "\\~");
    return strippedContent;
}

From source file:org.eclipse.php.internal.ui.editor.contentassist.PHPCompletionProposalSorter.java

@Override
public int compare(ICompletionProposal p1, ICompletionProposal p2) {
    if (p1 instanceof AbstractScriptCompletionProposal && p2 instanceof AbstractScriptCompletionProposal) {
        AbstractScriptCompletionProposal cp1 = (AbstractScriptCompletionProposal) p1;
        AbstractScriptCompletionProposal cp2 = (AbstractScriptCompletionProposal) p2;

        IModelElement el1 = cp1.getModelElement();
        IModelElement el2 = cp2.getModelElement();

        if (el1.getElementType() == IModelElement.TYPE && el2.getElementType() == IModelElement.TYPE) {
            try {
                int result = Boolean.compare(PHPFlags.isNamespace(((IType) el1).getFlags()),
                        PHPFlags.isNamespace(((IType) el2).getFlags()));
                if (result != 0) {
                    return result;
                }//from w w  w .j a v  a 2  s  .  c  o m
            } catch (ModelException e) {
                Logger.logException(e);
            }
        }

        if (el1.getElementName().equals(el2.getElementName())) {
            String parent1 = getParentQualifier(el1.getParent());
            String parent2 = getParentQualifier(el2.getParent());

            if (parent1 == null && parent2 == null) {
                return 0;
            } else if (parent1 == null) {
                return -1;
            } else if (parent2 == null) {
                return 1;
            }

            int segments1 = StringUtils.countMatches(parent1, "\\"); //$NON-NLS-1$
            int segments2 = StringUtils.countMatches(parent2, "\\"); //$NON-NLS-1$
            // bump up elements in global namespace
            if (segments1 == 0 || segments2 == 0) {
                return Integer.compare(segments1, segments2);
            }
            return parent1.compareToIgnoreCase(parent2);
        }
        return 0;
    }
    return 0;
}