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:RandomChooser.java

public static <T> void gte(Comparable<T> c1, T c2) {
    if (c1.compareTo(c2) >= 0)
        return;/*from   w  w w. j a v  a2s  .com*/
    throw new IllegalArgumentException(c1 + GTE + c2);
}

From source file:RandomChooser.java

public static <T> void lte(Comparable<T> c1, T c2) {
    if (c1.compareTo(c2) <= 0)
        return;/*from   w w w  .  j a va  2  s .c o m*/
    throw new IllegalArgumentException(c1 + LTE + c2);
}

From source file:org.sonar.core.measure.MeasureFilterSql.java

private static Ordering newObjectOrdering(boolean ascending) {
    if (ascending) {
        return Ordering.from(new Comparator<Comparable>() {
            public int compare(@Nullable Comparable left, @Nullable Comparable right) {
                if (left == null) {
                    return 1;
                }/*  w w w .  ja va2  s.  c  o  m*/
                if (right == null) {
                    return -1;
                }

                return left.compareTo(right);
            }
        });
    }
    return Ordering.from(new Comparator<Comparable>() {
        public int compare(@Nullable Comparable left, @Nullable Comparable right) {
            if (left == null) {
                return 1;
            }
            if (right == null) {
                return -1;
            }

            return -left.compareTo(right);
        }
    });
}

From source file:org.kuali.rice.core.api.util.EqualsAndHashCodeUtils.java

/**
 * This method provides an equals comparison of two objects by evaluating the results of compareTo across specified
 * internal fields of the class of the two objects being compared.
 * <p/>/*from   w  w w  .jav  a 2s  . c o m*/
 * This method should be used where evaluating equality on fields of two instances of type T using .equals() yields
 * false, but for the purposes of determining equality of the two instances of type T, should be true.  An example
 * is where a class has internal fields of type Calendar that need equality determined using only its time value
 * and not other internal fields of Calendar.
 *
 * @param o1         The first object used in an equality operation using compareTo
 * @param o2         The second object used in an equality operation using compareTo
 * @param fieldNames All field names within type T that should be determined equal or not using compareTo
 * @param <T>        Type of both o1 and o2 parameters.  Guarantees both o1 and o2 are the same reference type.
 * @return true if (o1.field.compareTo(o2.field) == 0) is true for all passed in fieldNames.  Otherwise false
 *         is returned.  False is also returned if any fields specified in fieldNames are not of type Comparable or if one
 *         (but not both) of the passed in objects are null references.
 */
public static <T> boolean equalsUsingCompareToOnFields(T o1, T o2, String... fieldNames) {
    if (o1 == o2) {
        return true;
    }
    if (o1 == null || o2 == null) {
        return false;
    }

    boolean isEqual = true;
    Class<?> targetClass = o1.getClass();
    try {
        for (String fieldName : fieldNames) {
            Field field = targetClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            Class<?> fieldClass = field.getType();

            if (ArrayUtils.contains(fieldClass.getInterfaces(), Comparable.class)) {
                @SuppressWarnings("unchecked")
                Comparable<Object> c1 = (Comparable<Object>) field.get(o1);
                @SuppressWarnings("unchecked")
                Comparable<Object> c2 = (Comparable<Object>) field.get(o2);
                if (c1 == c2) {
                    continue;
                }
                if (c1 == null || c2 == null) {
                    isEqual = false;
                } else {
                    isEqual = (c1.compareTo(c2) == 0);
                }
            } else {
                isEqual = false;
            }

            if (!isEqual) {
                break;
            }
        }

        return isEqual;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.hadoop.hive.ql.optimizer.calcite.druid.DruidIntervalUtils.java

protected static List<Range> leafToRanges(RelDataType type, RexCall call, boolean withNot) {
    switch (call.getKind()) {
    case EQUALS:// w  w w  .j  a  va2s.  com
    case LESS_THAN:
    case LESS_THAN_OR_EQUAL:
    case GREATER_THAN:
    case GREATER_THAN_OR_EQUAL: {
        RexLiteral literal = null;
        if (call.getOperands().get(0) instanceof RexInputRef
                && call.getOperands().get(1) instanceof RexLiteral) {
            literal = extractLiteral(call.getOperands().get(1));
        } else if (call.getOperands().get(0) instanceof RexInputRef
                && call.getOperands().get(1).getKind() == SqlKind.CAST) {
            literal = extractLiteral(call.getOperands().get(1));
        } else if (call.getOperands().get(1) instanceof RexInputRef
                && call.getOperands().get(0) instanceof RexLiteral) {
            literal = extractLiteral(call.getOperands().get(0));
        } else if (call.getOperands().get(1) instanceof RexInputRef
                && call.getOperands().get(0).getKind() == SqlKind.CAST) {
            literal = extractLiteral(call.getOperands().get(0));
        }
        if (literal == null) {
            return null;
        }
        Comparable value = literalToType(literal, type);
        if (value == null) {
            return null;
        }
        if (call.getKind() == SqlKind.LESS_THAN) {
            return Arrays.<Range>asList(withNot ? Range.atLeast(value) : Range.lessThan(value));
        } else if (call.getKind() == SqlKind.LESS_THAN_OR_EQUAL) {
            return Arrays.<Range>asList(withNot ? Range.greaterThan(value) : Range.atMost(value));
        } else if (call.getKind() == SqlKind.GREATER_THAN) {
            return Arrays.<Range>asList(withNot ? Range.atMost(value) : Range.greaterThan(value));
        } else if (call.getKind() == SqlKind.GREATER_THAN_OR_EQUAL) {
            return Arrays.<Range>asList(withNot ? Range.lessThan(value) : Range.atLeast(value));
        } else { //EQUALS
            if (!withNot) {
                return Arrays.<Range>asList(Range.closed(value, value));
            }
            return Arrays.<Range>asList(Range.lessThan(value), Range.greaterThan(value));
        }
    }
    case BETWEEN: {
        RexLiteral literal1 = extractLiteral(call.getOperands().get(2));
        if (literal1 == null) {
            return null;
        }
        RexLiteral literal2 = extractLiteral(call.getOperands().get(3));
        if (literal2 == null) {
            return null;
        }
        Comparable value1 = literalToType(literal1, type);
        Comparable value2 = literalToType(literal2, type);
        if (value1 == null || value2 == null) {
            return null;
        }
        boolean inverted = value1.compareTo(value2) > 0;
        if (!withNot) {
            return Arrays.<Range>asList(inverted ? Range.closed(value2, value1) : Range.closed(value1, value2));
        }
        return Arrays.<Range>asList(Range.lessThan(inverted ? value2 : value1),
                Range.greaterThan(inverted ? value1 : value2));
    }
    case IN: {
        List<Range> ranges = Lists.newArrayList();
        for (int i = 1; i < call.getOperands().size(); i++) {
            RexLiteral literal = extractLiteral(call.getOperands().get(i));
            if (literal == null) {
                return null;
            }
            Comparable element = literalToType(literal, type);
            if (element == null) {
                return null;
            }
            if (withNot) {
                ranges.addAll(Arrays.<Range>asList(Range.lessThan(element), Range.greaterThan(element)));
            } else {
                ranges.add(Range.closed(element, element));
            }
        }
        return ranges;
    }
    default:
        return null;
    }
}

From source file:org.apache.calcite.adapter.druid.DruidDateTimeUtils.java

protected static List<Range> leafToRanges(RelDataType type, RexCall call, boolean withNot) {
    switch (call.getKind()) {
    case EQUALS:/*from  w  ww . j  av  a  2  s . com*/
    case LESS_THAN:
    case LESS_THAN_OR_EQUAL:
    case GREATER_THAN:
    case GREATER_THAN_OR_EQUAL: {
        RexLiteral literal = null;
        if (call.getOperands().get(0) instanceof RexInputRef
                && call.getOperands().get(1) instanceof RexLiteral) {
            literal = extractLiteral(call.getOperands().get(1));
        } else if (call.getOperands().get(0) instanceof RexInputRef
                && call.getOperands().get(1).getKind() == SqlKind.CAST) {
            literal = extractLiteral(call.getOperands().get(1));
        } else if (call.getOperands().get(1) instanceof RexInputRef
                && call.getOperands().get(0) instanceof RexLiteral) {
            literal = extractLiteral(call.getOperands().get(0));
        } else if (call.getOperands().get(1) instanceof RexInputRef
                && call.getOperands().get(0).getKind() == SqlKind.CAST) {
            literal = extractLiteral(call.getOperands().get(0));
        }
        if (literal == null) {
            return null;
        }
        Comparable value = literalToType(literal, type);
        if (value == null) {
            return null;
        }
        switch (call.getKind()) {
        case LESS_THAN:
            return Arrays.<Range>asList(withNot ? Range.atLeast(value) : Range.lessThan(value));
        case LESS_THAN_OR_EQUAL:
            return Arrays.<Range>asList(withNot ? Range.greaterThan(value) : Range.atMost(value));
        case GREATER_THAN:
            return Arrays.<Range>asList(withNot ? Range.atMost(value) : Range.greaterThan(value));
        case GREATER_THAN_OR_EQUAL:
            return Arrays.<Range>asList(withNot ? Range.lessThan(value) : Range.atLeast(value));
        default:
            if (!withNot) {
                return Arrays.<Range>asList(Range.closed(value, value));
            }
            return Arrays.<Range>asList(Range.lessThan(value), Range.greaterThan(value));
        }
    }
    case BETWEEN: {
        RexLiteral literal1 = extractLiteral(call.getOperands().get(2));
        if (literal1 == null) {
            return null;
        }
        RexLiteral literal2 = extractLiteral(call.getOperands().get(3));
        if (literal2 == null) {
            return null;
        }
        Comparable value1 = literalToType(literal1, type);
        Comparable value2 = literalToType(literal2, type);
        if (value1 == null || value2 == null) {
            return null;
        }
        boolean inverted = value1.compareTo(value2) > 0;
        if (!withNot) {
            return Arrays.<Range>asList(inverted ? Range.closed(value2, value1) : Range.closed(value1, value2));
        }
        return Arrays.<Range>asList(Range.lessThan(inverted ? value2 : value1),
                Range.greaterThan(inverted ? value1 : value2));
    }
    case IN: {
        List<Range> ranges = Lists.newArrayList();
        for (int i = 1; i < call.getOperands().size(); i++) {
            RexLiteral literal = extractLiteral(call.getOperands().get(i));
            if (literal == null) {
                return null;
            }
            Comparable element = literalToType(literal, type);
            if (element == null) {
                return null;
            }
            if (withNot) {
                ranges.addAll(Arrays.<Range>asList(Range.lessThan(element), Range.greaterThan(element)));
            } else {
                ranges.add(Range.closed(element, element));
            }
        }
        return ranges;
    }
    default:
        return null;
    }
}

From source file:ArrayUtils.java

@SuppressWarnings("unchecked")
public static int binarySearch(final Object[] a, final int fromIndex, final int toIndex, final Object key) {
    int low = fromIndex;
    int high = toIndex - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        Comparable midVal = (Comparable) a[mid];
        int cmp = midVal.compareTo(key);

        if (cmp < 0) {
            low = mid + 1;//w  w  w  .j  av  a  2s.c  o m
        } else if (cmp > 0) {
            high = mid - 1;
        } else {
            return mid; // key found
        }
    }
    return -(low + 1); // key not found.
}

From source file:org.objectstyle.cayenne.util.Util.java

/**
 * Compares two objects similar to "Comparable.compareTo(Object)". 
 * Unlike Comparable.compareTo(..), this method doesn't throw an exception
 * if any of the two objects is null./*  w ww. jav  a  2s .  c o  m*/
 * 
 * @since 1.1
 */
public static int nullSafeCompare(boolean nullsFirst, Comparable o1, Object o2) {
    if (o1 == null && o2 == null) {
        return 0;
    } else if (o1 == null) {
        return nullsFirst ? -1 : 1;
    } else if (o2 == null) {
        return nullsFirst ? 1 : -1;
    } else {
        return o1.compareTo(o2);
    }
}

From source file:dbs_project.util.Utils.java

public static int compareObjects(Comparable left, Comparable right) {
    if (left != null) {
        if (right != null) {
            //left != null && right != null -> let compare decide
            return left.compareTo(right);
        } else {//  w  w  w  .  j  av a  2 s  .  c o m
            //left != null && right == null -> 1 as left > right
            return 1;
        }
    } else {
        if (right == null) {
            //left == right == null
            return 0;
        } else {
            //left == null && right != null -> -1 as left < right
            return -1;
        }
    }
}

From source file:Main.java

@Override
public void insertElementAt(Object element, int index) {
    int size = getSize();
    for (index = 0; index < size; index++) {
        Comparable c = (Comparable) getElementAt(index);
        if (c.compareTo(element) > 0) {
            break;
        }//from  ww w.ja va  2s .c  om
    }
    super.insertElementAt(element, index);
}