1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2015-2016, International Business Machines Corporation and 7 * others. All Rights Reserved. 8 ******************************************************************************* 9 */ 10 package ohos.global.icu.impl; 11 12 import java.nio.ByteBuffer; 13 14 import ohos.global.icu.util.UResourceBundle; 15 import ohos.global.icu.util.UResourceTypeMismatchException; 16 17 // Class UResource is named consistently with the public class UResourceBundle, 18 // in case we want to make it public at some point. 19 20 /** 21 * ICU resource bundle key and value types. 22 * @hide exposed on OHOS 23 */ 24 public final class UResource { 25 /** 26 * Represents a resource bundle item's key string. 27 * Avoids object creations as much as possible. 28 * Mutable, not thread-safe. 29 * For permanent storage, use clone() or toString(). 30 * @hide exposed on OHOS 31 */ 32 public static final class Key implements CharSequence, Cloneable, Comparable<Key> { 33 // Stores a reference to the resource bundle key string bytes array, 34 // with an offset of the key, to avoid creating a String object 35 // until one is really needed. 36 // Alternatively, we could try to always just get the key String object, 37 // and cache it in the reader, and see if that performs better or worse. 38 private byte[] bytes; 39 private int offset; 40 private int length; 41 private String s; 42 43 /** 44 * Constructs an empty resource key string object. 45 */ Key()46 public Key() { 47 s = ""; 48 } 49 50 /** 51 * Constructs a resource key object equal to the given string. 52 */ Key(String s)53 public Key(String s) { 54 setString(s); 55 } 56 Key(byte[] keyBytes, int keyOffset, int keyLength)57 private Key(byte[] keyBytes, int keyOffset, int keyLength) { 58 bytes = keyBytes; 59 offset = keyOffset; 60 length = keyLength; 61 } 62 63 /** 64 * Mutates this key for a new NUL-terminated resource key string. 65 * The corresponding ASCII-character bytes are not copied and 66 * must not be changed during the lifetime of this key 67 * (or until the next setBytes() call) 68 * and lifetimes of subSequences created from this key. 69 * 70 * @param keyBytes new key string byte array 71 * @param keyOffset new key string offset 72 */ setBytes(byte[] keyBytes, int keyOffset)73 public Key setBytes(byte[] keyBytes, int keyOffset) { 74 bytes = keyBytes; 75 offset = keyOffset; 76 for (length = 0; keyBytes[keyOffset + length] != 0; ++length) {} 77 s = null; 78 return this; 79 } 80 81 /** 82 * Mutates this key to an empty resource key string. 83 */ setToEmpty()84 public Key setToEmpty() { 85 bytes = null; 86 offset = length = 0; 87 s = ""; 88 return this; 89 } 90 91 /** 92 * Mutates this key to be equal to the given string. 93 */ setString(String s)94 public Key setString(String s) { 95 if (s.isEmpty()) { 96 setToEmpty(); 97 } else { 98 bytes = new byte[s.length()]; 99 offset = 0; 100 length = s.length(); 101 for (int i = 0; i < length; ++i) { 102 char c = s.charAt(i); 103 if (c <= 0x7f) { 104 bytes[i] = (byte)c; 105 } else { 106 throw new IllegalArgumentException('\"' + s + "\" is not an ASCII string"); 107 } 108 } 109 this.s = s; 110 } 111 return this; 112 } 113 114 /** 115 * {@inheritDoc} 116 * Does not clone the byte array. 117 */ 118 @Override clone()119 public Key clone() { 120 try { 121 return (Key)super.clone(); 122 } catch (CloneNotSupportedException cannotOccur) { 123 return null; 124 } 125 } 126 127 @Override charAt(int i)128 public char charAt(int i) { 129 assert(0 <= i && i < length); 130 return (char)bytes[offset + i]; 131 } 132 133 @Override length()134 public int length() { 135 return length; 136 } 137 138 @Override subSequence(int start, int end)139 public Key subSequence(int start, int end) { 140 assert(0 <= start && start < length); 141 assert(start <= end && end <= length); 142 return new Key(bytes, offset + start, end - start); 143 } 144 145 /** 146 * Creates/caches/returns this resource key string as a Java String. 147 */ 148 @Override toString()149 public String toString() { 150 if (s == null) { 151 s = internalSubString(0, length); 152 } 153 return s; 154 } 155 internalSubString(int start, int end)156 private String internalSubString(int start, int end) { 157 StringBuilder sb = new StringBuilder(end - start); 158 for (int i = start; i < end; ++i) { 159 sb.append((char)bytes[offset + i]); 160 } 161 return sb.toString(); 162 } 163 164 /** 165 * Creates a new Java String for a sub-sequence of this resource key string. 166 */ substring(int start)167 public String substring(int start) { 168 assert(0 <= start && start < length); 169 return internalSubString(start, length); 170 } 171 172 /** 173 * Creates a new Java String for a sub-sequence of this resource key string. 174 */ substring(int start, int end)175 public String substring(int start, int end) { 176 assert(0 <= start && start < length); 177 assert(start <= end && end <= length); 178 return internalSubString(start, end); 179 } 180 regionMatches(byte[] otherBytes, int otherOffset, int n)181 private boolean regionMatches(byte[] otherBytes, int otherOffset, int n) { 182 for (int i = 0; i < n; ++i) { 183 if (bytes[offset + i] != otherBytes[otherOffset + i]) { 184 return false; 185 } 186 } 187 return true; 188 } 189 regionMatches(int start, CharSequence cs, int n)190 private boolean regionMatches(int start, CharSequence cs, int n) { 191 for (int i = 0; i < n; ++i) { 192 if (bytes[offset + start + i] != cs.charAt(i)) { 193 return false; 194 } 195 } 196 return true; 197 } 198 199 @Override equals(Object other)200 public boolean equals(Object other) { 201 if (other == null) { 202 return false; 203 } else if (this == other) { 204 return true; 205 } else if (other instanceof Key) { 206 Key otherKey = (Key)other; 207 return length == otherKey.length && 208 regionMatches(otherKey.bytes, otherKey.offset, length); 209 } else { 210 return false; 211 } 212 } 213 contentEquals(CharSequence cs)214 public boolean contentEquals(CharSequence cs) { 215 if (cs == null) { 216 return false; 217 } 218 return this == cs || (cs.length() == length && regionMatches(0, cs, length)); 219 } 220 startsWith(CharSequence cs)221 public boolean startsWith(CharSequence cs) { 222 int csLength = cs.length(); 223 return csLength <= length && regionMatches(0, cs, csLength); 224 } 225 endsWith(CharSequence cs)226 public boolean endsWith(CharSequence cs) { 227 int csLength = cs.length(); 228 return csLength <= length && regionMatches(length - csLength, cs, csLength); 229 } 230 231 /** 232 * @return true if the substring of this key starting from the offset 233 * contains the same characters as the other sequence. 234 */ regionMatches(int start, CharSequence cs)235 public boolean regionMatches(int start, CharSequence cs) { 236 int csLength = cs.length(); 237 return csLength == (length - start) && regionMatches(start, cs, csLength); 238 } 239 240 @Override hashCode()241 public int hashCode() { 242 // Never return s.hashCode(), so that 243 // Key.hashCode() is the same whether we have cached s or not. 244 if (length == 0) { 245 return 0; 246 } 247 248 int h = bytes[offset]; 249 for (int i = 1; i < length; ++i) { 250 h = 37 * h + bytes[offset]; 251 } 252 return h; 253 } 254 255 @Override compareTo(Key other)256 public int compareTo(Key other) { 257 return compareTo((CharSequence)other); 258 } 259 compareTo(CharSequence cs)260 public int compareTo(CharSequence cs) { 261 int csLength = cs.length(); 262 int minLength = length <= csLength ? length : csLength; 263 for (int i = 0; i < minLength; ++i) { 264 int diff = charAt(i) - cs.charAt(i); 265 if (diff != 0) { 266 return diff; 267 } 268 } 269 return length - csLength; 270 } 271 } 272 273 /** 274 * Interface for iterating over a resource bundle array resource. 275 * Does not use Java Iterator to reduce object creations. 276 * @hide exposed on OHOS 277 */ 278 public interface Array { 279 /** 280 * @return The number of items in the array resource. 281 */ getSize()282 public int getSize(); 283 /** 284 * @param i Array item index. 285 * @param value Output-only, receives the value of the i'th item. 286 * @return true if i is non-negative and less than getSize(). 287 */ getValue(int i, Value value)288 public boolean getValue(int i, Value value); 289 } 290 291 /** 292 * Interface for iterating over a resource bundle table resource. 293 * Does not use Java Iterator to reduce object creations. 294 * @hide exposed on OHOS 295 */ 296 public interface Table { 297 /** 298 * @return The number of items in the table resource. 299 */ getSize()300 public int getSize(); 301 /** 302 * @param i Table item index. 303 * @param key Output-only, receives the key of the i'th item. 304 * @param value Output-only, receives the value of the i'th item. 305 * @return true if i is non-negative and less than getSize(). 306 */ getKeyAndValue(int i, Key key, Value value)307 public boolean getKeyAndValue(int i, Key key, Value value); 308 /** 309 * @param key Key string to find in the table. 310 * @param value Output-only, receives the value of the item with that key. 311 * @return true if the table contains the key. 312 */ findValue(CharSequence key, Value value)313 public boolean findValue(CharSequence key, Value value); 314 } 315 316 /** 317 * Represents a resource bundle item's value. 318 * Avoids object creations as much as possible. 319 * Mutable, not thread-safe. 320 * @hide exposed on OHOS 321 */ 322 public static abstract class Value { Value()323 protected Value() {} 324 325 /** 326 * @return ICU resource type like {@link UResourceBundle#getType()}, 327 * for example, {@link UResourceBundle#STRING} 328 */ getType()329 public abstract int getType(); 330 331 /** 332 * @see UResourceBundle#getString() 333 * @throws UResourceTypeMismatchException if this is not a string resource 334 */ getString()335 public abstract String getString(); 336 337 /** 338 * @throws UResourceTypeMismatchException if this is not an alias resource 339 */ getAliasString()340 public abstract String getAliasString(); 341 342 /** 343 * @see UResourceBundle#getInt() 344 * @throws UResourceTypeMismatchException if this is not an integer resource 345 */ getInt()346 public abstract int getInt(); 347 348 /** 349 * @see UResourceBundle#getUInt() 350 * @throws UResourceTypeMismatchException if this is not an integer resource 351 */ getUInt()352 public abstract int getUInt(); 353 354 /** 355 * @see UResourceBundle#getIntVector() 356 * @throws UResourceTypeMismatchException if this is not an intvector resource 357 */ getIntVector()358 public abstract int[] getIntVector(); 359 360 /** 361 * @see UResourceBundle#getBinary() 362 * @throws UResourceTypeMismatchException if this is not a binary-blob resource 363 */ getBinary()364 public abstract ByteBuffer getBinary(); 365 366 /** 367 * @throws UResourceTypeMismatchException if this is not an array resource 368 */ getArray()369 public abstract Array getArray(); 370 371 /** 372 * @throws UResourceTypeMismatchException if this is not a table resource 373 */ getTable()374 public abstract Table getTable(); 375 376 /** 377 * Is this a no-fallback/no-inheritance marker string? 378 * Such a marker is used for CLDR no-fallback data values of "∅∅∅" 379 * when enumerating tables with fallback from the specific resource bundle to root. 380 * 381 * @return true if this is a no-inheritance marker string 382 */ isNoInheritanceMarker()383 public abstract boolean isNoInheritanceMarker(); 384 385 /** 386 * @return the array of strings in this array resource. 387 * @see UResourceBundle#getStringArray() 388 * @throws UResourceTypeMismatchException if this is not an array resource 389 * or if any of the array items is not a string 390 */ getStringArray()391 public abstract String[] getStringArray(); 392 393 /** 394 * Same as 395 * <pre> 396 * if (getType() == STRING) { 397 * return new String[] { getString(); } 398 * } else { 399 * return getStringArray(); 400 * } 401 * </pre> 402 * 403 * @see #getString() 404 * @see #getStringArray() 405 * @throws UResourceTypeMismatchException if this is 406 * neither a string resource nor an array resource containing strings 407 */ getStringArrayOrStringAsArray()408 public abstract String[] getStringArrayOrStringAsArray(); 409 410 /** 411 * Same as 412 * <pre> 413 * if (getType() == STRING) { 414 * return getString(); 415 * } else { 416 * return getStringArray()[0]; 417 * } 418 * </pre> 419 * 420 * @see #getString() 421 * @see #getStringArray() 422 * @throws UResourceTypeMismatchException if this is 423 * neither a string resource nor an array resource containing strings 424 */ getStringOrFirstOfArray()425 public abstract String getStringOrFirstOfArray(); 426 427 /** 428 * Only for debugging. 429 */ 430 @Override toString()431 public String toString() { 432 switch(getType()) { 433 case UResourceBundle.STRING: 434 return getString(); 435 case UResourceBundle.INT: 436 return Integer.toString(getInt()); 437 case UResourceBundle.INT_VECTOR: 438 int[] iv = getIntVector(); 439 StringBuilder sb = new StringBuilder("["); 440 sb.append(iv.length).append("]{"); 441 if (iv.length != 0) { 442 sb.append(iv[0]); 443 for (int i = 1; i < iv.length; ++i) { 444 sb.append(", ").append(iv[i]); 445 } 446 } 447 return sb.append('}').toString(); 448 case UResourceBundle.BINARY: 449 return "(binary blob)"; 450 case UResourceBundle.ARRAY: 451 return "(array)"; 452 case UResourceBundle.TABLE: 453 return "(table)"; 454 default: // should not occur 455 return "???"; 456 } 457 } 458 } 459 460 /** 461 * Sink for ICU resource bundle contents. 462 * @hide exposed on OHOS 463 */ 464 public static abstract class Sink { 465 /** 466 * Called once for each bundle (child-parent-...-root). 467 * The value is normally an array or table resource, 468 * and implementations of this method normally iterate over the 469 * tree of resource items stored there. 470 * 471 * @param key Initially the key string of the enumeration-start resource. 472 * Empty if the enumeration starts at the top level of the bundle. 473 * Reuse for output values from Array and Table getters. 474 * @param value Call getArray() or getTable() as appropriate. 475 * Then reuse for output values from Array and Table getters. 476 * @param noFallback true if the bundle has no parent; 477 * that is, its top-level table has the nofallback attribute, 478 * or it is the root bundle of a locale tree. 479 */ put(Key key, Value value, boolean noFallback)480 public abstract void put(Key key, Value value, boolean noFallback); 481 } 482 } 483