Example usage for java.lang Integer compareTo

List of usage examples for java.lang Integer compareTo

Introduction

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

Prototype

public int compareTo(Integer anotherInteger) 

Source Link

Document

Compares two Integer objects numerically.

Usage

From source file:net.firejack.platform.service.registry.helper.PackageVersionHelper.java

public static FileInfo[] sortingByNameLikeNumber(FileInfo[] files, boolean desc) {
    Arrays.sort(files, new Comparator() {
        public int compare(final Object o1, final Object o2) {
            Integer i1 = Integer.parseInt(((FileInfo) o1).getFilename());
            Integer i2 = Integer.parseInt(((FileInfo) o2).getFilename());
            return i1.compareTo(i2);
        }//  w  w  w .j a va  2 s. co  m
    });
    if (desc) {
        org.apache.commons.lang.ArrayUtils.reverse(files);
    }
    return files;
}

From source file:com.doculibre.constellio.plugins.PluginFactory.java

private static <P extends ConstellioPlugin> List<P> getPlugins(Class<P> pluginClass, boolean onlyDefault) {
    List<P> matches = new ArrayList<P>();
    P defaultPlugin = null;//from  w  w w . j  a va  2  s.c o  m

    initPluginManager();
    PluginManagerUtil pmu = new PluginManagerUtil(pm);
    for (P impl : pmu.getPlugins(pluginClass)) {
        // ClassLoader classLoader = impl.getClass().getClassLoader();
        // classLoaders.add(classLoader);
        if (DefaultConstellioPlugin.NAME.equals(impl.getName())) {
            defaultPlugin = impl;
        } else if (!onlyDefault) {
            matches.add(impl);
        }
    }
    if (matches.isEmpty()) {
        if (defaultPlugin != null) {
            matches.add(defaultPlugin);
        }
    } else {
        // If many plugins are found, they are sorted in the order they are configured in constellio.xml
        // (the last has priority over the previous)
        Collections.sort(matches, new Comparator<ConstellioPlugin>() {
            @Override
            public int compare(ConstellioPlugin o1, ConstellioPlugin o2) {
                List<String> availablePluginNames = ConstellioSpringUtils.getAvailablePluginNames();
                Integer indexOfPluginName1 = availablePluginNames.indexOf(o1.getName());
                Integer indexOfPluginName2 = availablePluginNames.indexOf(o2.getName());
                return indexOfPluginName1.compareTo(indexOfPluginName2);
            }
        });
    }
    return matches;
}

From source file:io.spring.initializr.util.Version.java

private static int safeCompare(Integer first, Integer second) {
    Integer firstIndex = first != null ? first : 0;
    Integer secondIndex = second != null ? second : 0;
    return firstIndex.compareTo(secondIndex);
}

From source file:com.ephesoft.dcma.workflows.util.WorkflowUtil.java

/**
 * Compare batch instance on the basis of priority of their status for Pick up.
 * // w w  w .j  a va 2s.  c  om
 * @param batchInstance1 {@link BatchInstance}
 * @param batchInstance2 {@link BatchInstance}
 * @return int -1 if batchInstance1 is of higher priority than batchInstance2, -1 if batchInstance1 is of lower priority than
 *         batchInstance2, 0 otherwise.
 */
public static int comparePickUpStatus(final BatchInstance batchInstance1, final BatchInstance batchInstance2) {
    int result;
    initializeBatchStatusPriorityMap();
    if (null == pickUpStatusToPriorityMap) {
        result = -1;
    } else {
        final Integer statusPriority1 = pickUpStatusToPriorityMap.get(batchInstance1.getStatus());
        final Integer statusPriority2 = pickUpStatusToPriorityMap.get(batchInstance2.getStatus());
        result = statusPriority1.compareTo(statusPriority2);
    }
    return result;
}

From source file:de.dal33t.powerfolder.util.ByteSerializer.java

public static final void printStats() {
    if (totalObjects != 0) {
        LOG.fine("Serialization perfomance: " + totalObjects + " took " + totalTime + "ms. That is "
                + ((totalTime * 1000) / totalObjects) + " ms/1k objects. Message stats: ");
        List<Class<?>> sorted = new ArrayList<Class<?>>(CLASS_STATS.keySet());
        Collections.sort(sorted, new Comparator<Class<?>>() {
            public int compare(Class<?> o1, Class<?> o2) {
                Integer n1 = CLASS_STATS.get(o1);
                Integer n2 = CLASS_STATS.get(o2);
                if (n2 == null) {
                    return -1;
                }//w ww . j  av  a 2  s .c  o m
                return n2.compareTo(n1);
            }
        });
        for (Class<?> clazz : sorted) {
            LOG.fine(" " + clazz.getName() + ": " + CLASS_STATS.get(clazz));
        }
    } else {
        LOG.fine("Serialization perfomance: " + totalObjects);
    }
}

From source file:org.apache.sysml.runtime.util.UtilFunctions.java

/**
 * Compares two version strings of format x.y.z, where x is major,
 * y is minor, and z is maintenance release.
 * /*  ww w  .  jav a  2s.  co m*/
 * @param version1 first version string
 * @param version2 second version string
 * @return 1 if version1 greater, -1 if version2 greater, 0 if equal
 */
public static int compareVersion(String version1, String version2) {
    String[] partsv1 = version1.split("\\.");
    String[] partsv2 = version2.split("\\.");
    int len = Math.min(partsv1.length, partsv2.length);
    for (int i = 0; i < partsv1.length && i < len; i++) {
        Integer iv1 = Integer.parseInt(partsv1[i]);
        Integer iv2 = Integer.parseInt(partsv2[i]);
        if (iv1.compareTo(iv2) != 0)
            return iv1.compareTo(iv2);
    }
    return 0; //equal 
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.util.StatHelper.java

/**
 * Sorts a List of Map.Entry into descending or ascending order by value
 *
 * @param entries       a List of Map.Etnry objects
 * @param descending    sort order//from w w  w.  j a  va  2s . c o m
 */
public static void sortMapEntryListByValue(List<Map.Entry<String, Integer>> entries, boolean descending) {
    final int order = descending ? -1 : 1;
    Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
        public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
            Integer e1Value = e1.getValue();
            Integer e2Value = e2.getValue();
            return order * (e1Value.compareTo(e2Value));
        }

        public boolean equals(Object obj) {
            return super.equals(obj);
        }
    });
    //return entries;
}

From source file:org.projectforge.common.NumberHelper.java

/**
 * Compares two given Integers using compareTo method.
 * @param value1/*  w ww.j  a v  a 2  s .  co m*/
 * @param value
 * @return
 * @see Integer#compareTo(Integer)
 */
public static boolean isEqual(final Integer value1, final Integer value) {
    if (value1 == null) {
        return (value == null) ? true : false;
    }
    if (value == null) {
        return false;
    }
    return value1.compareTo(value) == 0;
}

From source file:org.lilyproject.runtime.ClassLoaderConfigurer.java

public static int compareVersions(String version1, String version2) throws UncomparableVersionException {
    // We assume versions follow the pattern: major.minor.revision-suffix
    Pattern pattern = Pattern.compile("(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?(?:(?:-|_)(.*))?");

    Matcher m1 = pattern.matcher(version1);
    Matcher m2 = pattern.matcher(version2);

    if (!m1.matches()) {
        throw new UncomparableVersionException("This version string is not comparable: " + version1);
    }/*from   w  w w .  ja  v a  2  s .  c  o  m*/
    if (!m2.matches()) {
        throw new UncomparableVersionException("This version string is not comparable: " + version2);
    }

    int[] v1 = new int[3];
    for (int i = 0; i < 3; i++) {
        v1[i] = m1.group(i + 1) == null ? 0 : Integer.parseInt(m1.group(i + 1));
    }

    int[] v2 = new int[3];
    for (int i = 0; i < 3; i++) {
        v2[i] = m2.group(i + 1) == null ? 0 : Integer.parseInt(m2.group(i + 1));
    }

    for (int i = 0; i < 3; i++) {
        if (v1[i] > v2[i]) {
            return 1;
        } else if (v1[i] < v2[i]) {
            return -1;
        } else {
            // if equal, compare next part of the version
        }
    }

    // The dotted version parts are equal, now check the suffixes

    // A version without suffix is considered to be more recent than a version with suffix: the suffix serves
    // to indicate some snapshot version (-alpha, -r2323, ...) while the version without suffix is the final
    // release.

    String suffix1 = m1.group(4);
    String suffix2 = m2.group(4);

    if (suffix1 == null && suffix2 == null) {
        return 0;
    } else if (suffix1 == null) {
        return 1;
    } else if (suffix2 == null) {
        return -1;
    }

    // Both have a suffix: try to compare the suffixes

    // Assume snapshot is more recent than anything else
    if (suffix1.equals("SNAPSHOT")) {
        return 1;
    } else if (suffix2.equals("SNAPSHOT")) {
        return -1;
    }

    // Suffix style 1: subversion revision indication with -r{number}
    if (suffix1.startsWith("r") && suffix2.startsWith("r")) {
        Pattern revisionNumberPattern = Pattern.compile("r(\\d+)");

        Matcher rm1 = revisionNumberPattern.matcher(suffix1);
        Matcher rm2 = revisionNumberPattern.matcher(suffix2);

        if (!rm1.matches()) {
            throw new UncomparableVersionException("This version string is not comparable: " + version1);
        }
        if (!rm2.matches()) {
            throw new UncomparableVersionException("This version string is not comparable: " + version2);
        }

        Integer r1 = Integer.parseInt(rm1.group(1));
        Integer r2 = Integer.parseInt(rm2.group(1));

        return r1.compareTo(r2);
    }

    // Suffix style 2: git
    // For git, suffix is assume to be a "date-githash"
    Pattern gitSuffixPattern = Pattern.compile("(\\d{8})-([a-z0-9]+)");
    Matcher gitm1 = gitSuffixPattern.matcher(suffix1);
    Matcher gitm2 = gitSuffixPattern.matcher(suffix2);
    if (gitm1.matches() && gitm2.matches()) {
        String date1 = gitm1.group(1);
        String date2 = gitm2.group(1);

        if (date1.equals(date2)) {
            throw new UncomparableVersionException(
                    "Can't compare two versions with different git-hashes: " + version1 + " and " + version2);
        }

        return date1.compareTo(date2);
    }

    throw new UncomparableVersionException(
            "Don't know how to compare versions " + version1 + " and " + version2);
}

From source file:org.lockss.devtools.RunKbartReport.java

/**
 * Print a usage message./*from  w ww  .jav a2 s  .  co  m*/
 * @param error whether a parsing error provoked this usage display
 */
private static void usage(boolean error) {
    HelpFormatter help = new HelpFormatter();
    // Set a comparator that will output the options in the order
    // they were specified above
    help.setOptionComparator(new Comparator<Option>() {
        public int compare(Option option, Option option1) {
            Integer i = optionList.indexOf(option);
            Integer i1 = optionList.indexOf(option1);
            return i.compareTo(i1);
        }
    });
    // Print blank line
    System.err.println();
    help.printHelp("RunKbartReport", options, true);
    if (error) {
        for (Object o : options.getRequiredOptions()) {
            System.err.format("Note that the -%s option is required\n", o);
        }
    }
    // Show defaults
    System.err.println("");
    //System.err.println("Default output format is "+defaultOutput);
    //System.err.println("Default data format is "+DEFAULT_COLUMN_ORDERING);

    // Print data format options
    System.err.format("Data format argument must be one of the following " + "identifiers (default %s):\n",
            DEFAULT_COLUMN_ORDERING.name());
    for (PredefinedColumnOrdering ord : PredefinedColumnOrdering.values()) {
        System.err.format("  %s (%s)\n", ord.name(), ord.description);
    }
    System.err.format("\nInput file should be UTF-8 encoded and include a "
            + "header row with field names matching KBART field names or any of the " + "following: %s.\n\n",
            StringUtils.join(nonKbartFields, ", "));
    System.exit(0);
}