Example usage for java.lang Integer signum

List of usage examples for java.lang Integer signum

Introduction

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

Prototype

public static int signum(int i) 

Source Link

Document

Returns the signum function of the specified int value.

Usage

From source file:test.java.com.spotify.docker.client.DefaultDockerClientTest.java

/**
 * Compares two version strings.//from w w  w .j a  v a  2 s .  c  o  m
 * <p>
 * https://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java
 * </p>
 * Use this instead of String.compareTo() for a non-lexicographical
 * comparison that works for version strings. e.g. "1.10".compareTo("1.6").
 *
 * @param str1 a string of ordinal numbers separated by decimal points.
 * @param str2 a string of ordinal numbers separated by decimal points.
 * @return The result is a negative integer if str1 is _numerically_ less than str2.
 * The result is a positive integer if str1 is _numerically_ greater than str2.
 * The result is zero if the strings are _numerically_ equal.
 * N.B. It does not work if "1.10" is supposed to be equal to "1.10.0".
 */
private int versionCompare(String str1, String str2) {
    String[] vals1 = str1.split("\\.");
    String[] vals2 = str2.split("\\.");
    int i = 0;
    // set index to first non-equal ordinal or length of shortest version string
    while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
        i++;
    }
    // compare first non-equal ordinal number
    if (i < vals1.length && i < vals2.length) {
        int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
        return Integer.signum(diff);
    }
    // the strings are equal or one string is a substring of the other
    // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
    else {
        return Integer.signum(vals1.length - vals2.length);
    }
}

From source file:org.apache.calcite.runtime.SqlFunctions.java

/** SQL <code>SIGN</code> operator applied to int values. */
public static int sign(int b0) {
    return Integer.signum(b0);
}

From source file:org.eclipse.skalli.commons.Statistics.java

Map<String, Long> sortedByFrequencyDescending(Map<String, Long> map) {
    if (map == null || map.size() <= 1) {
        return map;
    }//  w  ww.  jav a  2 s .  co  m
    List<Entry<String, Long>> list = new ArrayList<Entry<String, Long>>(map.entrySet());
    Collections.sort(list, new Comparator<Entry<String, Long>>() {
        @Override
        public int compare(Entry<String, Long> o1, Entry<String, Long> o2) {
            int result = -Integer.signum(o1.getValue().compareTo(o2.getValue()));
            if (result == 0) {
                result = o1.getKey().compareTo(o2.getKey());
            }
            return result;
        }
    });
    LinkedHashMap<String, Long> sortedMap = new LinkedHashMap<String, Long>();
    for (Entry<String, Long> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}

From source file:org.eclipse.skalli.services.permit.Permit.java

private int compareSegments(String[] left, String[] right) {
    int result = -Integer.signum(left.length - right.length);
    if (result == 0) {
        int i = 0;
        while (result == 0 && i < left.length && i < right.length) {
            result = compareSegments(left[i], right[i]);
            i++;//from  ww  w  . j  a  va  2 s  . c o  m
        }
        if (result == 0) {
            result = i < left.length ? -1 : (i < right.length ? 1 : 0);
        }
    }
    return result;
}

From source file:org.eclipse.skalli.services.tagging.TagCount.java

/**
 * This method sorts first by decreasing {@link #getCount() tag count},
 * then alphanumerically by {@link #getName() tag name}.
 *//*from  w w w .j  a  va 2s . co  m*/
@Override
public int compareTo(TagCount o) {
    // sort by reverse order of count
    int result = Integer.signum(o.count - count);
    if (result == 0) {
        // then alphanumerically
        result = name.compareTo(o.name);
    }
    return result;
}

From source file:org.geotools.data.wfs.internal.v1_x.MapServerWFSStrategy.java

/**
 * Compares two version strings. /*from  ww w  .  j a v  a  2 s .  c  om*/
 * 
 * Use this instead of String.compareTo() for a non-lexicographical 
 * comparison that works for version strings. e.g. "1.10".compareTo("1.6").
 * 
 * @note It does not work if "1.10" is supposed to be equal to "1.10.0".
 * 
 * @param str1 a string of ordinal numbers separated by decimal points. 
 * @param str2 a string of ordinal numbers separated by decimal points.
 * @return The result is a negative integer if str1 is _numerically_ less than str2. 
 *         The result is a positive integer if str1 is _numerically_ greater than str2. 
 *         The result is zero if the strings are _numerically_ equal.
 */
// Code from Stack Overflow:
// http://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java
//
private Integer versionCompare(String str1, String str2) {
    String[] vals1 = str1.split("\\.");
    String[] vals2 = str2.split("\\.");
    int i = 0;
    // set index to first non-equal ordinal or length of shortest version string
    while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
        i++;
    }
    // compare first non-equal ordinal number
    if (i < vals1.length && i < vals2.length) {
        int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
        return Integer.signum(diff);
    }
    // the strings are equal or one string is a substring of the other
    // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
    else {
        return Integer.signum(vals1.length - vals2.length);
    }
}

From source file:org.janusgraph.graphdb.serializer.SerializerTest.java

License:asdf

@Test
public void comparableStringSerialization() {
    //Characters//  ww w . j  a  v  a2 s.  co  m
    DataOutput out = serialize.getDataOutput(((int) Character.MAX_VALUE) * 2 + 8);
    for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
        out.writeObjectNotNull(c);
    }
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
        assertEquals(c, serialize.readObjectNotNull(b, Character.class).charValue());
    }

    //String
    for (int t = 0; t < 10000; t++) {
        DataOutput out1 = serialize.getDataOutput(32 + 5);
        DataOutput out2 = serialize.getDataOutput(32 + 5);
        String s1 = RandomGenerator.randomString(1, 32);
        String s2 = RandomGenerator.randomString(1, 32);
        out1.writeObjectByteOrder(s1, String.class);
        out2.writeObjectByteOrder(s2, String.class);
        StaticBuffer b1 = out1.getStaticBuffer();
        StaticBuffer b2 = out2.getStaticBuffer();
        assertEquals(s1, serialize.readObjectByteOrder(b1.asReadBuffer(), String.class));
        assertEquals(s2, serialize.readObjectByteOrder(b2.asReadBuffer(), String.class));
        assertEquals(s1 + " vs " + s2, Integer.signum(s1.compareTo(s2)), Integer.signum(b1.compareTo(b2)));
    }
}

From source file:org.parakoopa.gmnetgate.punch.Mediator.java

/**
 * Compares two version strings./*from w w  w  .  ja v  a 2 s .c om*/
 *
 * Use this instead of String.compareTo() for a non-lexicographical
 * comparison that works for version strings. e.g. "1.10".compareTo("1.6").
 * By http://stackoverflow.com/questions/6701948/efficient-way-to-compare-version-strings-in-java
 *
 * @note It does not work if "1.10" is supposed to be equal to "1.10.0".
 *
 * @param str1 a string of ordinal numbers separated by decimal points.
 * @param str2 a string of ordinal numbers separated by decimal points.
 * @return The result is a negative integer if str1 is _numerically_ less
 * than str2. The result is a positive integer if str1 is _numerically_
 * greater than str2. The result is zero if the strings are _numerically_
 * equal.
 */
public static Integer versionCompare(String str1, String str2) {
    String[] vals1 = str1.split("\\.");
    String[] vals2 = str2.split("\\.");
    int i = 0;
    // set index to first non-equal ordinal or length of shortest version string
    while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
        i++;
    }
    // compare first non-equal ordinal number
    if (i < vals1.length && i < vals2.length) {
        int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
        return Integer.signum(diff);
    } // the strings are equal or one string is a substring of the other
      // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
    else {
        return Integer.signum(vals1.length - vals2.length);
    }
}

From source file:org.waveprotocol.wave.examples.fedone.common.HashedVersion.java

/**
 * Lexicographic comparison of two byte arrays.
 *
 * @return -1, 0, or 1/*from  w  w  w  .ja  va2s  .  com*/
 */
private static int compare(byte[] first, byte[] second) {
    if (first == second) {
        return 0; // no need to compare contents
    }
    for (int i = 0; i < first.length && i < second.length; i++) {
        if (first[i] != second[i]) {
            return Integer.signum(first[i] - second[i]);
        }
    }
    // Bytes are equal up to the length of the shortest array. Then longest is bigger.
    return Integer.signum(first.length - second.length);
}

From source file:shuffle.fwk.data.simulation.SimulationTask.java

public static List<Integer> getComboForLimits(List<Integer> limits) {
    List<Integer> ret = new ArrayList<Integer>();
    int rowDir = Integer.signum(limits.get(2) - limits.get(0));
    int colDir = Integer.signum(limits.get(3) - limits.get(1));
    if (rowDir == 0 && colDir == 0) {
        ret.addAll(Arrays.asList(limits.get(0), limits.get(1)));
    } else {//from   w ww .j a  v a 2  s  . c om
        int row = limits.get(0);
        int col = limits.get(1);
        while (row <= limits.get(2) && col <= limits.get(3)) {
            ret.add(row);
            ret.add(col);
            row += rowDir;
            col += colDir;
        }
    }
    return ret;
}