Search in sources :

Example 41 with Collator

use of java.text.Collator 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)

Example 42 with Collator

use of java.text.Collator in project eclipse.platform.swt by eclipse.

the class MJ_Table method sort_by_column_Snippet2.

@Test
public void sort_by_column_Snippet2() {
    Shell shell = mkShell("Click on columns to verify items are sorted properly");
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.BORDER);
    table.setHeaderVisible(true);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    TableItem item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "a", "3" });
    item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "b", "2" });
    item = new TableItem(table, SWT.NONE);
    item.setText(new String[] { "c", "1" });
    column1.setWidth(100);
    column2.setWidth(100);
    Listener sortListener = e -> {
        TableItem[] items = table.getItems();
        Collator collator = Collator.getInstance(Locale.getDefault());
        TableColumn column = (TableColumn) e.widget;
        int index = column == column1 ? 0 : 1;
        for (int i = 1; i < items.length; i++) {
            String value1 = items[i].getText(index);
            for (int j = 0; j < i; j++) {
                String value2 = items[j].getText(index);
                if (collator.compare(value1, value2) < 0) {
                    String[] values = { items[i].getText(0), items[i].getText(1) };
                    items[i].dispose();
                    TableItem item1 = new TableItem(table, SWT.NONE, j);
                    item1.setText(values);
                    items = table.getItems();
                    break;
                }
            }
        }
        table.setSortColumn(column);
    };
    column1.addListener(SWT.Selection, sortListener);
    column2.addListener(SWT.Selection, sortListener);
    table.setSortColumn(column1);
    table.setSortDirection(SWT.UP);
    shell.setSize(SWIDTH, SHEIGHT);
    shell.open();
    mainLoop(shell);
}
Also used : TableEditor(org.eclipse.swt.custom.TableEditor) MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) TableColumn(org.eclipse.swt.widgets.TableColumn) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FontMetrics(org.eclipse.swt.graphics.FontMetrics) Image(org.eclipse.swt.graphics.Image) Rectangle(org.eclipse.swt.graphics.Rectangle) Random(java.util.Random) ControlListener(org.eclipse.swt.events.ControlListener) TextLayout(org.eclipse.swt.graphics.TextLayout) TextStyle(org.eclipse.swt.graphics.TextStyle) Table(org.eclipse.swt.widgets.Table) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point) Event(org.eclipse.swt.widgets.Event) ControlEvent(org.eclipse.swt.events.ControlEvent) Locale(java.util.Locale) Composite(org.eclipse.swt.widgets.Composite) Listener(org.eclipse.swt.widgets.Listener) BiConsumer(java.util.function.BiConsumer) Font(org.eclipse.swt.graphics.Font) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) RowData(org.eclipse.swt.layout.RowData) Collator(java.text.Collator) TableItem(org.eclipse.swt.widgets.TableItem) Text(org.eclipse.swt.widgets.Text) Shell(org.eclipse.swt.widgets.Shell) Button(org.eclipse.swt.widgets.Button) ShellEvent(org.eclipse.swt.events.ShellEvent) Test(org.junit.Test) ShellAdapter(org.eclipse.swt.events.ShellAdapter) Consumer(java.util.function.Consumer) RowLayout(org.eclipse.swt.layout.RowLayout) Color(org.eclipse.swt.graphics.Color) ControlAdapter(org.eclipse.swt.events.ControlAdapter) SWT(org.eclipse.swt.SWT) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ScrollBar(org.eclipse.swt.widgets.ScrollBar) FixMethodOrder(org.junit.FixMethodOrder) Label(org.eclipse.swt.widgets.Label) SelectionListener(org.eclipse.swt.events.SelectionListener) GridLayout(org.eclipse.swt.layout.GridLayout) Shell(org.eclipse.swt.widgets.Shell) Table(org.eclipse.swt.widgets.Table) ControlListener(org.eclipse.swt.events.ControlListener) Listener(org.eclipse.swt.widgets.Listener) SelectionListener(org.eclipse.swt.events.SelectionListener) TableItem(org.eclipse.swt.widgets.TableItem) FillLayout(org.eclipse.swt.layout.FillLayout) TableColumn(org.eclipse.swt.widgets.TableColumn) Collator(java.text.Collator) Test(org.junit.Test)

Example 43 with Collator

use of java.text.Collator in project OpenAM by OpenRock.

the class AMFormatUtils method sortItems.

/**
     * Sorts items in a set.
     *
     * @param collection to sort.
     * @param locale of user.
     * @return list of sorted items.
     */
public static List sortItems(Collection collection, Locale locale) {
    List sorted = Collections.EMPTY_LIST;
    if ((collection != null) && !collection.isEmpty()) {
        sorted = new ArrayList(collection);
        Collator collator = Collator.getInstance(locale);
        Collections.sort(sorted, collator);
    }
    return sorted;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) OptionList(com.iplanet.jato.view.html.OptionList) List(java.util.List) Collator(java.text.Collator)

Example 44 with Collator

use of java.text.Collator in project OpenAM by OpenRock.

the class SubConfigMeta method getSubConfigurationsFromConfig.

private void getSubConfigurationsFromConfig() {
    try {
        String[] params = { serviceName, parentConfig.getComponentName() };
        amModel.logEvent("ATTEMPT_READ_GLOBAL_SUB_CONFIGURATION_NAMES", params);
        Set names = parentConfig.getSubConfigNames();
        if ((names != null) && !names.isEmpty()) {
            Collator collator = Collator.getInstance(amModel.getUserLocale());
            SortedSet set = new TreeSet(new SMSubConfigComparator(collator));
            for (Iterator iter = names.iterator(); iter.hasNext(); ) {
                String name = (String) iter.next();
                ServiceConfig conf = parentConfig.getSubConfig(name);
                String schemaID = conf.getSchemaID();
                if (globalSubSchemaNames.contains(schemaID)) {
                    String displayType = (String) mapServiceSchemaNameToL10NName.get(schemaID);
                    String localizedName = getLocalizedString(CollectionHelper.getMapAttr(conf.getAttributes(), SMSUtils.I18N_KEY));
                    ServiceSchema schema = corrSchema.getSubSchema(schemaID);
                    set.add(new SMSubConfig(conf.getComponentName(), name, displayType, localizedName, schema.isHiddenInConfigUI()));
                    if (singleInstanceGlobalSubSchemas.contains(schemaID)) {
                        creatableGlobalSubSchemas.remove(schemaID);
                    }
                }
            }
            if (!set.isEmpty()) {
                globalSubConfigurations = new ArrayList(set.size());
                globalSubConfigurations.addAll(set);
            }
        }
        amModel.logEvent("SUCCEED_READ_GLOBAL_SUB_CONFIGURATION_NAMES", params);
    } catch (SSOException e) {
        String[] paramsEx = { serviceName, parentConfig.getComponentName(), amModel.getErrorString(e) };
        amModel.logEvent("SSO_EXCEPTION_READ_GLOBAL_SUB_CONFIGURATION_NAMES", paramsEx);
        AMModelBase.debug.error("SubConfigMeta.getSubConfigurations", e);
    } catch (SMSException e) {
        String[] paramsEx = { serviceName, parentConfig.getComponentName(), amModel.getErrorString(e) };
        amModel.logEvent("SMS_EXCEPTION_READ_GLOBAL_SUB_CONFIGURATION_NAMES", paramsEx);
        AMModelBase.debug.error("SubConfigMeta.getSubConfigurations", e);
    }
}
Also used : SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) SMSException(com.sun.identity.sm.SMSException) ArrayList(java.util.ArrayList) SSOException(com.iplanet.sso.SSOException) SortedSet(java.util.SortedSet) Collator(java.text.Collator) ServiceSchema(com.sun.identity.sm.ServiceSchema) ServiceConfig(com.sun.identity.sm.ServiceConfig) TreeSet(java.util.TreeSet) Iterator(java.util.Iterator)

Example 45 with Collator

use of java.text.Collator in project Klyph by jonathangerbaud.

the class GraphObjectAdapter method rebuildSections.

private void rebuildSections() {
    sectionKeys = new ArrayList<String>();
    graphObjectsBySection = new HashMap<String, ArrayList<T>>();
    graphObjectsById = new HashMap<String, T>();
    displaySections = false;
    if (cursor == null || cursor.getCount() == 0) {
        return;
    }
    int objectsAdded = 0;
    cursor.moveToFirst();
    do {
        T graphObject = cursor.getGraphObject();
        if (!filterIncludesItem(graphObject)) {
            continue;
        }
        objectsAdded++;
        String sectionKeyOfItem = getSectionKeyOfGraphObject(graphObject);
        if (!graphObjectsBySection.containsKey(sectionKeyOfItem)) {
            sectionKeys.add(sectionKeyOfItem);
            graphObjectsBySection.put(sectionKeyOfItem, new ArrayList<T>());
        }
        List<T> section = graphObjectsBySection.get(sectionKeyOfItem);
        section.add(graphObject);
        graphObjectsById.put(getIdOfGraphObject(graphObject), graphObject);
    } while (cursor.moveToNext());
    if (sortFields != null) {
        final Collator collator = Collator.getInstance();
        for (List<T> section : graphObjectsBySection.values()) {
            Collections.sort(section, new Comparator<GraphObject>() {

                @Override
                public int compare(GraphObject a, GraphObject b) {
                    return compareGraphObjects(a, b, sortFields, collator);
                }
            });
        }
    }
    Collections.sort(sectionKeys, Collator.getInstance());
    displaySections = sectionKeys.size() > 1 && objectsAdded > DISPLAY_SECTIONS_THRESHOLD;
}
Also used : GraphObject(com.facebook.model.GraphObject) Collator(java.text.Collator)

Aggregations

Collator (java.text.Collator)70 ArrayList (java.util.ArrayList)14 RuleBasedCollator (java.text.RuleBasedCollator)8 HashMap (java.util.HashMap)7 Comparator (java.util.Comparator)5 List (java.util.List)5 GraphObject (com.facebook.model.GraphObject)4 Context (android.content.Context)3 InputMethodInfo (android.view.inputmethod.InputMethodInfo)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 Locale (java.util.Locale)3 Entry (java.util.Map.Entry)3 SharedPreferences (android.content.SharedPreferences)2 ApplicationInfo (android.content.pm.ApplicationInfo)2 PackageInfo (android.content.pm.PackageInfo)2 DBObject (com.mongodb.DBObject)2 SMSException (com.sun.identity.sm.SMSException)2 ServiceSchema (com.sun.identity.sm.ServiceSchema)2 AutoPilot (com.ximpleware.AutoPilot)2