Home | History | Annotate | Download | only in coll
      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) 2012-2014, International Business Machines
      6 * Corporation and others.  All Rights Reserved.
      7 *******************************************************************************
      8 * CollationRoot.java, ported from collationroot.h/.cpp
      9 *
     10 * C++ version created on: 2012dec17
     11 * created by: Markus W. Scherer
     12 */
     13 
     14 package com.ibm.icu.impl.coll;
     15 
     16 import java.io.IOException;
     17 import java.nio.ByteBuffer;
     18 import java.util.MissingResourceException;
     19 
     20 import com.ibm.icu.impl.ICUBinary;
     21 import com.ibm.icu.impl.ICUData;
     22 
     23 /**
     24  * Collation root provider.
     25  */
     26 public final class CollationRoot {  // purely static
     27     private static final CollationTailoring rootSingleton;
     28     private static final RuntimeException exception;
     29 
     30     public static final CollationTailoring getRoot() {
     31         if(exception != null) {
     32             throw exception;
     33         }
     34         return rootSingleton;
     35     }
     36     public static final CollationData getData() {
     37         CollationTailoring root = getRoot();
     38         return root.data;
     39     }
     40     static final CollationSettings getSettings() {
     41         CollationTailoring root = getRoot();
     42         return root.settings.readOnly();
     43     }
     44 
     45     static {  // Corresponds to C++ load() function.
     46         CollationTailoring t = null;
     47         RuntimeException e2 = null;
     48         try {
     49             ByteBuffer bytes = ICUBinary.getRequiredData("coll/ucadata.icu");
     50             CollationTailoring t2 = new CollationTailoring(null);
     51             CollationDataReader.read(null, bytes, t2);
     52             // Keep t=null until after the root data has been read completely.
     53             // Otherwise we would set a non-null root object if the data reader throws an exception.
     54             t = t2;
     55         } catch(IOException e) {
     56             e2 = new MissingResourceException(
     57                     "IOException while reading CLDR root data",
     58                     "CollationRoot", ICUData.ICU_BUNDLE + "/coll/ucadata.icu");
     59         } catch(RuntimeException e) {
     60             e2 = e;
     61         }
     62         rootSingleton = t;
     63         exception = e2;
     64     }
     65 }
     66