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:com.blm.orc.RecordReaderImpl.java

/**
 * Given a point and min and max, determine if the point is before, at the
 * min, in the middle, at the max, or after the range.
 * @param point the point to test/*from  ww  w .  j  av  a 2  s .  c  o m*/
 * @param min the minimum point
 * @param max the maximum point
 * @param <T> the type of the comparision
 * @return the location of the point
 */
static <T> Location compareToRange(Comparable<T> point, T min, T max) {
    int minCompare = point.compareTo(min);
    if (minCompare < 0) {
        return Location.BEFORE;
    } else if (minCompare == 0) {
        return Location.MIN;
    }
    int maxCompare = point.compareTo(max);
    if (maxCompare > 0) {
        return Location.AFTER;
    } else if (maxCompare == 0) {
        return Location.MAX;
    }
    return Location.MIDDLE;
}

From source file:org.kuali.student.enrollment.class2.acal.service.impl.AcademicCalendarViewHelperServiceImpl.java

protected int compareObject(Comparable s1, Comparable s2) {
    if (s1 == null && s2 != null) {
        return -1;
    }//from  ww w  .j  av a 2 s  . c  o m
    if (s1 != null && s2 == null) {
        return 1;
    }
    if (s1 == null && s2 == null) {
        return 0;
    }
    return s1.compareTo(s2);
}

From source file:com.diversityarrays.kdxplore.curate.TrialDataEditor.java

@SuppressWarnings({ "rawtypes", "unchecked" })
static private Comparable minimumOf(Comparable a, Comparable b) {
    Comparable result = a;/* ww w  .  j  ava  2s  . co  m*/
    if (a == null) {
        if (b != null) {
            result = b;
        }
    } else if (b != null && b.compareTo(a) < 0) {
        result = b;
    }
    return result;
}

From source file:org.apache.solr.SolrTestCaseJ4.java

public static Comparator<Doc> createComparator(final String field, final boolean asc,
        final boolean sortMissingLast, final boolean sortMissingFirst, final boolean sortMissingAsZero) {
    final int mul = asc ? 1 : -1;

    if (field.equals("_docid_")) {
        return (o1, o2) -> (o1.order - o2.order) * mul;
    }//from   w  w  w .j  av  a2 s .c  o m

    if (field.equals("score")) {
        return createComparator("score_f", asc, sortMissingLast, sortMissingFirst, sortMissingAsZero);
    }

    return new Comparator<Doc>() {
        private Comparable zeroVal(Comparable template) {
            if (template == null)
                return null;
            if (template instanceof String)
                return null; // fast-path for string
            if (template instanceof Integer)
                return 0;
            if (template instanceof Long)
                return (long) 0;
            if (template instanceof Float)
                return (float) 0;
            if (template instanceof Double)
                return (double) 0;
            if (template instanceof Short)
                return (short) 0;
            if (template instanceof Byte)
                return (byte) 0;
            if (template instanceof Character)
                return (char) 0;
            return null;
        }

        @Override
        public int compare(Doc o1, Doc o2) {
            Comparable v1 = o1.getFirstValue(field);
            Comparable v2 = o2.getFirstValue(field);

            v1 = v1 == null ? zeroVal(v2) : v1;
            v2 = v2 == null ? zeroVal(v1) : v2;

            int c = 0;
            if (v1 == v2) {
                c = 0;
            } else if (v1 == null) {
                if (sortMissingLast)
                    c = mul;
                else if (sortMissingFirst)
                    c = -mul;
                else
                    c = -1;
            } else if (v2 == null) {
                if (sortMissingLast)
                    c = -mul;
                else if (sortMissingFirst)
                    c = mul;
                else
                    c = 1;
            } else {
                c = v1.compareTo(v2);
            }

            c = c * mul;

            return c;
        }
    };
}

From source file:mondrian.olap.fun.FunUtil.java

/**
 * Compares two members which are known to have the same parent.
 *
 * First, compare by ordinal.//from w w  w.  j  a v a2 s  .  c  o m
 * This is only valid now we know they're siblings, because
 * ordinals are only unique within a parent.
 * If the dimension does not use ordinals, both ordinals
 * will be -1.
 *
 * <p>If the ordinals do not differ, compare using regular member
 * comparison.
 *
 * @param m1 First member
 * @param m2 Second member
 * @return -1 if m1 collates less than m2,
 *   1 if m1 collates after m2,
 *   0 if m1 == m2.
 */
public static int compareSiblingMembers(Member m1, Member m2) {
    // calculated members collate after non-calculated
    final boolean calculated1 = m1.isCalculatedInQuery();
    final boolean calculated2 = m2.isCalculatedInQuery();
    if (calculated1) {
        if (!calculated2) {
            return 1;
        }
    } else {
        if (calculated2) {
            return -1;
        }
    }
    final Comparable k1 = m1.getOrderKey();
    final Comparable k2 = m2.getOrderKey();
    if ((k1 != null) && (k2 != null)) {
        return k1.compareTo(k2);
    } else {
        final int ordinal1 = m1.getOrdinal();
        final int ordinal2 = m2.getOrdinal();
        return (ordinal1 == ordinal2) ? m1.compareTo(m2) : (ordinal1 < ordinal2) ? -1 : 1;
    }
}

From source file:org.eclipse.php.internal.core.ast.util.Util.java

/**
 * Sort the comparable objects in the given collection.
 */// ww  w .  j a  v a2 s . c  o  m
private static void quickSort(Comparable[] sortedCollection, int left, int right) {
    int original_left = left;
    int original_right = right;
    Comparable mid = sortedCollection[left + (right - left) / 2];
    do {
        while (sortedCollection[left].compareTo(mid) < 0) {
            left++;
        }
        while (mid.compareTo(sortedCollection[right]) < 0) {
            right--;
        }
        if (left <= right) {
            Comparable tmp = sortedCollection[left];
            sortedCollection[left] = sortedCollection[right];
            sortedCollection[right] = tmp;
            left++;
            right--;
        }
    } while (left <= right);
    if (original_left < right) {
        quickSort(sortedCollection, original_left, right);
    }
    if (left < original_right) {
        quickSort(sortedCollection, left, original_right);
    }
}

From source file:org.ojbc.bundles.adapters.staticmock.StaticMockQuery.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private boolean checkRange(Document personSearchRequestMessage, Comparable valueParam, Comparable minValueParam,
        Comparable maxValueParam, Comparable documentValue) throws Exception {
    if (valueParam == null && minValueParam == null && maxValueParam == null) {
        // skipping this query parameter
        return true;
    }/*from  w ww.j a va2s .c  o m*/
    if (valueParam != null) {
        // single-value test
        return valueParam.equals(documentValue);
    } else {
        // range test
        return minValueParam.compareTo(documentValue) != 1 && documentValue.compareTo(maxValueParam) != 1;
    }

}

From source file:NavigableMap.java

/**
 * Specialized variant of findNode to perform Map.get. Does a weak traversal,
 * not bothering to fix any deleted index nodes, returning early if it happens
 * to see key in index, and passing over any deleted base nodes, falling back
 * to getUsingFindNode only if it would otherwise return value from an ongoing
 * deletion. Also uses "bound" to eliminate need for some comparisons (see
 * Pugh Cookbook). Also folds uses of null checks and node-skipping because
 * markers have null keys./*from www. ja  v a 2 s.co  m*/
 * 
 * @param okey
 *          the key
 * @return the value, or null if absent
 */
private V doGet(Object okey) {
    Comparable<K> key = comparable(okey);
    K bound = null;
    Index<K, V> q = head;
    for (;;) {
        K rk;
        Index<K, V> d, r;
        if ((r = q.right) != null && (rk = r.key) != null && rk != bound) {
            int c = key.compareTo(rk);
            if (c > 0) {
                q = r;
                continue;
            }
            if (c == 0) {
                Object v = r.node.value;
                return (v != null) ? (V) v : getUsingFindNode(key);
            }
            bound = rk;
        }
        if ((d = q.down) != null)
            q = d;
        else {
            for (Node<K, V> n = q.node.next; n != null; n = n.next) {
                K nk = n.key;
                if (nk != null) {
                    int c = key.compareTo(nk);
                    if (c == 0) {
                        Object v = n.value;
                        return (v != null) ? (V) v : getUsingFindNode(key);
                    }
                    if (c < 0)
                        return null;
                }
            }
            return null;
        }
    }
}

From source file:NavigableMap.java

/**
 * Return a base-level node with key strictly less than given key, or the
 * base-level header if there is no such node. Also unlinks indexes to deleted
 * nodes found along the way. Callers rely on this side-effect of clearing
 * indices to deleted nodes./*  w  w  w .  j ava 2 s.  co m*/
 * 
 * @param key
 *          the key
 * @return a predecessor of key
 */
private Node<K, V> findPredecessor(Comparable<K> key) {
    for (;;) {
        Index<K, V> q = head;
        for (;;) {
            Index<K, V> d, r;
            if ((r = q.right) != null) {
                if (r.indexesDeletedNode()) {
                    if (q.unlink(r))
                        continue; // reread r
                    else
                        break; // restart
                }
                if (key.compareTo(r.key) > 0) {
                    q = r;
                    continue;
                }
            }
            if ((d = q.down) != null)
                q = d;
            else
                return q.node;
        }
    }
}

From source file:NavigableMap.java

/**
 * Add given index nodes from given level down to 1.
 * /*from  ww  w  .j  a  v a  2s. c o  m*/
 * @param idx
 *          the topmost index node being inserted
 * @param h
 *          the value of head to use to insert. This must be snapshotted by
 *          callers to provide correct insertion level
 * @param indexLevel
 *          the level of the index
 */
private void addIndex(Index<K, V> idx, HeadIndex<K, V> h, int indexLevel) {
    // Track next level to insert in case of retries
    int insertionLevel = indexLevel;
    Comparable<K> key = comparable(idx.key);

    // Similar to findPredecessor, but adding index nodes along
    // path to key.
    for (;;) {
        Index<K, V> q = h;
        Index<K, V> t = idx;
        int j = h.level;
        for (;;) {
            Index<K, V> r = q.right;
            if (r != null) {
                // compare before deletion check avoids needing recheck
                int c = key.compareTo(r.key);
                if (r.indexesDeletedNode()) {
                    if (q.unlink(r))
                        continue;
                    else
                        break;
                }
                if (c > 0) {
                    q = r;
                    continue;
                }
            }

            if (j == insertionLevel) {
                // Don't insert index if node already deleted
                if (t.indexesDeletedNode()) {
                    findNode(key); // cleans up
                    return;
                }
                if (!q.link(r, t))
                    break; // restart
                if (--insertionLevel == 0) {
                    // need final deletion check before return
                    if (t.indexesDeletedNode())
                        findNode(key);
                    return;
                }
            }

            if (j > insertionLevel && j <= indexLevel)
                t = t.down;
            q = q.down;
            --j;
        }
    }
}