1/*
2*******************************************************************************
3* Copyright (C) 2013-2015, International Business Machines
4* Corporation and others.  All Rights Reserved.
5*******************************************************************************
6* CollationTailoring.java, ported from collationtailoring.h/.cpp
7*
8* C++ version created on: 2013mar12
9* created by: Markus W. Scherer
10*/
11
12package com.ibm.icu.impl.coll;
13
14import java.util.Map;
15
16import com.ibm.icu.impl.Norm2AllModes;
17import com.ibm.icu.impl.Normalizer2Impl;
18import com.ibm.icu.impl.Trie2_32;
19import com.ibm.icu.text.UnicodeSet;
20import com.ibm.icu.util.ULocale;
21import com.ibm.icu.util.VersionInfo;
22
23/**
24 * Collation tailoring data & settings.
25 * This is a container of values for a collation tailoring
26 * built from rules or deserialized from binary data.
27 *
28 * It is logically immutable: Do not modify its values.
29 * The fields are public for convenience.
30 */
31public final class CollationTailoring {
32    CollationTailoring(SharedObject.Reference<CollationSettings> baseSettings) {
33        if(baseSettings != null) {
34            assert(baseSettings.readOnly().reorderCodes.length == 0);
35            assert(baseSettings.readOnly().reorderTable == null);
36            assert(baseSettings.readOnly().minHighNoReorder == 0);
37            settings = baseSettings.clone();
38        } else {
39            settings = new SharedObject.Reference<CollationSettings>(new CollationSettings());
40        }
41    }
42
43    void ensureOwnedData() {
44        if(ownedData == null) {
45            Normalizer2Impl nfcImpl = Norm2AllModes.getNFCInstance().impl;
46            ownedData = new CollationData(nfcImpl);
47        }
48        data = ownedData;
49    }
50
51    static VersionInfo makeBaseVersion(VersionInfo ucaVersion) {
52        return VersionInfo.getInstance(
53                VersionInfo.UCOL_BUILDER_VERSION.getMajor(),
54                (ucaVersion.getMajor() << 3) + ucaVersion.getMinor(),
55                ucaVersion.getMilli() << 6,
56                0);
57    }
58    void setVersion(int baseVersion, int rulesVersion) {
59        // See comments for version field.
60        int r = (rulesVersion >> 16) & 0xff00;
61        int s = (rulesVersion >> 16) & 0xff;
62        int t = (rulesVersion >> 8) & 0xff;
63        int q = rulesVersion & 0xff;
64        version = (VersionInfo.UCOL_BUILDER_VERSION.getMajor() << 24) |
65                (baseVersion & 0xffc000) |  // UCA version u.v.w
66                ((r + (r >> 6)) & 0x3f00) |
67                (((s << 3) + (s >> 5) + t + (q << 4) + (q >> 4)) & 0xff);
68    }
69    int getUCAVersion() {
70        // Version second byte/bits 23..16 to bits 11..4,
71        // third byte/bits 15..14 to bits 1..0.
72        return ((version >> 12) & 0xff0) | ((version >> 14) & 3);
73    }
74
75    // data for sorting etc.
76    public CollationData data;  // == base data or ownedData
77    public SharedObject.Reference<CollationSettings> settings;  // reference-counted
78    public String rules = "";
79    // The locale is null (C++: bogus) when built from rules or constructed from a binary blob.
80    // It can then be set by the service registration code which is thread-safe.
81    public ULocale actualLocale = ULocale.ROOT;
82    // UCA version u.v.w & rules version r.s.t.q:
83    // version[0]: builder version (runtime version is mixed in at runtime)
84    // version[1]: bits 7..3=u, bits 2..0=v
85    // version[2]: bits 7..6=w, bits 5..0=r
86    // version[3]= (s<<5)+(s>>3)+t+(q<<4)+(q>>4)
87    public int version = 0;
88
89    // owned objects
90    CollationData ownedData;
91    Trie2_32 trie;
92    UnicodeSet unsafeBackwardSet;
93    public Map<Integer, Integer> maxExpansions;
94
95    /*
96     * Not Cloneable: A CollationTailoring cannot be copied.
97     * It is immutable, and the data trie cannot be copied either.
98     */
99}
100