Java Code Examples for com.ibm.icu.text.RuleBasedCollator#setStrength()

The following examples show how to use com.ibm.icu.text.RuleBasedCollator#setStrength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollationSpecifier.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void setCollatorStrength(RuleBasedCollator collator, CollationSpecifier specifier) {
    if (specifier.caseSensitive() && specifier.accentSensitive()) {
        collator.setStrength(Collator.TERTIARY);
        collator.setCaseLevel(false);
    }
    else if (specifier.caseSensitive() && !specifier.accentSensitive()) {
        collator.setCaseLevel(true);
        collator.setStrength(Collator.PRIMARY);
    }
    else if (!specifier.caseSensitive() && specifier.accentSensitive()) {
        collator.setStrength(Collator.SECONDARY);
        collator.setCaseLevel(false);
    }
    else {
        collator.setStrength(Collator.PRIMARY);
        collator.setCaseLevel(false);
    }
}
 
Example 2
Source File: TestICUPortabilityBug.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Unit test for ICU generation of Unicode sort keys.
 * <pre>
 * Input   : "__globalRowStore"
 * 
 * Expected: [7, -124, 7, -124, 53, 63, 69, 43, 41, 63, 75, 69, 85, 77, 79, 69, 75, 49, 1, 20, 1, 126, -113, -124, -113, 8]
 * </pre>
 */
public void test_ICU_Unicode_SortKey() {
    
    final String input = "__globalRowStore";

    // Buffer reused for each String from which a sort key is derived.
    final RawCollationKey raw = new RawCollationKey(128);

    /*
     * Setup the collator by specifying the locale, strength, and
     * decomposition mode.
     */
    final Locale locale = new Locale("en", "US");
    
    final RuleBasedCollator collator = (RuleBasedCollator) Collator
            .getInstance(locale);

    collator.setStrength(Collator.TERTIARY);

    collator.setDecomposition(Collator.NO_DECOMPOSITION);

    collator.getRawCollationKey(input, raw);

    // do not include the nul byte
    final byte[] actual = new byte[raw.size - 1];

    // copy data from the buffer.
    System.arraycopy(raw.bytes/* src */, 0/* srcPos */, actual/* dest */,
            0/* destPos */, actual.length);

    if (log.isInfoEnabled()) {
        log.info("Actual  : " + Arrays.toString(actual));
    }
    
    /*
     * The expected Unicode sort key (this depends on the runtime ICU
     * version).
     */
    final byte[] expected;
    if (VersionInfo.ICU_VERSION.getMajor() == 3
            && VersionInfo.ICU_VERSION.getMinor() == 6) {
        /*
         * bigdata was initially deployed against v3.6.
         */
        expected = new byte[] { 7, -124, 7, -124, 53, 63, 69, 43, 41, 63,
                75, 69, 85, 77, 79, 69, 75, 49, 1, 20, 1, 126, -113, -124,
                -113, 8 };
    } else if (VersionInfo.ICU_VERSION.getMajor() == 4
            && VersionInfo.ICU_VERSION.getMinor() == 8) {
        /*
         * The next bundled version was 4.8.
         */
        expected = new byte[] { 6, 12, 6, 12, 51, 61, 67, 41, 39, 61, 73,
                67, 83, 75, 77, 67, 73, 47, 1, 20, 1, 126, -113, -124,
                -113, 8};
    } else {

        throw new AssertionFailedError("Not an expected ICU version: "
                + VersionInfo.ICU_VERSION);

    }

    if (log.isInfoEnabled()) {
        log.info("Expected: " + Arrays.toString(expected));
    }

    if (!Arrays.equals(expected, actual)) {
        fail("Expected: " + Arrays.toString(expected) + ", " + //
                "Actual: " + Arrays.toString(actual));
    }

}
 
Example 3
Source File: BlobDescriptorList.java    From aard2-android with GNU General Public License v3.0 4 votes vote down vote up
BlobDescriptorList(Application app, DescriptorStore<BlobDescriptor> store, int maxSize) {
    this.app = app;
    this.store = store;
    this.maxSize = maxSize;
    this.list = new ArrayList<BlobDescriptor>();
    this.filteredList = new ArrayList<BlobDescriptor>();
    this.dataSetObservable = new DataSetObservable();
    this.filter = "";
    keyComparator = Slob.Strength.QUATERNARY.comparator;

    nameComparatorAsc = new Comparator<BlobDescriptor>() {
        @Override
        public int compare(BlobDescriptor b1, BlobDescriptor b2) {
        return keyComparator.compare(b1.key, b2.key);
        }
    };

    nameComparatorDesc = Collections.reverseOrder(nameComparatorAsc);

    timeComparatorAsc = new Comparator<BlobDescriptor>() {
        @Override
        public int compare(BlobDescriptor b1, BlobDescriptor b2) {
        return Util.compare(b1.createdAt, b2.createdAt);
        }
    };

    timeComparatorDesc = Collections.reverseOrder(timeComparatorAsc);

    lastAccessComparator = new Comparator<BlobDescriptor>() {
        @Override
        public int compare(BlobDescriptor b1, BlobDescriptor b2) {
            return  Util.compare(b2.lastAccess, b1.lastAccess);
        }
    };

    order = SortOrder.TIME;
    ascending = false;
    setSort(order, ascending);

    try {
        filterCollator = (RuleBasedCollator) Collator.getInstance(Locale.ROOT).clone();
    } catch (CloneNotSupportedException e) {
        throw new RuntimeException(e);
    }
    filterCollator.setStrength(Collator.PRIMARY);
    filterCollator.setAlternateHandlingShifted(true);
    handler = new Handler(Looper.getMainLooper());
}
 
Example 4
Source File: CollatorObject.java    From es6draft with MIT License 4 votes vote down vote up
private Collator createCollator() {
    ULocale locale = ULocale.forLanguageTag(this.locale);
    if ("search".equals(usage)) {
        // "search" usage cannot be set through unicode extensions (u-co-search), handle here:
        locale = locale.setKeywordValue("collation", "search");
    }
    RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(locale);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    collator.setNumericCollation(numeric);
    switch (caseFirst) {
    case "upper":
        collator.setUpperCaseFirst(true);
        break;
    case "lower":
        collator.setLowerCaseFirst(true);
        break;
    case "false":
        if (collator.isLowerCaseFirst()) {
            collator.setLowerCaseFirst(false);
        }
        if (collator.isUpperCaseFirst()) {
            collator.setUpperCaseFirst(false);
        }
        break;
    default:
        throw new AssertionError();
    }
    switch (sensitivity) {
    case "base":
        collator.setStrength(Collator.PRIMARY);
        break;
    case "accent":
        collator.setStrength(Collator.SECONDARY);
        break;
    case "case":
        collator.setStrength(Collator.PRIMARY);
        collator.setCaseLevel(true);
        break;
    case "variant":
        collator.setStrength(Collator.TERTIARY);
        break;
    default:
        throw new AssertionError();
    }
    collator.setAlternateHandlingShifted(ignorePunctuation);
    return collator;
}