Example usage for java.text RuleBasedCollator RuleBasedCollator

List of usage examples for java.text RuleBasedCollator RuleBasedCollator

Introduction

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

Prototype

private RuleBasedCollator(RuleBasedCollator that) 

Source Link

Document

"Copy constructor."

Usage

From source file:org.apache.wiki.plugin.AbstractReferralPlugin.java

/**
 * Helper method to initialize the comparator for this page.
 *//*from ww  w.  j  a va  2 s.  c  om*/
private void initSorter(WikiContext context, Map<String, String> params) {
    String order = params.get(PARAM_SORTORDER);
    if (order == null || order.length() == 0) {
        // Use the configured comparator
        m_sorter = context.getEngine().getPageSorter();
    } else if (order.equalsIgnoreCase(PARAM_SORTORDER_JAVA)) {
        // use Java "natural" ordering
        m_sorter = new PageSorter(JavaNaturalComparator.DEFAULT_JAVA_COMPARATOR);
    } else if (order.equalsIgnoreCase(PARAM_SORTORDER_LOCALE)) {
        // use this locale's ordering
        m_sorter = new PageSorter(LocaleComparator.DEFAULT_LOCALE_COMPARATOR);
    } else if (order.equalsIgnoreCase(PARAM_SORTORDER_HUMAN)) {
        // use human ordering
        m_sorter = new PageSorter(HumanComparator.DEFAULT_HUMAN_COMPARATOR);
    } else
        try {
            Collator collator = new RuleBasedCollator(order);
            collator.setStrength(Collator.PRIMARY);
            m_sorter = new PageSorter(new CollatorComparator(collator));
        } catch (ParseException pe) {
            log.info("Failed to parse requested collator - using default ordering", pe);
            m_sorter = context.getEngine().getPageSorter();
        }
}

From source file:org.sakaiproject.component.app.roster.RosterManagerImpl.java

private Comparator<Group> sortGroups() {
    Comparator<Group> groupComparator = new Comparator<Group>() {
        public int compare(Group one, Group another) {
            try {
                RuleBasedCollator r_collator = new RuleBasedCollator(
                        ((RuleBasedCollator) Collator.getInstance()).getRules().replaceAll("<'\u005f'",
                                "<' '<'\u005f'"));
                return r_collator.compare(one.getTitle(), another.getTitle());
            } catch (ParseException e) {
                return Collator.getInstance().compare(one.getTitle(), another.getTitle());
            }/*  w  w w. j a  v  a2  s  .c om*/
        }
    };
    return groupComparator;
}

From source file:org.sakaiproject.sitestats.tool.wicket.providers.ReportDefsProvider.java

public final Comparator<ReportDef> getReportDefComparator() {
    return new Comparator<ReportDef>() {
        private transient Collator collator = Collator.getInstance();
        {/*from   w w  w . j  a v  a2s.  c o m*/
            try {
                collator = new RuleBasedCollator(((RuleBasedCollator) Collator.getInstance()).getRules()
                        .replaceAll("<'\u005f'", "<' '<'\u005f'"));
            } catch (ParseException e) {
                log.error("Unable to create RuleBasedCollator");
            }
        }

        public int compare(ReportDef o1, ReportDef o2) {
            String title1 = null;
            String title2 = null;
            if (o1.isTitleLocalized()) {
                title1 = (String) new ResourceModel(o1.getTitleBundleKey()).getObject();
            } else {
                title1 = o1.getTitle();
            }
            if (o2.isTitleLocalized()) {
                title2 = (String) new ResourceModel(o2.getTitleBundleKey()).getObject();
            } else {
                title2 = o2.getTitle();
            }
            return collator.compare(title1, title2);
        }

    };
}

From source file:org.sakaiproject.sitestats.tool.wicket.providers.ReportsDataProvider.java

public final Comparator<Stat> getReportDataComparator(final String fieldName, final boolean sortAscending,
        final StatsManager SST_sm, final EventRegistryService SST_ers, final UserDirectoryService M_uds) {
    return new Comparator<Stat>() {
        private transient Collator collator = Collator.getInstance();
        {//  w ww. j a  v a2  s  .c o m
            try {
                collator = new RuleBasedCollator(((RuleBasedCollator) Collator.getInstance()).getRules()
                        .replaceAll("<'\u005f'", "<' '<'\u005f'"));
            } catch (ParseException e) {
                logger.error("Unable to create RuleBasedCollator");
            }
        }

        public int compare(Stat r1, Stat r2) {
            if (fieldName.equals(COL_SITE)) {
                String s1 = Locator.getFacade().getSiteService().getSiteDisplay(r1.getSiteId()).toLowerCase();
                String s2 = Locator.getFacade().getSiteService().getSiteDisplay(r2.getSiteId()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_USERID)) {
                String s1;
                try {
                    s1 = M_uds.getUser(r1.getUserId()).getDisplayId();
                } catch (UserNotDefinedException e) {
                    s1 = "-";
                }
                String s2;
                try {
                    s2 = M_uds.getUser(r2.getUserId()).getDisplayId();
                } catch (UserNotDefinedException e) {
                    s2 = "-";
                }
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_USERNAME)) {
                String s1 = Locator.getFacade().getStatsManager().getUserNameForDisplay(r1.getUserId())
                        .toLowerCase();
                String s2 = Locator.getFacade().getStatsManager().getUserNameForDisplay(r2.getUserId())
                        .toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_EVENT)) {
                EventStat es1 = (EventStat) r1;
                EventStat es2 = (EventStat) r2;
                String s1 = SST_ers.getEventName(es1.getEventId()).toLowerCase();
                String s2 = SST_ers.getEventName(es2.getEventId()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_TOOL)) {
                EventStat es1 = (EventStat) r1;
                EventStat es2 = (EventStat) r2;
                String s1 = SST_ers.getToolName(es1.getToolId()).toLowerCase();
                String s2 = SST_ers.getToolName(es2.getToolId()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_RESOURCE)) {
                ResourceStat rs1 = (ResourceStat) r1;
                ResourceStat rs2 = (ResourceStat) r2;
                String s1 = SST_sm.getResourceName(rs1.getResourceRef()).toLowerCase();
                String s2 = SST_sm.getResourceName(rs2.getResourceRef()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_ACTION)) {
                ResourceStat rs1 = (ResourceStat) r1;
                ResourceStat rs2 = (ResourceStat) r2;
                String s1 = ((String) rs1.getResourceAction()).toLowerCase();
                String s2 = ((String) rs2.getResourceAction()).toLowerCase();
                int res = collator.compare(s1, s2);
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_DATE)) {
                int res = r1.getDate() != null ? r1.getDate().compareTo(r2.getDate()) : -1;
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_TOTAL)) {
                int res = Long.valueOf(r1.getCount()).compareTo(Long.valueOf(r2.getCount()));
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_VISITS)) {
                SiteVisits sv1 = (SiteVisits) r1;
                SiteVisits sv2 = (SiteVisits) r2;
                int res = Long.valueOf(sv1.getTotalVisits()).compareTo(Long.valueOf(sv2.getTotalVisits()));
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_UNIQUEVISITS)) {
                SiteVisits sv1 = (SiteVisits) r1;
                SiteVisits sv2 = (SiteVisits) r2;
                int res = Long.valueOf(sv1.getTotalUnique()).compareTo(Long.valueOf(sv2.getTotalUnique()));
                if (sortAscending)
                    return res;
                else
                    return -res;
            } else if (fieldName.equals(COL_DURATION)) {
                SitePresence sv1 = (SitePresence) r1;
                SitePresence sv2 = (SitePresence) r2;
                int res = Long.valueOf(sv1.getDuration()).compareTo(Long.valueOf(sv2.getDuration()));
                if (sortAscending)
                    return res;
                else
                    return -res;
            }
            return 0;
        }
    };
}

From source file:org.sakaiproject.tool.assessment.util.BeanSortComparator.java

private int subCompare(String s1, String s2) {
    //we do not want to use null values for sorting
    if (s1 == null) {
        s1 = "";//from  ww  w .ja v  a2s.  c  o  m
    }

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

    // Deal with n/a case
    if (s1.toLowerCase().startsWith("n/a") && !s2.toLowerCase().startsWith("n/a"))
        return 1;

    if (s2.toLowerCase().startsWith("n/a") && !s1.toLowerCase().startsWith("n/a"))
        return -1;

    String finalS1 = s1.replaceAll("<.*?>", "");
    String finalS2 = s2.replaceAll("<.*?>", "");
    RuleBasedCollator collator_ini = (RuleBasedCollator) Collator.getInstance();
    try {
        RuleBasedCollator collator = new RuleBasedCollator(
                collator_ini.getRules().replaceAll("<'\u005f'", "<' '<'\u005f'"));
        return collator.compare(finalS1.toLowerCase(), finalS2.toLowerCase());
    } catch (ParseException e) {
    }
    return Collator.getInstance().compare(finalS1.toLowerCase(), finalS2.toLowerCase());
}

From source file:org.sakaiproject.tool.roster.RosterGroupMembership.java

private Comparator<GroupedParticipants> sortByGroup() {
    Comparator<GroupedParticipants> groupComparator = new Comparator<GroupedParticipants>() {
        public int compare(GroupedParticipants one, GroupedParticipants another) {
            try {
                RuleBasedCollator r_collator = new RuleBasedCollator(
                        ((RuleBasedCollator) Collator.getInstance()).getRules().replaceAll("<'\u005f'",
                                "<' '<'\u005f'"));
                return r_collator.compare(one.getGroupTitle(), another.getGroupTitle());
            } catch (ParseException e) {
                return Collator.getInstance().compare(one.getGroupTitle(), another.getGroupTitle());
            }/*from  w w  w  .j a v a 2s  .  c  o m*/
        }
    };
    return groupComparator;
}