Example usage for java.text Collator compare

List of usage examples for java.text Collator compare

Introduction

In this page you can find the example usage for java.text Collator compare.

Prototype

@Override
public int compare(Object o1, Object o2) 

Source Link

Document

Compares its two arguments for order.

Usage

From source file:com.android.leanlauncher.LauncherModel.java

public static Comparator<AppInfo> getAppNameComparator() {
    final Collator collator = Collator.getInstance();
    return new Comparator<AppInfo>() {
        public final int compare(AppInfo a, AppInfo b) {
            if (a.user.equals(b.user)) {
                int result = a.componentName.compareTo(b.componentName);
                if (a.title == null || b.title == null) {
                    return result;
                }//www.  j  a v  a2s  .c  o m
                int titleCompareResult = collator.compare(a.title.toString().trim(), b.title.toString().trim());
                if (titleCompareResult != 0) {
                    result = titleCompareResult;
                }

                return result;
            } else {
                // TODO Need to figure out rules for sorting
                // profiles, this puts work second.
                return a.user.toString().compareTo(b.user.toString());
            }
        }
    };
}

From source file:org.jumpmind.db.platform.AbstractJdbcDdlReader.java

protected Collection<Table> readTables(Connection connection, String catalog, String schemaPattern,
        String[] tableTypes) throws SQLException {
    ResultSet tableData = null;//w w  w .j a va  2  s .  com
    try {
        DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper();

        metaData.setMetaData(connection.getMetaData());
        metaData.setCatalog(catalog == null ? getDefaultCatalogPattern() : catalog);
        metaData.setSchemaPattern(schemaPattern == null ? getDefaultSchemaPattern() : schemaPattern);
        metaData.setTableTypes(
                (tableTypes == null) || (tableTypes.length == 0) ? getDefaultTableTypes() : tableTypes);

        tableData = metaData.getTables(getDefaultTablePattern());

        List<Table> tables = new ArrayList<Table>();

        while (tableData.next()) {
            Map<String, Object> values = readMetaData(tableData, getColumnsForTable());
            Table table = readTable(connection, metaData, values);

            if (table != null) {
                tables.add(table);
            }
        }

        final Collator collator = Collator.getInstance();

        Collections.sort(tables, new Comparator<Table>() {
            public int compare(Table obj1, Table obj2) {
                return collator.compare(obj1.getName().toUpperCase(), obj2.getName().toUpperCase());
            }
        });

        return tables;
    } finally {
        if (tableData != null) {
            tableData.close();
        }
    }
}

From source file:org.alfresco.repo.jscript.People.java

private List<NodeRef> getSortedPeopleObjects(List<NodeRef> peopleRefs, final String sortBy, Boolean sortAsc) {
    if (sortBy == null) {
        return peopleRefs;
    }/*w  w  w  .j a  v  a  2  s.co  m*/

    //make copy of peopleRefs because it can be unmodifiable list.
    List<NodeRef> sortedPeopleRefs = new ArrayList<NodeRef>(peopleRefs);
    final Collator col = Collator.getInstance(I18NUtil.getLocale());
    final NodeService nodeService = services.getNodeService();
    final int orderMultiplicator = ((sortAsc == null) || sortAsc) ? 1 : -1;
    Collections.sort(sortedPeopleRefs, new Comparator<NodeRef>() {
        @Override
        public int compare(NodeRef n1, NodeRef n2) {
            Serializable p1 = getProperty(n1);
            Serializable p2 = getProperty(n2);

            if ((p1 instanceof Long) && (p2 instanceof Long)) {
                return Long.compare((Long) p1, (Long) p2) * orderMultiplicator;
            }

            return col.compare(p1.toString(), p2) * orderMultiplicator;
        }

        public Serializable getProperty(NodeRef nodeRef) {
            Serializable result;

            if ("fullName".equalsIgnoreCase(sortBy)) {
                String firstName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_FIRSTNAME);
                String lastName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LASTNAME);
                String fullName = firstName;
                if (lastName != null && lastName.length() > 0) {
                    fullName = fullName + " " + lastName;
                }

                result = fullName;
            } else if ("jobtitle".equalsIgnoreCase(sortBy)) {
                result = nodeService.getProperty(nodeRef, ContentModel.PROP_JOBTITLE);
            } else if ("email".equalsIgnoreCase(sortBy)) {
                result = nodeService.getProperty(nodeRef, ContentModel.PROP_EMAIL);
            } else if ("usage".equalsIgnoreCase(sortBy)) {
                result = nodeService.getProperty(nodeRef, ContentModel.PROP_SIZE_CURRENT);
            } else if ("quota".equalsIgnoreCase(sortBy)) {
                result = nodeService.getProperty(nodeRef, ContentModel.PROP_SIZE_QUOTA);
            } else {
                // Default
                result = nodeService.getProperty(nodeRef, ContentModel.PROP_USERNAME);
            }

            if (result == null) {
                result = "";
            }

            return result;
        }

    });

    return sortedPeopleRefs;
}

From source file:org.jumpmind.db.model.Table.java

/**
 * Sorts the foreign keys alphabetically.
 * //from  www.j  ava 2  s.co m
 * @param caseSensitive
 *            Whether case matters
 */
public void sortForeignKeys(final boolean caseSensitive) {
    if (!foreignKeys.isEmpty()) {
        final Collator collator = Collator.getInstance();

        Collections.sort(foreignKeys, new Comparator<ForeignKey>() {
            public int compare(ForeignKey obj1, ForeignKey obj2) {
                String fk1Name = ((ForeignKey) obj1).getName();
                String fk2Name = ((ForeignKey) obj2).getName();

                if (!caseSensitive) {
                    fk1Name = (fk1Name != null ? fk1Name.toLowerCase() : null);
                    fk2Name = (fk2Name != null ? fk2Name.toLowerCase() : null);
                }
                return collator.compare(fk1Name, fk2Name);
            }
        });
    }
}

From source file:org.alfresco.rest.api.tests.RepoService.java

private PagingResults<SiteInfo> getFavouriteSites(String userName, PagingRequest pagingRequest) {
    final Collator collator = Collator.getInstance();

    final Set<SiteInfo> sortedFavouriteSites = new TreeSet<SiteInfo>(new Comparator<SiteInfo>() {
        @Override/*from  w ww  . j  a  v a2  s.c  o  m*/
        public int compare(SiteInfo o1, SiteInfo o2) {
            return collator.compare(o1.getTitle(), o2.getTitle());
        }
    });

    Map<String, Serializable> prefs = preferenceService.getPreferences(userName, FAVOURITE_SITES_PREFIX);
    for (String key : prefs.keySet()) {
        boolean isFavourite = false;
        Serializable s = prefs.get(key);
        if (s instanceof Boolean) {
            isFavourite = (Boolean) s;
        }
        if (isFavourite) {
            String siteShortName = key.substring(FAVOURITE_SITES_PREFIX_LENGTH);
            SiteInfo siteInfo = siteService.getSite(siteShortName);
            if (siteInfo != null) {
                sortedFavouriteSites.add(siteInfo);
            }
        }
    }

    int totalSize = sortedFavouriteSites.size();
    final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);

    final List<SiteInfo> page = new ArrayList<SiteInfo>(pageDetails.getPageSize());
    Iterator<SiteInfo> it = sortedFavouriteSites.iterator();
    for (int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++) {
        SiteInfo favouriteSite = it.next();

        if (counter < pageDetails.getSkipCount()) {
            continue;
        }

        if (counter > pageDetails.getEnd() - 1) {
            break;
        }

        page.add(favouriteSite);
    }

    return new PagingResults<SiteInfo>() {
        @Override
        public List<SiteInfo> getPage() {
            return page;
        }

        @Override
        public boolean hasMoreItems() {
            return pageDetails.hasMoreItems();
        }

        @Override
        public Pair<Integer, Integer> getTotalResultCount() {
            Integer total = Integer.valueOf(sortedFavouriteSites.size());
            return new Pair<Integer, Integer>(total, total);
        }

        @Override
        public String getQueryExecutionId() {
            return null;
        }
    };
}

From source file:com.marand.thinkmed.medications.business.impl.DefaultMedicationsBo.java

@Override
public int compareTherapiesForSort(final TherapyDto firstTherapy, final TherapyDto secondTherapy,
        final Collator collator) {
    final boolean firstTherapyIsBaselineInfusion = firstTherapy instanceof ComplexTherapyDto
            && ((ComplexTherapyDto) firstTherapy).isBaselineInfusion();
    final boolean secondTherapyIsBaselineInfusion = secondTherapy instanceof ComplexTherapyDto
            && ((ComplexTherapyDto) secondTherapy).isBaselineInfusion();

    if (firstTherapyIsBaselineInfusion && !secondTherapyIsBaselineInfusion) {
        return -1;
    }//w  ww  .j a  v a  2 s . c o m
    if (!firstTherapyIsBaselineInfusion && secondTherapyIsBaselineInfusion) {
        return 1;
    }
    return collator.compare(firstTherapy.getTherapyDescription(), secondTherapy.getTherapyDescription());
}

From source file:org.alfresco.repo.jscript.ScriptNode.java

/**
 * Performs a locale-sensitive sort by name of a node array
 * @param nodes the node array/*w w w  .  j  av a  2 s. c om*/
 */
private static void sort(Object[] nodes) {
    final Collator col = Collator.getInstance(I18NUtil.getLocale());
    Arrays.sort(nodes, new Comparator<Object>() {
        @Override
        public int compare(Object o1, Object o2) {
            return col.compare(((ScriptNode) o1).getName(), ((ScriptNode) o2).getName());
        }
    });
}

From source file:nz.ac.otago.psyanlab.common.designer.ExperimentDesignerActivity.java

/**
 * Get an adapter for all methods in given class hierarchy which return a given type.
 *
 * @param object      Object to fetch methods from.
 * @param returnTypes Type to select methods by.
 *//*ww  w .j  av  a  2 s.  com*/
@Override
public SpinnerAdapter getMethodsAdapter(ExperimentObject object, int returnTypes) {
    // Obtain the name factory to pull the internationalised event names.
    SortedSet<MethodData> filteredMethods = new TreeSet<MethodData>(new Comparator<MethodData>() {
        @Override
        public int compare(MethodData lhs, MethodData rhs) {
            Collator collator = getCollater();
            return collator.compare(lhs.name, rhs.name);
        }
    });

    object.loadInMatchingMethods(ExperimentDesignerActivity.this, returnTypes, filteredMethods);

    return new MethodAdapter(this, filteredMethods);
}

From source file:nz.ac.otago.psyanlab.common.designer.ExperimentDesignerActivity.java

/**
 * Get an adapter containing all methods which register listeners for events emitted by the
 * given class./*from  w  ww.j  a  va 2  s  .c  o  m*/
 *
 * @param clazz Class with the events that are emitted.
 * @return Events ListAdapter.
 */
@Override
public SpinnerAdapter getEventsAdapter(final Class<?> clazz) {
    // Obtain the name factory to pull the internationalised event names.
    SortedSet<EventData> filteredEvents;

    final NameResolverFactory nameFactory = ModelUtils.getEventNameFactory(clazz);

    filteredEvents = new TreeSet<EventData>(new Comparator<EventData>() {
        @Override
        public int compare(EventData lhs, EventData rhs) {
            Collator collator = getCollater();
            return collator.compare(nameFactory.getName(ExperimentDesignerActivity.this, lhs.id()),
                    nameFactory.getName(ExperimentDesignerActivity.this, rhs.id()));
        }
    });

    Method[] methods = clazz.getMethods();

    // Filter methods for those which register listeners for events.
    for (int i = 0; i < methods.length; i++) {
        EventData annotation = methods[i].getAnnotation(EventData.class);
        if (annotation != null) {
            filteredEvents.add(annotation);
        }
    }

    return new EventAdapter(this, filteredEvents, nameFactory);
}

From source file:nz.ac.otago.psyanlab.common.designer.ExperimentDesignerActivity.java

private void updateReferencesToProp(long id) {
    Prop prop = mExperiment.props.get(id);
    updateRuleReferencesToObject(id, ExperimentObject.KIND_PROP, prop);

    ArrayList<Long> calls = findAffectedCallIds(ExperimentObject.KIND_PROP, id);

    for (Long callId : calls) {
        // The kind of the prop may have changed, as such there may be a
        // different set of calls that can be made. The job here is to
        // identify if an incorrect call is referenced, if this is the case,
        // throw away the call operand.
        CallValue call = (CallValue) mExperiment.operands.get(callId);

        if (call == null) {
            // It is possible we might be trying to update a call we have
            // already obliterated due to removing a parent operand, so we
            // just skip the missing operand and continue processing the
            // list.
            continue;
        }/*from   ww  w. j  av  a  2 s . com*/

        if (prop == null) {
            // The prop was actually deleted so we just need to remove the
            // reference.
            StubOperand replacement = new StubOperand(call.getName());
            replacement.type = call.type;
            replacement.tag = call.tag;
            deleteOperand(callId, false);
            putOperand(callId, replacement);
            continue;
        }

        SortedSet<MethodData> filteredMethods = new TreeSet<MethodData>(new Comparator<MethodData>() {
            @Override
            public int compare(MethodData lhs, MethodData rhs) {
                Collator collator = getCollater();
                return collator.compare(lhs.name, rhs.name);
            }
        });

        prop.loadInMatchingMethods(this, call.type, filteredMethods);

        boolean foundMethod = false;
        for (MethodData methodData : filteredMethods) {
            if (methodData.id == call.method) {
                foundMethod = true;
                break;
            }
        }

        if (!foundMethod) {
            // Obliterate the operand and replace it with a stub. We throw
            // away the call value because we don't want a reference to an
            // object without a matching method.
            StubOperand replacement = new StubOperand(call.getName());
            replacement.type = call.type;
            replacement.tag = call.tag;
            deleteOperand(callId, false);
            putOperand(callId, replacement);
        } else {
            notifyOperandAdapters(callId);
            notifyOperandDataChangeListeners();
        }
    }
}