Search in sources :

Example 56 with Collator

use of java.text.Collator in project AndroidChromium by JackyAndroid.

the class AutofillProfileBridge method getSupportedCountries.

/** @return The list of supported countries sorted by their localized display names. */
public static List<DropdownKeyValue> getSupportedCountries() {
    List<String> countryCodes = new ArrayList<>();
    List<String> countryNames = new ArrayList<>();
    List<DropdownKeyValue> countries = new ArrayList<>();
    nativeGetSupportedCountries(countryCodes, countryNames);
    for (int i = 0; i < countryCodes.size(); i++) {
        countries.add(new DropdownKeyValue(countryCodes.get(i), countryNames.get(i)));
    }
    final Collator collator = Collator.getInstance(Locale.getDefault());
    collator.setStrength(Collator.PRIMARY);
    Collections.sort(countries, new Comparator<DropdownKeyValue>() {

        @Override
        public int compare(DropdownKeyValue lhs, DropdownKeyValue rhs) {
            int result = collator.compare(lhs.getValue(), rhs.getValue());
            if (result == 0)
                result = lhs.getKey().compareTo(rhs.getKey());
            return result;
        }
    });
    return countries;
}
Also used : ArrayList(java.util.ArrayList) Collator(java.text.Collator)

Example 57 with Collator

use of java.text.Collator in project jdbc-shards by wplatform.

the class Parser method parseSetCollation.

private Set parseSetCollation() {
    Set command = new Set(session, SetTypes.COLLATION);
    String name = readAliasIdentifier();
    command.setString(name);
    if (equalsToken(name, CompareMode.OFF)) {
        return command;
    }
    Collator coll = CompareMode.getCollator(name);
    if (coll == null) {
        throw DbException.getInvalidValueException("collation", name);
    }
    if (readIf("STRENGTH")) {
        if (readIf("PRIMARY")) {
            command.setInt(Collator.PRIMARY);
        } else if (readIf("SECONDARY")) {
            command.setInt(Collator.SECONDARY);
        } else if (readIf("TERTIARY")) {
            command.setInt(Collator.TERTIARY);
        } else if (readIf("IDENTICAL")) {
            command.setInt(Collator.IDENTICAL);
        }
    } else {
        command.setInt(coll.getStrength());
    }
    return command;
}
Also used : HashSet(java.util.HashSet) Collator(java.text.Collator)

Example 58 with Collator

use of java.text.Collator in project MonjaDB by Kanatoko.

the class MSortAction method executeFunction.

//--------------------------------------------------------------------------------
public void executeFunction() throws Exception {
    final String columnName = MRegEx.getMatch("mj sort by ([^ ]+) ", action);
    final int _sortOrder = MStringUtil.parseInt(MRegEx.getMatch(".* (1|-1)$", action));
    Comparator c = new Comparator() {

        /**************/
        public int compare(Object o1, Object o2) {
            Map map1 = (Map) o1;
            Map map2 = (Map) o2;
            Object value1 = map1.get(columnName);
            Object value2 = map2.get(columnName);
            if (value1 == null && value2 == null) {
                return 0;
            } else if (value1 == null) {
                return -1 * _sortOrder;
            } else if (value2 == null) {
                return 1 * _sortOrder;
            }
            if (value1 == null || value2 == null) {
                return 0;
            }
            if ((value1 instanceof Integer || value1 instanceof Double) && (value2 instanceof Integer || value2 instanceof Double)) {
                double double1 = toDouble(value1);
                double double2 = toDouble(value2);
                if (double1 > double2) {
                    return 1 * _sortOrder;
                } else if (double1 == double2) {
                    return 0;
                } else {
                    return -1 * _sortOrder;
                }
            }
            String str1 = value1.toString();
            String str2 = value2.toString();
            Collator collator = Collator.getInstance(Locale.getDefault());
            return collator.compare(str1, str2) * _sortOrder;
        }

        public boolean equals(Object o1, Object o2) {
            return o1.equals(o2);
        }
    };
    /***************/
    Collections.sort(dataManager.getDocumentDataList(), c);
}
Also used : Collator(java.text.Collator)

Example 59 with Collator

use of java.text.Collator in project NetGuard by M66B.

the class Rule method getRules.

public static List<Rule> getRules(final boolean all, Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE);
    SharedPreferences other = context.getSharedPreferences("other", Context.MODE_PRIVATE);
    SharedPreferences screen_wifi = context.getSharedPreferences("screen_wifi", Context.MODE_PRIVATE);
    SharedPreferences screen_other = context.getSharedPreferences("screen_other", Context.MODE_PRIVATE);
    SharedPreferences roaming = context.getSharedPreferences("roaming", Context.MODE_PRIVATE);
    SharedPreferences lockdown = context.getSharedPreferences("lockdown", Context.MODE_PRIVATE);
    SharedPreferences apply = context.getSharedPreferences("apply", Context.MODE_PRIVATE);
    SharedPreferences notify = context.getSharedPreferences("notify", Context.MODE_PRIVATE);
    // Get settings
    boolean default_wifi = prefs.getBoolean("whitelist_wifi", true);
    boolean default_other = prefs.getBoolean("whitelist_other", true);
    boolean default_screen_wifi = prefs.getBoolean("screen_wifi", false);
    boolean default_screen_other = prefs.getBoolean("screen_other", false);
    boolean default_roaming = prefs.getBoolean("whitelist_roaming", true);
    boolean manage_system = prefs.getBoolean("manage_system", false);
    boolean screen_on = prefs.getBoolean("screen_on", true);
    boolean show_user = prefs.getBoolean("show_user", true);
    boolean show_system = prefs.getBoolean("show_system", false);
    boolean show_nointernet = prefs.getBoolean("show_nointernet", true);
    boolean show_disabled = prefs.getBoolean("show_disabled", true);
    default_screen_wifi = default_screen_wifi && screen_on;
    default_screen_other = default_screen_other && screen_on;
    // Get predefined rules
    Map<String, Boolean> pre_wifi_blocked = new HashMap<>();
    Map<String, Boolean> pre_other_blocked = new HashMap<>();
    Map<String, Boolean> pre_roaming = new HashMap<>();
    Map<String, String[]> pre_related = new HashMap<>();
    Map<String, Boolean> pre_system = new HashMap<>();
    try {
        XmlResourceParser xml = context.getResources().getXml(R.xml.predefined);
        int eventType = xml.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG)
                if ("wifi".equals(xml.getName())) {
                    String pkg = xml.getAttributeValue(null, "package");
                    boolean pblocked = xml.getAttributeBooleanValue(null, "blocked", false);
                    pre_wifi_blocked.put(pkg, pblocked);
                } else if ("other".equals(xml.getName())) {
                    String pkg = xml.getAttributeValue(null, "package");
                    boolean pblocked = xml.getAttributeBooleanValue(null, "blocked", false);
                    boolean proaming = xml.getAttributeBooleanValue(null, "roaming", default_roaming);
                    pre_other_blocked.put(pkg, pblocked);
                    pre_roaming.put(pkg, proaming);
                } else if ("relation".equals(xml.getName())) {
                    String pkg = xml.getAttributeValue(null, "package");
                    String[] rel = xml.getAttributeValue(null, "related").split(",");
                    pre_related.put(pkg, rel);
                } else if ("type".equals(xml.getName())) {
                    String pkg = xml.getAttributeValue(null, "package");
                    boolean system = xml.getAttributeBooleanValue(null, "system", true);
                    pre_system.put(pkg, system);
                }
            eventType = xml.next();
        }
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
    // Build rule list
    List<Rule> listRules = new ArrayList<>();
    List<PackageInfo> listPI = getPackages(context);
    // Add root
    PackageInfo root = new PackageInfo();
    root.packageName = "root";
    root.versionCode = Build.VERSION.SDK_INT;
    root.versionName = Build.VERSION.RELEASE;
    root.applicationInfo = new ApplicationInfo();
    root.applicationInfo.uid = 0;
    root.applicationInfo.icon = 0;
    listPI.add(root);
    // Add mediaserver
    PackageInfo media = new PackageInfo();
    media.packageName = "android.media";
    media.versionCode = Build.VERSION.SDK_INT;
    media.versionName = Build.VERSION.RELEASE;
    media.applicationInfo = new ApplicationInfo();
    media.applicationInfo.uid = 1013;
    media.applicationInfo.icon = 0;
    listPI.add(media);
    // Add nobody
    PackageInfo nobody = new PackageInfo();
    nobody.packageName = "nobody";
    nobody.versionCode = Build.VERSION.SDK_INT;
    nobody.versionName = Build.VERSION.RELEASE;
    nobody.applicationInfo = new ApplicationInfo();
    nobody.applicationInfo.uid = 9999;
    nobody.applicationInfo.icon = 0;
    listPI.add(nobody);
    DatabaseHelper dh = DatabaseHelper.getInstance(context);
    for (PackageInfo info : listPI) try {
        Rule rule = new Rule(info, context);
        if (pre_system.containsKey(info.packageName))
            rule.system = pre_system.get(info.packageName);
        if (info.applicationInfo.uid == Process.myUid())
            rule.system = true;
        if (all || ((rule.system ? show_system : show_user) && (show_nointernet || rule.internet) && (show_disabled || rule.enabled))) {
            rule.wifi_default = (pre_wifi_blocked.containsKey(info.packageName) ? pre_wifi_blocked.get(info.packageName) : default_wifi);
            rule.other_default = (pre_other_blocked.containsKey(info.packageName) ? pre_other_blocked.get(info.packageName) : default_other);
            rule.screen_wifi_default = default_screen_wifi;
            rule.screen_other_default = default_screen_other;
            rule.roaming_default = (pre_roaming.containsKey(info.packageName) ? pre_roaming.get(info.packageName) : default_roaming);
            rule.wifi_blocked = (!(rule.system && !manage_system) && wifi.getBoolean(info.packageName, rule.wifi_default));
            rule.other_blocked = (!(rule.system && !manage_system) && other.getBoolean(info.packageName, rule.other_default));
            rule.screen_wifi = screen_wifi.getBoolean(info.packageName, rule.screen_wifi_default) && screen_on;
            rule.screen_other = screen_other.getBoolean(info.packageName, rule.screen_other_default) && screen_on;
            rule.roaming = roaming.getBoolean(info.packageName, rule.roaming_default);
            rule.lockdown = lockdown.getBoolean(info.packageName, false);
            rule.apply = apply.getBoolean(info.packageName, true);
            rule.notify = notify.getBoolean(info.packageName, true);
            // Related packages
            List<String> listPkg = new ArrayList<>();
            if (pre_related.containsKey(info.packageName))
                listPkg.addAll(Arrays.asList(pre_related.get(info.packageName)));
            String[] pkgs = getPackages(info.applicationInfo.uid, context);
            if (pkgs != null && pkgs.length > 1) {
                rule.relateduids = true;
                listPkg.addAll(Arrays.asList(pkgs));
                listPkg.remove(info.packageName);
            }
            rule.related = listPkg.toArray(new String[0]);
            rule.hosts = dh.getHostCount(rule.info.applicationInfo.uid, true);
            rule.updateChanged(default_wifi, default_other, default_roaming);
            listRules.add(rule);
        }
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
    final Collator collator = Collator.getInstance(Locale.getDefault());
    // Case insensitive, process accents etc
    collator.setStrength(Collator.SECONDARY);
    // Sort rule list
    String sort = prefs.getString("sort", "name");
    if ("uid".equals(sort))
        Collections.sort(listRules, new Comparator<Rule>() {

            @Override
            public int compare(Rule rule, Rule other) {
                if (rule.info.applicationInfo.uid < other.info.applicationInfo.uid)
                    return -1;
                else if (rule.info.applicationInfo.uid > other.info.applicationInfo.uid)
                    return 1;
                else {
                    int i = collator.compare(rule.name, other.name);
                    return (i == 0 ? rule.info.packageName.compareTo(other.info.packageName) : i);
                }
            }
        });
    else
        Collections.sort(listRules, new Comparator<Rule>() {

            @Override
            public int compare(Rule rule, Rule other) {
                if (all || rule.changed == other.changed) {
                    int i = collator.compare(rule.name, other.name);
                    return (i == 0 ? rule.info.packageName.compareTo(other.info.packageName) : i);
                }
                return (rule.changed ? -1 : 1);
            }
        });
    return listRules;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) SharedPreferences(android.content.SharedPreferences) HashMap(java.util.HashMap) PackageInfo(android.content.pm.PackageInfo) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) Collator(java.text.Collator) Comparator(java.util.Comparator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 60 with Collator

use of java.text.Collator in project robovm by robovm.

the class OldCollationKeyTest method test_toByteArray.

public void test_toByteArray() {
    // Test for method byte [] java.text.CollationKey.toByteArray()
    Collator collator = Collator.getInstance();
    collator.setStrength(Collator.PRIMARY);
    CollationKey key1 = collator.getCollationKey("abc");
    byte[] bytes = key1.toByteArray();
    assertTrue("Not enough bytes", bytes.length >= 3);
    try {
        collator = new RuleBasedCollator("= 1 , 2 ; 3 , 4 < 5 ; 6 , 7");
    } catch (ParseException e) {
        fail("ParseException");
        return;
    }
    /*
         * CollationElementIterator it =
         * ((RuleBasedCollator)collator).getCollationElementIterator("1234567");
         * int order; while ((order = it.next()) !=
         * CollationElementIterator.NULLORDER) {
         * System.out.println(Integer.toHexString(order)); } for (int i=0; i<bytes.length;
         * i+=2) { System.out.print(Integer.toHexString(bytes[i]) +
         * Integer.toHexString(bytes[i+1]) + " "); } System.out.println();
         */
    // The RI has a different algorithm to generate the collation keys.
    // bytes = collator.getCollationKey("1234567").toByteArray();
    // byte[] result = new byte[] { 0, 2, 0, 2, 0, 2, 0, 0, 0, 3, 0, 3, 0, 1,
    //         0, 2, 0, 2, 0, 0, 0, 4, 0, 4, 0, 1, 0, 1, 0, 2 };
    byte[] bytes1 = collator.getCollationKey("12").toByteArray();
    byte[] bytes2 = collator.getCollationKey("123").toByteArray();
    byte[] bytes3 = collator.getCollationKey("124").toByteArray();
    byte[] bytes4 = collator.getCollationKey("1245").toByteArray();
    byte[] bytes5 = collator.getCollationKey("1245").toByteArray();
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes1, bytes2) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes2, bytes3) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes3, bytes4) < 0);
    assertTrue("returned collation key does not sort correctly", compareUnsignedByteArrays(bytes4, bytes5) == 0);
}
Also used : RuleBasedCollator(java.text.RuleBasedCollator) CollationKey(java.text.CollationKey) ParseException(java.text.ParseException) RuleBasedCollator(java.text.RuleBasedCollator) 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