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:org.sakaiproject.umem.tool.ui.SiteListBean.java

public static final Comparator getUserSitesRowComparator(final String fieldName, final boolean sortAscending,
        final Collator collator) {
    return new Comparator() {
        public int compare(Object o1, Object o2) {
            if (o1 instanceof UserSitesRow && o2 instanceof UserSitesRow) {
                UserSitesRow r1 = (UserSitesRow) o1;
                UserSitesRow r2 = (UserSitesRow) o2;
                try {
                    if (fieldName.equals(SORT_SITE_NAME)) {
                        String s1 = r1.getSiteTitle();
                        String s2 = r2.getSiteTitle();
                        int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                                s2 != null ? s2.toLowerCase() : "");
                        if (sortAscending)
                            return res;
                        else
                            return -res;
                    } else if (fieldName.equals(SORT_SITE_TYPE)) {
                        String s1 = r1.getSiteType();
                        String s2 = r2.getSiteType();
                        int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                                s2 != null ? s2.toLowerCase() : "");
                        if (sortAscending)
                            return res;
                        else
                            return -res;
                    } else if (fieldName.equals(SORT_SITE_RID)) {
                        String s1 = r1.getRoleName();
                        String s2 = r2.getRoleName();
                        int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                                s2 != null ? s2.toLowerCase() : "");
                        if (sortAscending)
                            return res;
                        else
                            return -res;
                    } else if (fieldName.equals(SORT_SITE_PV)) {
                        String s1 = r1.getPubView();
                        String s2 = r2.getPubView();
                        int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                                s2 != null ? s2.toLowerCase() : "");
                        if (sortAscending)
                            return res;
                        else
                            return -res;
                    } else if (fieldName.equals(SORT_USER_STATUS)) {
                        String s1 = r1.getUserStatus();
                        String s2 = r2.getUserStatus();
                        int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                                s2 != null ? s2.toLowerCase() : "");
                        if (sortAscending)
                            return res;
                        else
                            return -res;
                    } else if (fieldName.equals(SORT_SITE_TERM)) {
                        String s1 = r1.getSiteTerm();
                        String s2 = r2.getSiteTerm();
                        int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                                s2 != null ? s2.toLowerCase() : "");
                        if (sortAscending)
                            return res;
                        else
                            return -res;
                    }/*from   ww w  . ja va2  s  . c  om*/
                } catch (Exception e) {
                    LOG.warn("Error occurred while sorting by: " + fieldName, e);
                }
            }
            return 0;
        }
    };
}

From source file:org.n52.io.response.ParameterOutput.java

@Override
public int compare(Collator collator, ParameterOutput o) {
    if (collator == null) {
        collator = Collator.getInstance();
    }//from  www .j a  v a2 s.c  o m
    return collator.compare(getLabel().toLowerCase(), o.getLabel().toLowerCase());
}

From source file:org.kuali.kfs.sys.DynamicCollectionComparatorTest.java

private <T> boolean isSortedByAscendingOrder(List<T> objectList, String fieldName) throws Exception {
    Collator collator = Collator.getInstance();
    Object tempValue = null;/*from   www .  ja  v a2 s . c  om*/
    for (T object : objectList) {
        Object fieldValue = PropertyUtils.getProperty(object, fieldName);

        if (tempValue == null || collator.compare(tempValue, fieldValue) <= 0) {
            tempValue = fieldValue;
        } else {
            return false;
        }
    }
    return true;
}

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

private Long[] sortKeys(Context context, LongSparseArray<T> items) {
    Locale locale = context.getResources().getConfiguration().locale;
    final Collator collator = Collator.getInstance(locale);
    collator.setStrength(Collator.SECONDARY);

    SortedSet<Long> sortedKeys = new TreeSet<Long>(new Comparator<Long>() {
        @Override//from   w  w  w.  ja  v  a 2 s .  c  o m
        public int compare(Long lhs, Long rhs) {
            return collator.compare(mItems.get(lhs).toString(), mItems.get(rhs).toString());
        }
    });

    for (int i = 0; i < items.size(); i++) {
        sortedKeys.add(items.keyAt(i));
    }

    return sortedKeys.toArray(new Long[sortedKeys.size()]);
}

From source file:edu.cornell.mannlib.vedit.controller.BaseEditController.java

public List<Option> getSortedList(HashMap<String, Option> hashMap, List<Option> optionList, VitroRequest vreq) {

    class ListComparator implements Comparator<String> {

        Collator collator;/*  w w w .  j  a  va 2  s  .co m*/

        public ListComparator(Collator collator) {
            this.collator = collator;
        }

        @Override
        public int compare(String str1, String str2) {
            return collator.compare(str1, str2);
        }

    }

    List<String> bodyVal = new ArrayList<String>();
    List<Option> options = new ArrayList<Option>();
    Iterator<Option> itr = optionList.iterator();
    while (itr.hasNext()) {
        Option option = itr.next();
        hashMap.put(option.getBody(), option);
        bodyVal.add(option.getBody());
    }

    Collections.sort(bodyVal, new ListComparator(vreq.getCollator()));
    ListIterator<String> itrStr = bodyVal.listIterator();
    while (itrStr.hasNext()) {
        options.add(hashMap.get(itrStr.next()));
    }
    return options;
}

From source file:io.gravitee.gateway.services.sync.SyncManager.java

private boolean hasMatchingTags(Api api) {
    final Optional<List<String>> optTagList = gatewayConfiguration.shardingTags();

    if (optTagList.isPresent()) {
        List<String> tagList = optTagList.get();
        if (api.getTags() != null) {
            final List<String> inclusionTags = tagList.stream().map(String::trim)
                    .filter(tag -> !tag.startsWith("!")).collect(Collectors.toList());

            final List<String> exclusionTags = tagList.stream().map(String::trim)
                    .filter(tag -> tag.startsWith("!")).map(tag -> tag.substring(1))
                    .collect(Collectors.toList());

            if (inclusionTags.stream().filter(exclusionTags::contains).count() > 0) {
                throw new IllegalArgumentException("You must not configure a tag to be included and excluded");
            }//from   ww w  .j  a v a2s .  c  om

            final boolean hasMatchingTags = inclusionTags.stream()
                    .anyMatch(tag -> api.getTags().stream().anyMatch(apiTag -> {
                        final Collator collator = Collator.getInstance();
                        collator.setStrength(Collator.NO_DECOMPOSITION);
                        return collator.compare(tag, apiTag) == 0;
                    })) || (!exclusionTags.isEmpty() && exclusionTags.stream()
                            .noneMatch(tag -> api.getTags().stream().anyMatch(apiTag -> {
                                final Collator collator = Collator.getInstance();
                                collator.setStrength(Collator.NO_DECOMPOSITION);
                                return collator.compare(tag, apiTag) == 0;
                            })));

            if (!hasMatchingTags) {
                logger.info("The API {} has been ignored because not in configured tags {}", api.getName(),
                        tagList);
            }
            return hasMatchingTags;
        }
        logger.info("Tags {} are configured on gateway instance but not found on the API {}", tagList,
                api.getName());
        return false;
    }
    // no tags configured on this gateway instance
    return true;
}

From source file:de.geeksfactory.opacclient.objects.Library.java

@Override
public int compareTo(Library arg0) {
    Collator deCollator = Collator.getInstance(Locale.GERMAN);
    deCollator.setStrength(Collator.TERTIARY);

    int g = deCollator.compare(country, arg0.getCountry());
    if (g == 0) {
        g = deCollator.compare(state, arg0.getState());
        if (g == 0) {
            g = deCollator.compare(city, arg0.getCity());
            if (g == 0) {
                g = deCollator.compare(title, arg0.getTitle());
            }//from   w  w  w  .  ja v  a 2  s.c  o  m
        }
    }
    return g;
}

From source file:net.gcompris.GComprisActivity.java

public int localeCompare(String a, String b, String locale) {
    String[] parts = locale.split("[\\._]");
    Locale l;/*  www .  ja va2 s.  c  om*/
    if (parts.length == 1)
        l = new Locale(parts[0]);
    else if (parts.length == 2)
        l = new Locale(parts[0], parts[1]);
    else
        l = Locale.getDefault();
    Collator collator = Collator.getInstance(l);
    // Note: This works only if the device supports the
    // passed locale. If it does not or if an invalid locale string has been
    // passed, the collator seems to sort according to Locale.getDefault()
    return collator.compare(a, b);
}

From source file:org.eclipse.smarthome.io.rest.core.extensions.ExtensionResource.java

private Set<ExtensionType> getAllExtensionTypes(Locale locale) {
    final Collator coll = Collator.getInstance(locale);
    coll.setStrength(Collator.PRIMARY);
    Set<ExtensionType> ret = new TreeSet<>(new Comparator<ExtensionType>() {
        @Override/*from  ww  w. j  a v  a2  s.  co m*/
        public int compare(ExtensionType o1, ExtensionType o2) {
            return coll.compare(o1.getLabel(), o2.getLabel());
        }
    });
    for (ExtensionService extensionService : extensionServices) {
        ret.addAll(extensionService.getTypes(locale));
    }
    return ret;
}

From source file:com.andryr.musicplayer.activities.MusicPicker.java

private void getSongList() {
    ContentResolver resolver = getContentResolver();
    Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    Cursor cursor = resolver.query(musicUri, sProjection, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
        int idCol = cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID);
        if (idCol == -1) {
            idCol = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
        }//from   www  . j  ava2s.  c om
        int titleCol = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int artistCol = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
        int albumCol = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
        int albumIdCol = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
        int trackCol = cursor.getColumnIndex(MediaStore.Audio.Media.TRACK);

        int durationCol = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION);

        do {
            long id = cursor.getLong(idCol);
            String title = cursor.getString(titleCol);

            String artist = cursor.getString(artistCol);

            String album = cursor.getString(albumCol);

            long albumId = cursor.getLong(albumIdCol);

            int track = cursor.getInt(trackCol);

            long duration = cursor.getLong(durationCol);

            mSongList.add(new Song(id, title, artist, album, albumId, track, duration));
        } while (cursor.moveToNext());

        Collections.sort(mSongList, new Comparator<Song>() {

            @Override
            public int compare(Song lhs, Song rhs) {
                Collator c = Collator.getInstance(Locale.getDefault());
                c.setStrength(Collator.PRIMARY);
                return c.compare(lhs.getTitle(), rhs.getTitle());
            }
        });

    }
    if (cursor != null) {
        cursor.close();
    }
}