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