PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/com.ibm.icu.source_4.4.2.v20110208/com/ibm/icu/impl/Norm2AllModes.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 367 lines | 322 code | 25 blank | 20 comment | 26 complexity | 23e7f5b3a8e998132baafae6414715d2 MD5 | raw file
  1. /*
  2. *******************************************************************************
  3. * Copyright (C) 2009-2010, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. *******************************************************************************
  6. */
  7. package com.ibm.icu.impl;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.MissingResourceException;
  11. import com.ibm.icu.text.Normalizer;
  12. import com.ibm.icu.text.Normalizer2;
  13. public final class Norm2AllModes {
  14. // Public API dispatch via Normalizer2 subclasses -------------------------- ***
  15. // Normalizer2 implementation for the old UNORM_NONE.
  16. public static final class NoopNormalizer2 extends Normalizer2 {
  17. @Override
  18. public StringBuilder normalize(CharSequence src, StringBuilder dest) {
  19. if(dest!=src) {
  20. dest.setLength(0);
  21. return dest.append(src);
  22. } else {
  23. throw new IllegalArgumentException();
  24. }
  25. }
  26. @Override
  27. public Appendable normalize(CharSequence src, Appendable dest) {
  28. if(dest!=src) {
  29. try {
  30. return dest.append(src);
  31. } catch(IOException e) {
  32. throw new RuntimeException(e); // Avoid declaring "throws IOException".
  33. }
  34. } else {
  35. throw new IllegalArgumentException();
  36. }
  37. }
  38. @Override
  39. public StringBuilder normalizeSecondAndAppend(StringBuilder first, CharSequence second) {
  40. if(first!=second) {
  41. return first.append(second);
  42. } else {
  43. throw new IllegalArgumentException();
  44. }
  45. }
  46. @Override
  47. public StringBuilder append(StringBuilder first, CharSequence second) {
  48. if(first!=second) {
  49. return first.append(second);
  50. } else {
  51. throw new IllegalArgumentException();
  52. }
  53. }
  54. @Override
  55. public boolean isNormalized(CharSequence s) { return true; }
  56. @Override
  57. public Normalizer.QuickCheckResult quickCheck(CharSequence s) { return Normalizer.YES; }
  58. @Override
  59. public int spanQuickCheckYes(CharSequence s) { return s.length(); }
  60. @Override
  61. public boolean hasBoundaryBefore(int c) { return true; }
  62. @Override
  63. public boolean hasBoundaryAfter(int c) { return true; }
  64. @Override
  65. public boolean isInert(int c) { return true; }
  66. }
  67. // Intermediate class:
  68. // Has Normalizer2Impl and does boilerplate argument checking and setup.
  69. public static abstract class Normalizer2WithImpl extends Normalizer2 {
  70. public Normalizer2WithImpl(Normalizer2Impl ni) {
  71. impl=ni;
  72. }
  73. // normalize
  74. @Override
  75. public StringBuilder normalize(CharSequence src, StringBuilder dest) {
  76. if(dest==src) {
  77. throw new IllegalArgumentException();
  78. }
  79. dest.setLength(0);
  80. normalize(src, new Normalizer2Impl.ReorderingBuffer(impl, dest, src.length()));
  81. return dest;
  82. }
  83. @Override
  84. public Appendable normalize(CharSequence src, Appendable dest) {
  85. if(dest==src) {
  86. throw new IllegalArgumentException();
  87. }
  88. Normalizer2Impl.ReorderingBuffer buffer=
  89. new Normalizer2Impl.ReorderingBuffer(impl, dest, src.length());
  90. normalize(src, buffer);
  91. buffer.flush();
  92. return dest;
  93. }
  94. protected abstract void normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer);
  95. // normalize and append
  96. @Override
  97. public StringBuilder normalizeSecondAndAppend(StringBuilder first, CharSequence second) {
  98. return normalizeSecondAndAppend(first, second, true);
  99. }
  100. @Override
  101. public StringBuilder append(StringBuilder first, CharSequence second) {
  102. return normalizeSecondAndAppend(first, second, false);
  103. }
  104. public StringBuilder normalizeSecondAndAppend(
  105. StringBuilder first, CharSequence second, boolean doNormalize) {
  106. if(first==second) {
  107. throw new IllegalArgumentException();
  108. }
  109. normalizeAndAppend(
  110. second, doNormalize,
  111. new Normalizer2Impl.ReorderingBuffer(impl, first, first.length()+second.length()));
  112. return first;
  113. }
  114. protected abstract void normalizeAndAppend(
  115. CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer);
  116. // quick checks
  117. @Override
  118. public boolean isNormalized(CharSequence s) {
  119. return s.length()==spanQuickCheckYes(s);
  120. }
  121. @Override
  122. public Normalizer.QuickCheckResult quickCheck(CharSequence s) {
  123. return isNormalized(s) ? Normalizer.YES : Normalizer.NO;
  124. }
  125. public int getQuickCheck(int c) {
  126. return 1;
  127. }
  128. public final Normalizer2Impl impl;
  129. }
  130. public static final class DecomposeNormalizer2 extends Normalizer2WithImpl {
  131. public DecomposeNormalizer2(Normalizer2Impl ni) {
  132. super(ni);
  133. }
  134. @Override
  135. protected void normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer) {
  136. impl.decompose(src, 0, src.length(), buffer);
  137. }
  138. @Override
  139. protected void normalizeAndAppend(
  140. CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer) {
  141. impl.decomposeAndAppend(src, doNormalize, buffer);
  142. }
  143. @Override
  144. public int spanQuickCheckYes(CharSequence s) {
  145. return impl.decompose(s, 0, s.length(), null);
  146. }
  147. @Override
  148. public int getQuickCheck(int c) {
  149. return impl.isDecompYes(impl.getNorm16(c)) ? 1 : 0;
  150. }
  151. @Override
  152. public boolean hasBoundaryBefore(int c) { return impl.hasDecompBoundary(c, true); }
  153. @Override
  154. public boolean hasBoundaryAfter(int c) { return impl.hasDecompBoundary(c, false); }
  155. @Override
  156. public boolean isInert(int c) { return impl.isDecompInert(c); }
  157. }
  158. public static final class ComposeNormalizer2 extends Normalizer2WithImpl {
  159. public ComposeNormalizer2(Normalizer2Impl ni, boolean fcc) {
  160. super(ni);
  161. onlyContiguous=fcc;
  162. }
  163. @Override
  164. protected void normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer) {
  165. impl.compose(src, 0, src.length(), onlyContiguous, true, buffer);
  166. }
  167. @Override
  168. protected void normalizeAndAppend(
  169. CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer) {
  170. impl.composeAndAppend(src, doNormalize, onlyContiguous, buffer);
  171. }
  172. @Override
  173. public boolean isNormalized(CharSequence s) {
  174. // 5: small destCapacity for substring normalization
  175. return impl.compose(s, 0, s.length(),
  176. onlyContiguous, false,
  177. new Normalizer2Impl.ReorderingBuffer(impl, new StringBuilder(), 5));
  178. }
  179. @Override
  180. public Normalizer.QuickCheckResult quickCheck(CharSequence s) {
  181. int spanLengthAndMaybe=impl.composeQuickCheck(s, 0, s.length(), onlyContiguous, false);
  182. if((spanLengthAndMaybe&1)!=0) {
  183. return Normalizer.MAYBE;
  184. } else if((spanLengthAndMaybe>>>1)==s.length()) {
  185. return Normalizer.YES;
  186. } else {
  187. return Normalizer.NO;
  188. }
  189. }
  190. @Override
  191. public int spanQuickCheckYes(CharSequence s) {
  192. return impl.composeQuickCheck(s, 0, s.length(), onlyContiguous, true)>>>1;
  193. }
  194. @Override
  195. public int getQuickCheck(int c) {
  196. return impl.getCompQuickCheck(impl.getNorm16(c));
  197. }
  198. @Override
  199. public boolean hasBoundaryBefore(int c) { return impl.hasCompBoundaryBefore(c); }
  200. @Override
  201. public boolean hasBoundaryAfter(int c) {
  202. return impl.hasCompBoundaryAfter(c, onlyContiguous, false);
  203. }
  204. @Override
  205. public boolean isInert(int c) {
  206. return impl.hasCompBoundaryAfter(c, onlyContiguous, true);
  207. }
  208. private final boolean onlyContiguous;
  209. }
  210. public static final class FCDNormalizer2 extends Normalizer2WithImpl {
  211. public FCDNormalizer2(Normalizer2Impl ni) {
  212. super(ni);
  213. }
  214. @Override
  215. protected void normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer) {
  216. impl.makeFCD(src, 0, src.length(), buffer);
  217. }
  218. @Override
  219. protected void normalizeAndAppend(
  220. CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer) {
  221. impl.makeFCDAndAppend(src, doNormalize, buffer);
  222. }
  223. @Override
  224. public int spanQuickCheckYes(CharSequence s) {
  225. return impl.makeFCD(s, 0, s.length(), null);
  226. }
  227. @Override
  228. public int getQuickCheck(int c) {
  229. return impl.isDecompYes(impl.getNorm16(c)) ? 1 : 0;
  230. }
  231. @Override
  232. public boolean hasBoundaryBefore(int c) { return impl.hasFCDBoundaryBefore(c); }
  233. @Override
  234. public boolean hasBoundaryAfter(int c) { return impl.hasFCDBoundaryAfter(c); }
  235. @Override
  236. public boolean isInert(int c) { return impl.isFCDInert(c); }
  237. }
  238. // instance cache ---------------------------------------------------------- ***
  239. private Norm2AllModes(Normalizer2Impl ni) {
  240. impl=ni;
  241. comp=new ComposeNormalizer2(ni, false);
  242. decomp=new DecomposeNormalizer2(ni);
  243. fcd=new FCDNormalizer2(ni);
  244. fcc=new ComposeNormalizer2(ni, true);
  245. }
  246. public final Normalizer2Impl impl;
  247. public final ComposeNormalizer2 comp;
  248. public final DecomposeNormalizer2 decomp;
  249. public final FCDNormalizer2 fcd;
  250. public final ComposeNormalizer2 fcc;
  251. private static Norm2AllModes getInstanceFromSingleton(Norm2AllModesSingleton singleton) {
  252. if(singleton.exception!=null) {
  253. throw singleton.exception;
  254. }
  255. return singleton.allModes;
  256. }
  257. public static Norm2AllModes getNFCInstance() {
  258. return getInstanceFromSingleton(NFCSingleton.INSTANCE);
  259. }
  260. public static Norm2AllModes getNFKCInstance() {
  261. return getInstanceFromSingleton(NFKCSingleton.INSTANCE);
  262. }
  263. public static Norm2AllModes getNFKC_CFInstance() {
  264. return getInstanceFromSingleton(NFKC_CFSingleton.INSTANCE);
  265. }
  266. // For use in properties APIs.
  267. public static Normalizer2WithImpl getN2WithImpl(int index) {
  268. switch(index) {
  269. case 0: return getNFCInstance().decomp; // NFD
  270. case 1: return getNFKCInstance().decomp; // NFKD
  271. case 2: return getNFCInstance().comp; // NFC
  272. case 3: return getNFKCInstance().comp; // NFKC
  273. default: return null;
  274. }
  275. }
  276. public static Norm2AllModes getInstance(InputStream data, String name) {
  277. if(data==null) {
  278. Norm2AllModesSingleton singleton;
  279. if(name.equals("nfc")) {
  280. singleton=NFCSingleton.INSTANCE;
  281. } else if(name.equals("nfkc")) {
  282. singleton=NFKCSingleton.INSTANCE;
  283. } else if(name.equals("nfkc_cf")) {
  284. singleton=NFKC_CFSingleton.INSTANCE;
  285. } else {
  286. singleton=null;
  287. }
  288. if(singleton!=null) {
  289. if(singleton.exception!=null) {
  290. throw singleton.exception;
  291. }
  292. return singleton.allModes;
  293. }
  294. }
  295. return cache.getInstance(name, data);
  296. }
  297. private static CacheBase<String, Norm2AllModes, InputStream> cache =
  298. new SoftCache<String, Norm2AllModes, InputStream>() {
  299. protected Norm2AllModes createInstance(String key, InputStream data) {
  300. if(data==null) {
  301. throw new MissingResourceException(
  302. "No Normalizer2 data name \""+key+"\" cached, and InputStream is null",
  303. "Normalizer2",
  304. key);
  305. }
  306. Normalizer2Impl impl=new Normalizer2Impl().load(data);
  307. return new Norm2AllModes(impl);
  308. }
  309. };
  310. public static final NoopNormalizer2 NOOP_NORMALIZER2=new NoopNormalizer2();
  311. /**
  312. * Gets the FCD normalizer, with the FCD data initialized.
  313. * @return FCD normalizer
  314. */
  315. public static Normalizer2 getFCDNormalizer2() {
  316. Norm2AllModes allModes=getNFCInstance();
  317. allModes.impl.getFCDTrie();
  318. return allModes.fcd;
  319. }
  320. private static final class Norm2AllModesSingleton {
  321. private Norm2AllModesSingleton(String name) {
  322. try {
  323. Normalizer2Impl impl=new Normalizer2Impl().load(
  324. ICUResourceBundle.ICU_BUNDLE+"/"+name+".nrm");
  325. allModes=new Norm2AllModes(impl);
  326. } catch(RuntimeException e) {
  327. exception=e;
  328. }
  329. }
  330. private Norm2AllModes allModes;
  331. private RuntimeException exception;
  332. }
  333. private static final class NFCSingleton {
  334. private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfc");
  335. }
  336. private static final class NFKCSingleton {
  337. private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfkc");
  338. }
  339. private static final class NFKC_CFSingleton {
  340. private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfkc_cf");
  341. }
  342. }