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:viper.api.time.TimeEncodedList.java

/**
 * Sets the value at the given range. Note that, like 
 * <code>SortedMap</code>, this means that value is set in the
 * range from start, inclusive, to stop, exclusive.
 * @param start the first index to set/*from   w ww . j a  v  a 2s. com*/
 * @param stop the first index that is not set
 * @param value all elements in the list in the range [start, stop)
 *   will take this value
 * @throws IllegalArgumentException if start is not less than stop
 */
public void set(Comparable start, Comparable stop, Object value) {
    if (start.compareTo(stop) >= 0) {
        throw new IllegalArgumentException("Start not strictly less than stop: " + start + " !< " + stop);
    }
    if (value == null) {
        remove(start, stop);
        return;
    }
    SortedMap head = values.headMap(start);
    if (!head.isEmpty()) {
        LelNode n = (LelNode) head.get(head.lastKey());
        if (n.getValue().equals(value)) {
            if (n.getEnd().compareTo(start) >= 0) {
                start = (Instant) head.lastKey();
            }
        } else if (n.getEnd().compareTo(start) > 0) {
            head.put(head.lastKey(), new LelNode((Instant) start, n.getValue()));
            if (n.getEnd().compareTo(stop) > 0) {
                values.put(start, new LelNode((Instant) stop, value));
                values.put(stop, new LelNode(n.getEnd(), n.getValue()));
                return;
            }
        }
    }
    SortedMap sub = values.subMap(start, stop);
    if (!sub.isEmpty()) {
        LelNode n = (LelNode) sub.get(sub.lastKey());
        if (n.getValue().equals(value)) {
            if (n.getEnd().compareTo(stop) > 0) {
                stop = n.getEnd();
            }
        } else if (n.getEnd().compareTo(stop) > 0) {
            values.put(stop, new LelNode(n.getEnd(), n.getValue()));
        }
    }
    values.subMap(start, stop).clear();
    values.put(start, new TimeEncodedList.LelNode((Instant) stop, value));
}

From source file:org.apache.jcs.utils.struct.SortedPreferentialArray.java

/**
 * If the array is full this will remove the smallest if preferLarge==true and if obj is bigger,
 * or the largest if preferLarge=false and obj is smaller than the largest.
 * <p>// ww  w  . j  a  va  2s.c  om
 * @param obj Object
 */
public synchronized void add(Comparable obj) {
    if (obj == null) {
        return;
    }

    if (curSize < maxSize) {
        insert(obj);
        return;
    }
    if (preferLarge) {
        // insert if obj is larger than the smallest
        Comparable sma = getSmallest();
        if (obj.compareTo(sma) > 0) {
            insert(obj);
            return;
        }
        // obj is less than or equal to the smallest.
        if (log.isDebugEnabled()) {
            log.debug("New object is smaller than or equal to the smallest");
        }
        return;
    }
    // Not preferLarge
    Comparable lar = getLargest();
    // insert if obj is smaller than the largest
    int diff = obj.compareTo(lar);
    if (diff > 0 || diff == 0) {
        if (log.isDebugEnabled()) {
            log.debug("New object is larger than or equal to the largest");
        }
        return;
    }
    // obj is less than the largest.
    insert(obj);
    return;
}

From source file:de.tudarmstadt.ukp.csniper.webapp.evaluation.SortableEvaluationResultDataProvider.java

private void updateView() {
    final SortParam<String> sp = getSort();

    if (!sp.getProperty().equals(lastSortProperty) || (lastSortOrder != sp.isAscending()) || filterChanged) {
        // Apply filter
        if (getFilter() != ResultFilter.ALL) {
            limitedResults = new ArrayList<EvaluationResult>();
            for (EvaluationResult e : results) {
                Mark emark = Mark.fromString(e.getResult());
                switch (getFilter()) {
                case ANNOTATED:
                    if (emark == Mark.CORRECT || emark == Mark.WRONG) {
                        limitedResults.add(e);
                    }/*  w  w w . j a  v  a 2 s  .  c o  m*/
                    break;
                case TO_CHECK:
                    if (emark == Mark.CHECK) {
                        limitedResults.add(e);
                    }
                    break;
                case TODO:
                    if (StringUtils.isBlank(e.getResult()) || emark == Mark.PRED_CORRECT
                            || emark == Mark.PRED_WRONG) {
                        limitedResults.add(e);
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unknown filter setting");
                }
            }
        } else {
            limitedResults = results;
        }

        // Apply sorting
        Collections.sort(limitedResults, new Comparator<EvaluationResult>() {
            @SuppressWarnings("rawtypes")
            @Override
            public int compare(EvaluationResult aO1, EvaluationResult aO2) {
                try {
                    Comparable v1 = (Comparable) PropertyUtils.getNestedProperty(aO1, sp.getProperty());
                    Comparable v2 = (Comparable) PropertyUtils.getNestedProperty(aO2, sp.getProperty());

                    if (v1 == null) {
                        if (v2 == null) {
                            return 0;
                        }
                        return sp.isAscending() ? -1 : 1;
                    }
                    if (v2 == null) {
                        return sp.isAscending() ? 1 : -1;
                    }
                    return sp.isAscending() ? v1.compareTo(v2) : v2.compareTo(v1);
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        });

        lastSortProperty = sp.getProperty();
        lastSortOrder = sp.isAscending();
        filterChanged = false;
    }
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * Checks to see if the interval defined as [s,e)
 * is entirely contained within this Range object.
 * //  www  . ja  v  a2 s . c o  m
 * @param s
 * @param e
 * @return boolean
 */
public boolean withinRange(Comparable s, Comparable e) {
    SortedMap m = spans.headMap(e);
    if (!m.isEmpty()) {
        // thankfully Range keeps contiguous spans merged,
        // so this is easy
        Comparable start = (Comparable) m.lastKey();
        Comparable end = (Comparable) m.get(start);
        if (end.compareTo(s) >= 0) {
            return start.compareTo(s) <= 0 && end.compareTo(e) >= 0;
        }
    }
    m = spans.tailMap(s);
    if (!m.isEmpty()) {
        Comparable sPrime = (Comparable) m.firstKey();
        if (sPrime.compareTo(s) == 0) {
            return e.compareTo((Comparable) m.get(sPrime)) <= 0;
        }
    }
    return false;
}

From source file:org.kuali.test.runner.execution.AbstractOperationExecution.java

/**
 * //from w w  w  .  j a  v a  2  s .  c  o  m
 * @param testWrapper
 * @param cp
 * @return
 * @throws TestException 
 */
protected boolean evaluateCheckpointProperty(KualiTestWrapper testWrapper, CheckpointProperty cp)
        throws TestException {
    boolean retval = false;

    try {
        Object comparisonValue = getComparisonValue(cp);
        ComparisonOperator.Enum comparisonOperator = cp.getOperator();
        Object value = getValueForType(cp.getActualValue(), cp.getValueType());

        if (ComparisonOperator.NULL.equals(cp.getOperator())) {
            retval = ((value == null) && (comparisonValue == null));
        } else if ((value == null) || (comparisonValue == null)) {
            if (((cp.getOperator() == null) || ComparisonOperator.EQUAL_TO.equals(cp.getOperator()))
                    && (value == null) && (comparisonValue == null)) {
                retval = true;
            } else {
                throw new TestException("input value is null, comparison value = " + comparisonValue, op,
                        cp.getOnFailure());
            }
        } else {
            ValueType.Enum type = cp.getValueType();

            if (type == null) {
                type = ValueType.STRING;
            }

            ValueType.Enum inputType = getInputValueType(value);
            if (type.equals(inputType)) {
                if (ComparisonOperator.IN.equals(cp.getOperator()) && (comparisonValue instanceof List)) {
                    Iterator<Comparable> it = ((List) comparisonValue).iterator();

                    while (it.hasNext()) {
                        if (it.next().equals(value)) {
                            retval = true;
                            break;
                        }
                    }
                } else {
                    if (ValueType.STRING.equals(type)) {
                        String s1 = (String) comparisonValue;
                        String s2 = (String) value;
                        if (StringUtils.isNotBlank(s1)) {
                            comparisonValue = s1.trim();
                        }

                        if (StringUtils.isNotBlank(s2)) {
                            value = s2.trim();
                        }
                    }
                    Comparable c1 = (Comparable) comparisonValue;
                    Comparable c2 = (Comparable) value;

                    switch (comparisonOperator.intValue()) {
                    case ComparisonOperator.INT_EQUAL_TO:
                        retval = c1.equals(c2);
                        break;
                    case ComparisonOperator.INT_GREATER_THAN:
                        retval = (c1.compareTo(c2) < 0);
                        break;
                    case ComparisonOperator.INT_GREATER_THAN_OR_EQUAL:
                        retval = (c1.compareTo(c2) <= 0);
                        break;
                    case ComparisonOperator.INT_LESS_THAN:
                        retval = (c1.compareTo(c2) > 0);
                        break;
                    case ComparisonOperator.INT_LESS_THAN_OR_EQUAL:
                        retval = (c1.compareTo(c2) >= 0);
                        break;
                    case ComparisonOperator.INT_BETWEEN:

                        break;
                    case ComparisonOperator.INT_NOT_NULL:
                        retval = true;
                        break;
                    }
                }
            } else {
                throw new TestException("input type (" + inputType + ") comparison type (" + type + ") mismatch"
                        + comparisonValue, op, cp.getOnFailure());
            }
        }
    }

    catch (ParseException ex) {
        throw new TestException(
                "Exception occurred while parsing data for checkpoint comparison - " + ex.toString(), op, ex);
    }

    return retval;
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * @see java.util.Set#contains(java.lang.Object)
 */// w w w .jav  a  2s  .co m
public boolean contains(Object o) {
    if (spans.size() == 0) {
        return false;
    } else if (o instanceof Comparable) {
        SortedMap m = spans.headMap(o);
        if (m.size() > 0) {
            Comparable e = (Comparable) m.get(m.lastKey());
            if (e.compareTo(o) > 0) {
                return true;
            }
        }
        m = spans.tailMap(o);
        if (m.size() > 0) {
            Comparable s = (Comparable) m.firstKey();
            return s.compareTo(o) == 0;
        }
        return false;
    } else {
        return withinRange((Interval) o);
    }
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * @see edu.umd.cfar.lamp.viper.util.IntervalIndexList#endOf(java.lang.Comparable)
 *//*www.j a  va  2  s  . co m*/
public Comparable endOf(Comparable c) {
    if (c == null) {
        return null;
    }
    Object o = spans.get(c);
    if (o == null) {
        SortedMap head = spans.headMap(c);
        if (head != null && !head.isEmpty()) {
            Comparable last = (Comparable) spans.get(head.lastKey());
            if (last.compareTo(c) >= 0) {
                return last;
            }
        }
        return null;
    } else {
        return (Comparable) o;
    }
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * @see edu.umd.cfar.lamp.viper.util.IntervalIndexList#remove(java.lang.Comparable, java.lang.Comparable)
 *///from  w  w w.  j  a v a  2 s  .c o m
public boolean remove(Comparable start, Comparable end) {
    boolean someFound = false;
    SortedMap head = spans.headMap(start);
    if (!head.isEmpty()) {
        Comparable oldStart = (Comparable) head.lastKey();
        Comparable oldEnd = (Comparable) head.get(oldStart);
        if (oldEnd.compareTo(start) > 0) {
            // if there is a span that goes into the span to
            // be removed, replace it.
            head.put(oldStart, start);
            someFound = true;
            double toCheck = oldEnd.compareTo(end);
            if (toCheck > 0) {
                // if the span to be removed is a strict subset 
                // of some existing span, you also have
                // to add back the end.
                spans.put(end, oldEnd);
                return true;
            } else if (toCheck == 0) {
                return true;
            }
        }
    }
    SortedMap sub = spans.subMap(start, end);
    if (!sub.isEmpty()) {
        someFound = true;
        Comparable oldStart = (Comparable) sub.lastKey();
        Comparable oldEnd = (Comparable) sub.get(oldStart);
        if (oldEnd.compareTo(end) > 0) {
            // if there is a span that starts during the
            // span to removed that goes past the end,
            // have to add back the difference.
            spans.put(end, oldEnd);
        }
        sub.clear();
    }
    return someFound;
}

From source file:edu.umd.cfar.lamp.viper.util.Range.java

/**
 * Subsumes the Instants in the Span into this Range. 
 * @param start the first instant to add
 * @param stop the stop instant, exclusive
 * @return <code>true</code> iff the operation modified this Range
 *//*  w w w.j av a  2  s. co  m*/
public boolean add(Comparable start, Comparable stop) {
    Comparable old = (Comparable) spans.get(start);
    if (old != null && old.compareTo(stop) >= 0) {
        return false;
    }
    SortedMap head = spans.headMap(start);
    if (!head.isEmpty()) {
        Comparable oldStart = (Comparable) head.lastKey();
        Comparable oldEnd = (Comparable) head.get(oldStart);
        if (oldEnd.compareTo(stop) >= 0) {
            return false;
        } else {
            if (oldEnd.compareTo(start) >= 0) {
                start = oldStart;
                spans.remove(oldStart);
            }
        }
    }
    SortedMap sub = spans.subMap(start, stop);
    if (!sub.isEmpty()) {
        Comparable oldStart = (Comparable) sub.lastKey();
        Comparable oldEnd = (Comparable) sub.get(oldStart);
        if (oldStart.compareTo(start) == 0 && oldEnd.compareTo(stop) >= 0) {
            return false;
        } else if (oldEnd.compareTo(stop) > 0) {
            stop = oldEnd;
        }
        sub.clear();
    }
    if (spans.containsKey(stop)) {
        stop = (Comparable) spans.remove(stop);
    }
    spans.put(start, stop);
    return true;
}

From source file:$.java

/**
     * Decision helper used when handling ajax autoComplete event and regular page postback.
     *//*from   w w w.j ava 2s.c om*/
    public boolean shouldReplace(E currentEntity, E selectedEntity) {
        if (currentEntity == selectedEntity) {
            return false;
        }

        if (currentEntity != null && selectedEntity != null && currentEntity.isIdSet()
                && selectedEntity.isIdSet()) {
            if (selectedEntity.getId().equals(currentEntity.getId())) {
                Comparable<Object> currentVersion = repository.getVersion(currentEntity);
                if (currentVersion == null) {
                    // assume no version at all is available
                    // let's stick with current entity.
                    return false;
                }
                Comparable<Object> selectedVersion = repository.getVersion(selectedEntity);
                if (currentVersion.compareTo(selectedVersion) == 0) {
                    // currentEntity could have been edited and not yet saved, we keep it.
                    return false;
                } else {
                    // we could have an optimistic locking exception at save time
                    // TODO: what should we do here?
                    return false;
                }
            }
        }
        return true;
    }