Example usage for java.text Collator getCollationKey

List of usage examples for java.text Collator getCollationKey

Introduction

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

Prototype

public abstract CollationKey getCollationKey(String source);

Source Link

Document

Transforms the String into a series of bits that can be compared bitwise to other CollationKeys.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    // Build a vector of words to be sorted
    ArrayList list = new ArrayList();
    list.add("m");
    list.add("c2");
    list.add("e");
    list.add("c1");

    Collator collate = Collator.getInstance();

    CollationKey[] keys = new CollationKey[list.size()];

    for (int k = 0; k < list.size(); k++)
        keys[k] = collate.getCollationKey((String) list.get(k));

    Arrays.sort(keys);//  w  ww .  java 2  s.com

    for (int l = 0; l < keys.length; l++) {
        System.out.println(keys[l].getSourceString());
    }
}

From source file:SortWithCollationKeys.java

public static void main(String[] args) {
    Vector<String> list = new Vector<String>();
    list.add("m");
    list.add("c");
    list.add("e");
    list.add("c");

    Collator collate = Collator.getInstance();

    CollationKey[] keys = new CollationKey[list.size()];

    for (int k = 0; k < list.size(); k++)
        keys[k] = collate.getCollationKey((String) list.elementAt(k));

    CollationKey tmp;/*from  w  ww . jav a  2 s .c  om*/
    for (int i = 0; i < keys.length; i++) {
        for (int j = i + 1; j < keys.length; j++) {
            if (keys[i].compareTo(keys[j]) > 0) {
                tmp = keys[i];
                keys[i] = keys[j];
                keys[j] = tmp;
            }
        }
    }
    for (int l = 0; l < keys.length; l++) {
        System.out.println(keys[l].getSourceString());
    }
}

From source file:KeysDemo.java

static public void main(String[] args) {

    Collator enUSCollator = Collator.getInstance(new Locale("en", "US"));

    String[] words = { "peach", "apricot", "grape", "lemon" };

    CollationKey[] keys = new CollationKey[words.length];

    for (int k = 0; k < keys.length; k++) {
        keys[k] = enUSCollator.getCollationKey(words[k]);
    }// w  w w  .  j a va2  s  . co m

    sortArray(keys);
    displayWords(keys);
}

From source file:org.alfresco.web.data.Sort.java

/**
 * Build a list of collation keys for comparing locale sensitive strings or build
 * the appropriate objects for comparison for other standard data types.
 * /*from   ww  w  .  ja  va2s.  co m*/
 * @param collator      the Collator object to use to build String keys
 */
@SuppressWarnings("unchecked")
protected List buildCollationKeys(Collator collator) {
    List data = this.data;
    int iSize = data.size();
    List keys = new ArrayList(iSize);

    try {
        // create the Bean getter method invoker to retrieve the value for a colunm
        String methodName = getGetterMethodName(this.column);
        Class returnType = null;
        ;
        Method getter = null;
        // there will always be at least one item to sort if we get to this method
        Object bean = this.data.get(0);
        try {
            getter = bean.getClass().getMethod(methodName, (Class[]) null);
            returnType = getter.getReturnType();
        } catch (NoSuchMethodException nsmerr) {
            // no bean getter method found - try Map implementation
            if (bean instanceof Map) {
                Object obj = ((Map) bean).get(this.column);
                if (obj != null) {
                    returnType = obj.getClass();
                } else {
                    if (s_logger.isInfoEnabled()) {
                        s_logger.info("Unable to get return type class for RichList column: " + column
                                + ". Suggest set java type directly in sort component tag.");
                    }
                    returnType = Object.class;
                }
            } else {
                throw new IllegalStateException(
                        "Unable to find bean getter or Map impl for column name: " + this.column);
            }
        }

        // create appropriate comparator instance based on data type
        // using the strategy pattern so  sub-classes of Sort simply invoke the
        // compare() method on the comparator interface - no type info required
        boolean bknownType = true;
        if (returnType.equals(String.class)) {
            if (strongStringCompare == true) {
                this.comparator = new StringComparator();
            } else {
                this.comparator = new SimpleStringComparator();
            }
        } else if (returnType.equals(Date.class)) {
            this.comparator = new DateComparator();
        } else if (returnType.equals(boolean.class) || returnType.equals(Boolean.class)) {
            this.comparator = new BooleanComparator();
        } else if (returnType.equals(int.class) || returnType.equals(Integer.class)) {
            this.comparator = new IntegerComparator();
        } else if (returnType.equals(long.class) || returnType.equals(Long.class)) {
            this.comparator = new LongComparator();
        } else if (returnType.equals(float.class) || returnType.equals(Float.class)) {
            this.comparator = new FloatComparator();
        } else if (returnType.equals(Timestamp.class)) {
            this.comparator = new TimestampComparator();
        } else if (DynamicResolver.class.isAssignableFrom(returnType)) {
            this.comparator = new SimpleStringComparator();
        } else {
            if (s_logger.isDebugEnabled())
                s_logger.debug("Unsupported sort data type: " + returnType + " defaulting to .toString()");
            this.comparator = new SimpleComparator();
            bknownType = false;
        }

        // create a collation key for each required column item in the dataset
        for (int iIndex = 0; iIndex < iSize; iIndex++) {
            Object obj;
            if (getter != null) {
                // if we have a bean getter method impl use that
                try {
                    getter.setAccessible(true);
                } catch (SecurityException se) {
                }
                obj = getter.invoke(data.get(iIndex), (Object[]) null);
            } else {
                // else we must have a bean Map impl
                obj = ((Map) data.get(iIndex)).get(column);
            }

            if (obj instanceof String) {
                String str = (String) obj;
                if (strongStringCompare == true) {
                    if (str.indexOf(' ') != -1) {
                        // quote white space characters or they will be ignored by the Collator!
                        int iLength = str.length();
                        StringBuilder s = new StringBuilder(iLength + 4);
                        char c;
                        for (int i = 0; i < iLength; i++) {
                            c = str.charAt(i);
                            if (c != ' ') {
                                s.append(c);
                            } else {
                                s.append('\'').append(c).append('\'');
                            }
                        }
                        str = s.toString();
                    }
                    keys.add(collator.getCollationKey(str));
                } else {
                    keys.add(str);
                }
            } else if (bknownType == true) {
                // the appropriate wrapper object will be create by the reflection
                // system to wrap primative types e.g. int and boolean.
                // therefore the correct type will be ready for use by the comparator
                keys.add(obj);
            } else {
                if (obj != null) {
                    keys.add(obj.toString());
                } else {
                    keys.add(null);
                }
            }
        }
    } catch (Exception err) {
        throw new RuntimeException(err);
    }

    return keys;
}