Example usage for java.lang Comparable compareTo

List of usage examples for java.lang Comparable compareTo

Introduction

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

Prototype

public int compareTo(T o);

Source Link

Document

Compares this object with the specified object for order.

Usage

From source file:NavigableMap.java

/**
 * Return node holding key or null if no such, clearing out any deleted nodes
 * seen along the way. Repeatedly traverses at base-level looking for key
 * starting at predecessor returned from findPredecessor, processing
 * base-level deletions as encountered. Some callers rely on this side-effect
 * of clearing deleted nodes./*w w w .j  a v  a 2 s . co  m*/
 * 
 * Restarts occur, at traversal step centered on node n, if:
 * 
 * (1) After reading n's next field, n is no longer assumed predecessor b's
 * current successor, which means that we don't have a consistent 3-node
 * snapshot and so cannot unlink any subsequent deleted nodes encountered.
 * 
 * (2) n's value field is null, indicating n is deleted, in which case we help
 * out an ongoing structural deletion before retrying. Even though there are
 * cases where such unlinking doesn't require restart, they aren't sorted out
 * here because doing so would not usually outweigh cost of restarting.
 * 
 * (3) n is a marker or n's predecessor's value field is null, indicating
 * (among other possibilities) that findPredecessor returned a deleted node.
 * We can't unlink the node because we don't know its predecessor, so rely on
 * another call to findPredecessor to notice and return some earlier
 * predecessor, which it will do. This check is only strictly needed at
 * beginning of loop, (and the b.value check isn't strictly needed at all) but
 * is done each iteration to help avoid contention with other threads by
 * callers that will fail to be able to change links, and so will retry
 * anyway.
 * 
 * The traversal loops in doPut, doRemove, and findNear all include the same
 * three kinds of checks. And specialized versions appear in doRemoveFirst,
 * doRemoveLast, findFirst, and findLast. They can't easily share code because
 * each uses the reads of fields held in locals occurring in the orders they
 * were performed.
 * 
 * @param key
 *          the key
 * @return node holding key, or null if no such.
 */
private Node<K, V> findNode(Comparable<K> key) {
    for (;;) {
        Node<K, V> b = findPredecessor(key);
        Node<K, V> n = b.next;
        for (;;) {
            if (n == null)
                return null;
            Node<K, V> f = n.next;
            if (n != b.next) // inconsistent read
                break;
            Object v = n.value;
            if (v == null) { // n is deleted
                n.helpDelete(b, f);
                break;
            }
            if (v == n || b.value == null) // b is deleted
                break;
            int c = key.compareTo(n.key);
            if (c < 0)
                return null;
            if (c == 0)
                return n;
            b = n;
            n = f;
        }
    }
}

From source file:NavigableMap.java

/**
 * Main deletion method. Locates node, nulls value, appends a deletion marker,
 * unlinks predecessor, removes associated index nodes, and possibly reduces
 * head index level./*from  w  ww.java  2 s  .c  om*/
 * 
 * Index nodes are cleared out simply by calling findPredecessor. which
 * unlinks indexes to deleted nodes found along path to key, which will
 * include the indexes to this node. This is done unconditionally. We can't
 * check beforehand whether there are index nodes because it might be the case
 * that some or all indexes hadn't been inserted yet for this node during
 * initial search for it, and we'd like to ensure lack of garbage retention,
 * so must call to be sure.
 * 
 * @param okey
 *          the key
 * @param value
 *          if non-null, the value that must be associated with key
 * @return the node, or null if not found
 */
private V doRemove(Object okey, Object value) {
    Comparable<K> key = comparable(okey);
    for (;;) {
        Node<K, V> b = findPredecessor(key);
        Node<K, V> n = b.next;
        for (;;) {
            if (n == null)
                return null;
            Node<K, V> f = n.next;
            if (n != b.next) // inconsistent read
                break;
            Object v = n.value;
            if (v == null) { // n is deleted
                n.helpDelete(b, f);
                break;
            }
            if (v == n || b.value == null) // b is deleted
                break;
            int c = key.compareTo(n.key);
            if (c < 0)
                return null;
            if (c > 0) {
                b = n;
                n = f;
                continue;
            }
            if (value != null && !value.equals(v))
                return null;
            if (!n.casValue(v, null))
                break;
            if (!n.appendMarker(f) || !b.casNext(n, f))
                findNode(key); // Retry via findNode
            else {
                findPredecessor(key); // Clean index
                if (head.right == null)
                    tryReduceLevel();
            }
            return (V) v;
        }
    }
}

From source file:NavigableMap.java

/**
 * Utility for ceiling, floor, lower, higher methods.
 * //  w  w  w.  ja v a  2s . c om
 * @param kkey
 *          the key
 * @param rel
 *          the relation -- OR'ed combination of EQ, LT, GT
 * @return nearest node fitting relation, or null if no such
 */
Node<K, V> findNear(K kkey, int rel) {
    Comparable<K> key = comparable(kkey);
    for (;;) {
        Node<K, V> b = findPredecessor(key);
        Node<K, V> n = b.next;
        for (;;) {
            if (n == null)
                return ((rel & LT) == 0 || b.isBaseHeader()) ? null : b;
            Node<K, V> f = n.next;
            if (n != b.next) // inconsistent read
                break;
            Object v = n.value;
            if (v == null) { // n is deleted
                n.helpDelete(b, f);
                break;
            }
            if (v == n || b.value == null) // b is deleted
                break;
            int c = key.compareTo(n.key);
            if ((c == 0 && (rel & EQ) != 0) || (c < 0 && (rel & LT) == 0))
                return n;
            if (c <= 0 && (rel & LT) != 0)
                return (b.isBaseHeader()) ? null : b;
            b = n;
            n = f;
        }
    }
}

From source file:NavigableMap.java

/**
 * Main insertion method. Adds element if not present, or replaces value if
 * present and onlyIfAbsent is false./*from  ww w  . j a va 2  s  . com*/
 * 
 * @param kkey
 *          the key
 * @param value
 *          the value that must be associated with key
 * @param onlyIfAbsent
 *          if should not insert if already present
 * @return the old value, or null if newly inserted
 */
private V doPut(K kkey, V value, boolean onlyIfAbsent) {
    Comparable<K> key = comparable(kkey);
    for (;;) {
        Node<K, V> b = findPredecessor(key);
        Node<K, V> n = b.next;
        for (;;) {
            if (n != null) {
                Node<K, V> f = n.next;
                if (n != b.next) // inconsistent read
                    break;
                ;
                Object v = n.value;
                if (v == null) { // n is deleted
                    n.helpDelete(b, f);
                    break;
                }
                if (v == n || b.value == null) // b is deleted
                    break;
                int c = key.compareTo(n.key);
                if (c > 0) {
                    b = n;
                    n = f;
                    continue;
                }
                if (c == 0) {
                    if (onlyIfAbsent || n.casValue(v, value))
                        return (V) v;
                    else
                        break; // restart if lost race to replace value
                }
                // else c < 0; fall through
            }

            Node<K, V> z = new Node<K, V>(kkey, value, n);
            if (!b.casNext(n, z))
                break; // restart if lost race to append to b
            int level = randomLevel();
            if (level > 0)
                insertIndex(z, level);
            return null;
        }
    }
}

From source file:org.openTwoFactor.server.util.TwoFactorServerUtilsElSafe.java

/**
 * compare null safe// ww  w.j a  v  a 2s.c  o  m
 * @param first
 * @param second
 * @return 0 for equal, 1 for greater, -1 for less
 */
@SuppressWarnings("unchecked")
public static int compare(Comparable first, Comparable second) {
    if (first == second) {
        return 0;
    }
    if (first == null) {
        return -1;
    }
    if (second == null) {
        return 1;
    }
    return first.compareTo(second);
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from  w  w  w  . java2  s  .  c om
 * Validate that the specified argument object fall between the two
 * inclusive values specified; otherwise, throws an exception.
 * </p>
 * 
 * <pre>
 * Validate.inclusiveBetween(0, 2, 1);
 * </pre>
 * 
 * @param value
 *            the object to validate
 * @param start
 *            the inclusive start value
 * @param end
 *            the inclusive end value
 * @throws IllegalArgumentException
 *             if the value falls out of the boundaries
 * @see #inclusiveBetween(Object, Object, Comparable, String, Object...)
 */
public static <T> void inclusiveBetween(T start, T end, Comparable<T> value) {
    if (value.compareTo(start) < 0 || value.compareTo(end) > 0) {
        throw new IllegalArgumentException(
                String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end));
    }
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from www.  j av  a 2 s .co  m
 * Validate that the specified argument object fall between the two
 * exclusive values specified; otherwise, throws an exception.
 * </p>
 * 
 * <pre>
 * Validate.inclusiveBetween(0, 2, 1);
 * </pre>
 * 
 * @param value
 *            the object to validate
 * @param start
 *            the exclusive start value
 * @param end
 *            the exclusive end value
 * @throws IllegalArgumentException
 *             if the value falls out of the boundaries
 * @see #exclusiveBetween(Object, Object, Comparable, String, Object...)
 */
public static <T> void exclusiveBetween(T start, T end, Comparable<T> value) {
    if (value.compareTo(start) <= 0 || value.compareTo(end) >= 0) {
        throw new IllegalArgumentException(
                String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end));
    }
}

From source file:com.clark.func.Functions.java

/**
 * <p>// w  w w .j  a  v  a2  s  . c o  m
 * Validate that the specified argument object fall between the two
 * inclusive values specified; otherwise, throws an exception with the
 * specified message.
 * </p>
 * 
 * <pre>
 * Validate.inclusiveBetween(0, 2, 1, &quot;Not in boundaries&quot;);
 * </pre>
 * 
 * @param value
 *            the object to validate
 * @param start
 *            the inclusive start value
 * @param end
 *            the inclusive end value
 * @param message
 *            the exception message
 * @param values
 *            to replace in the exception message (optional)
 * @throws IllegalArgumentException
 *             if the value falls out of the boundaries
 * @see #inclusiveBetween(Object, Object, Comparable)
 */
public static <T> void inclusiveBetween(T start, T end, Comparable<T> value, String message, Object... values) {
    if (value.compareTo(start) < 0 || value.compareTo(end) > 0) {
        throw new IllegalArgumentException(String.format(message, values));
    }
}

From source file:com.clark.func.Functions.java

/**
 * <p>// w  w w . j a va 2s . c  o  m
 * Validate that the specified argument object fall between the two
 * exclusive values specified; otherwise, throws an exception with the
 * specified message.
 * </p>
 * 
 * <pre>
 * Validate.inclusiveBetween(0, 2, 1, &quot;Not in boundaries&quot;);
 * </pre>
 * 
 * @param value
 *            the object to validate
 * @param start
 *            the exclusive start value
 * @param end
 *            the exclusive end value
 * @param message
 *            the exception message
 * @param values
 *            to replace in the exception message (optional)
 * @throws IllegalArgumentException
 *             if the value falls out of the boundaries
 * @see #exclusiveBetween(Object, Object, Comparable)
 */
public static <T> void exclusiveBetween(T start, T end, Comparable<T> value, String message, Object... values) {
    if (value.compareTo(start) <= 0 || value.compareTo(end) >= 0) {
        throw new IllegalArgumentException(String.format(message, values));
    }
}