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.haulmont.cuba.gui.components.validators.RangeValidator.java

protected int getCompareValue(Comparable src, Comparable to) {
    if (String.class.isAssignableFrom(type) && src instanceof String && isIgnoreCase()) {
        return ((String) src).compareToIgnoreCase((String) to);
    }/*from  www  .j  ava2s. c o m*/
    return src.compareTo(to);
}

From source file:com.pureinfo.srm.reports.table.data.sci.SCIBySubjectOnlyStatistic.java

/**
 * @see com.pureinfo.srm.reports.table.data.institute.InstituteReportBase#buildDatas(boolean,
 *      boolean)/*ww  w . j  av a  2 s  . c o m*/
 */
public Object[][] buildDatas(boolean _bAscOrder, boolean _bOrderByValue) throws PureException {
    IObjects nums = doQuery();
    DolphinObject num = null;
    Map hDatas = new HashedMap();
    boolean hasOther = false;
    while ((num = nums.next()) != null) {
        String subest = num.getStrProperty("_SUB");
        if (subest == null || subest.trim().length() == 0)
            continue;
        String[] subs = subest.split(";");
        String sSchool = num.getStrProperty("_SCH");
        int nNum = num.getIntProperty("_NUM", 0);
        for (int i = 0; i < subs.length; i++) {
            if (nNum < 1)
                continue;
            String sSubject = subs[i].trim();
            Object[] hSubjectDatas = (Object[]) hDatas.get(sSubject);
            if (hSubjectDatas == null) {
                hDatas.put(sSubject, hSubjectDatas = new Object[SCHOOLS.size() * 2]);
                hSubjectDatas[0] = sSubject;
                for (int j = 1; j < hSubjectDatas.length; j++) {
                    hSubjectDatas[j] = new Integer(0);
                }
            }
            Integer idxSchool = (Integer) SCHOOLS.get(sSchool);
            if (idxSchool == null)
                hasOther = true;
            int idxData = (idxSchool == null ? SCHOOLS.size() : idxSchool.intValue()) + 1;
            Integer odValue = (Integer) hSubjectDatas[idxData];
            int sum = odValue == null ? nNum : (nNum + odValue.intValue());
            hSubjectDatas[idxData] = new Integer(sum);
        }
    }

    List l = new ArrayList(hDatas.size());

    for (Iterator iter = hDatas.entrySet().iterator(); iter.hasNext();) {
        Map.Entry en = (Map.Entry) iter.next();
        Object[] hSubjectDatas = (Object[]) en.getValue();
        if (!hasOther) {
            Object[] line = new Object[SCHOOLS.size() + 1];
            System.arraycopy(hSubjectDatas, 0, line, 0, line.length);
            hSubjectDatas = line;
        }
        l.add(hSubjectDatas);
    }

    Collections.sort(l, new Comparator() {
        public int compare(Object _sO1, Object _sO2) {
            String sOrder = getParamValue(PARAM_NAME_ORDER, 0);
            boolean orderValid = sOrder != null && sOrder.matches("[0-9]+");
            int orderIdx = orderValid ? Integer.parseInt(sOrder) : 0;
            Object[] arr1 = (Object[]) _sO1;
            Object[] arr2 = (Object[]) _sO2;
            if (orderIdx < 0 || orderIdx >= arr1.length) {
                orderIdx = 0;
            }
            Comparable s1 = (Comparable) arr1[orderIdx];
            Comparable s2 = (Comparable) arr2[orderIdx];
            int nComp = s1.compareTo(s2);
            if (nComp == 0) {
                Comparable ss1 = (Comparable) arr1[0];
                Comparable ss2 = (Comparable) arr2[0];
                return ss1.compareTo(ss2);
            }
            return nComp;
        }
    });
    Object[][] datas = new Object[l.size() + 1][];
    datas[0] = new Object[(hasOther ? 1 : 0) + SCHOOLS.size() + 1];
    int j = 1;
    datas[0][0] = "";
    for (Iterator iter = SCHOOLS.keySet().iterator(); iter.hasNext();) {
        datas[0][j++] = iter.next();

    }
    if (hasOther) {
        datas[0][j++] = "";
    }
    int rowIdx = 1;
    for (Iterator iter = l.iterator(); iter.hasNext();) {
        datas[rowIdx++] = (Object[]) iter.next();

    }
    nums.clear();
    return datas;
}

From source file:com.mirth.connect.client.ui.components.rsta.ac.MirthFunctionCompletion.java

private <T> int compare(Comparable<T> obj1, T obj2) {
    if (obj1 == null) {
        if (obj2 == null) {
            return 0;
        } else {/*from   w ww .  j a  v a  2 s .co m*/
            return -1;
        }
    } else if (obj2 == null) {
        return 1;
    } else {
        return obj1.compareTo(obj2);
    }
}

From source file:org.betaconceptframework.astroboa.engine.jcr.query.CmsScoreNode.java

private int compareValues(Comparable value1, Comparable value2) throws RepositoryException {

    if (value1 == null) {
        return (value2 == null ? 0 : -1);
    } else {// ww  w  .  j a  v  a2  s  . c  o m
        return (value2 != null ? value1.compareTo(value2) : 1);
    }

}

From source file:com.jd.survey.domain.survey.QuestionAnswerRowLabel.java

@Override
public int compareTo(QuestionAnswerRowLabel that) {
    final int BEFORE = -1;
    final int AFTER = 1;
    if (that == null) {
        return BEFORE;
    }//from  w ww.j a v a  2s  .c om
    Comparable<Short> thisQuestionAnswerRowLabel = this.getOrder();
    Comparable<Short> thatQuestionAnswerRowLabel = that.getOrder();
    if (thisQuestionAnswerRowLabel == null) {
        return AFTER;
    } else if (thatQuestionAnswerRowLabel == null) {
        return BEFORE;
    } else {
        return thisQuestionAnswerRowLabel.compareTo(that.getOrder());
    }
}

From source file:com.jd.survey.domain.survey.QuestionAnswerOption.java

@Override
public int compareTo(QuestionAnswerOption that) {
    final int BEFORE = -1;
    final int AFTER = 1;
    if (that == null) {
        return BEFORE;
    }// w  w  w .  j  av  a2  s .  c o  m
    Comparable<Short> thisQuestionAnswerOption = this.getOrder();
    Comparable<Short> thatQuestionAnswerOption = that.getOrder();
    if (thisQuestionAnswerOption == null) {
        return AFTER;
    } else if (thatQuestionAnswerOption == null) {
        return BEFORE;
    } else {
        return thisQuestionAnswerOption.compareTo(that.getOrder());
    }
}

From source file:com.jd.survey.domain.survey.QuestionAnswerColumnLabel.java

@Override
public int compareTo(QuestionAnswerColumnLabel that) {
    final int BEFORE = -1;
    final int AFTER = 1;
    if (that == null) {
        return BEFORE;
    }// w  w w . java2s  . c  om
    Comparable<Short> thisQuestionAnswerColumnLabel = this.getOrder();
    Comparable<Short> thatQuestionAnswerColumnLabel = that.getOrder();
    if (thisQuestionAnswerColumnLabel == null) {
        return AFTER;
    } else if (thatQuestionAnswerColumnLabel == null) {
        return BEFORE;
    } else {
        return thisQuestionAnswerColumnLabel.compareTo(that.getOrder());
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentSerializer.java

private <T extends Document> void applyUpdate(T doc, List updateString, List<Object> op) {
    String opcode = op.get(0).toString();
    String key = op.get(1).toString();
    Revision rev = null;//  www  .  j av a 2  s  .c o m
    Object value = null;
    if (op.size() == 3) {
        value = op.get(2);
    } else {
        rev = Revision.fromString(op.get(2).toString());
        value = op.get(3);
    }
    Object old = doc.get(key);

    if ("=".equals(opcode)) {
        if (rev == null) {
            doc.put(key, value);
        } else {
            @SuppressWarnings("unchecked")
            Map<Revision, Object> m = (Map<Revision, Object>) old;
            if (m == null) {
                m = new TreeMap<Revision, Object>(comparator);
                doc.put(key, m);
            }
            m.put(rev, value);
        }
    } else if ("*".equals(opcode)) {
        if (rev == null) {
            throw new DocumentStoreException("unexpected operation " + op + " in: " + updateString);
        } else {
            @SuppressWarnings("unchecked")
            Map<Revision, Object> m = (Map<Revision, Object>) old;
            if (m != null) {
                m.remove(rev);
            }
        }
    } else if ("+".equals(opcode)) {
        if (rev == null) {
            Long x = (Long) value;
            if (old == null) {
                old = 0L;
            }
            doc.put(key, ((Long) old) + x);
        } else {
            throw new DocumentStoreException("unexpected operation " + op + " in: " + updateString);
        }
    } else if ("M".equals(opcode)) {
        if (rev == null) {
            Comparable newValue = (Comparable) value;
            if (old == null || newValue.compareTo(old) > 0) {
                doc.put(key, value);
            }
        } else {
            throw new DocumentStoreException("unexpected operation " + op + " in: " + updateString);
        }
    } else {
        throw new DocumentStoreException("unexpected operation " + op + " in: " + updateString);
    }
}

From source file:com.centurylink.mdw.plugin.designer.properties.value.DefaultViewerComparator.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public int compare(Viewer viewer, Object element1, Object element2) {
    try {//from  w ww  .j  a  va  2 s . co m
        Comparable c1 = (Comparable) propUtilsBean.getProperty(element1, property);
        Comparable c2 = (Comparable) propUtilsBean.getProperty(element2, property);
        if (sortDirectionProvider.getSortDirection() == SWT.UP) {
            if (c1 == null)
                return -1;
            else if (c2 == null)
                return 1;
            return c1.compareTo(c2);
        } else {
            if (c2 == null)
                return -1;
            else if (c1 == null)
                return 1;
            return c2.compareTo(c1);
        }
    } catch (Exception ex) {
        PluginMessages.log(ex);
        return 0;
    }
}

From source file:imgb64.StringEncoderComparator.java

/**
 * Compares two strings based not on the strings themselves, but on an encoding of the two strings using the
 * StringEncoder this Comparator was created with.
 *
 * If an {@link EncoderException} is encountered, return <code>0</code>.
 *
 * @param o1/*from   ww  w .j ava  2  s .c om*/
 *            the object to compare
 * @param o2
 *            the object to compare to
 * @return the Comparable.compareTo() return code or 0 if an encoding error was caught.
 * @see Comparable
 */
@Override
public int compare(final Object o1, final Object o2) {

    int compareCode = 0;

    try {
        @SuppressWarnings("unchecked") // May fail with CCE if encode returns something that is not Comparable
        // However this was always the case.
        final Comparable<Comparable<?>> s1 = (Comparable<Comparable<?>>) this.stringEncoder.encode(o1);
        final Comparable<?> s2 = (Comparable<?>) this.stringEncoder.encode(o2);
        compareCode = s1.compareTo(s2);
    } catch (final EncoderException ee) {
        compareCode = 0;
    }
    return compareCode;
}