Search in sources :

Example 1 with StringValueComparator

use of org.knime.base.util.StringValueComparator in project knime-core by knime.

the class AccuracyScorerNodeModel method sort.

/**
 * @param order The cells to sort.
 */
private void sort(final DataCell[] order) {
    if (order.length == 0) {
        return;
    }
    DataType type = order[0].getType();
    for (DataCell dataCell : order) {
        type = DataType.getCommonSuperType(type, dataCell.getType());
    }
    final Comparator<DataCell> comparator;
    switch(m_sortingStrategy) {
        case InsertionOrder:
            if (m_sortingReversed) {
                reverse(order);
            }
            return;
        case Unsorted:
            return;
        case Lexical:
            if (StringCell.TYPE.isASuperTypeOf(type)) {
                Comparator<String> stringComparator;
                Collator instance = Collator.getInstance();
                // do not try to combine characters
                instance.setDecomposition(Collator.NO_DECOMPOSITION);
                // case and accents matter.
                instance.setStrength(Collator.IDENTICAL);
                @SuppressWarnings("unchecked") Comparator<String> collator = (Comparator<String>) (Comparator<?>) instance;
                stringComparator = collator;
                comparator = new StringValueComparator(stringComparator);
            } else if (DoubleCell.TYPE.isASuperTypeOf(type)) {
                comparator = new DataValueComparator() {

                    @Override
                    protected int compareDataValues(final DataValue v1, final DataValue v2) {
                        String s1 = v1.toString();
                        String s2 = v2.toString();
                        return s1.compareTo(s2);
                    }
                };
            } else {
                throw new IllegalStateException("Lexical sorting strategy is not supported.");
            }
            break;
        case Numeric:
            if (DoubleCell.TYPE.isASuperTypeOf(type)) {
                comparator = type.getComparator();
            } else {
                throw new IllegalStateException("Numerical sorting strategy is not supported.");
            }
            break;
        default:
            throw new IllegalStateException("Unrecognized sorting strategy: " + m_sortingStrategy);
    }
    Arrays.sort(order, comparator);
    if (m_sortingReversed) {
        reverse(order);
    }
}
Also used : DataValue(org.knime.core.data.DataValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DataValueComparator(org.knime.core.data.DataValueComparator) Collator(java.text.Collator) StringValueComparator(org.knime.base.util.StringValueComparator) DataValueComparator(org.knime.core.data.DataValueComparator) Comparator(java.util.Comparator) StringValueComparator(org.knime.base.util.StringValueComparator)

Aggregations

Collator (java.text.Collator)1 Comparator (java.util.Comparator)1 StringValueComparator (org.knime.base.util.StringValueComparator)1 DataCell (org.knime.core.data.DataCell)1 DataType (org.knime.core.data.DataType)1 DataValue (org.knime.core.data.DataValue)1 DataValueComparator (org.knime.core.data.DataValueComparator)1