1 7 8 package com.ibm.icu.text; 9 10 import java.lang.ref.SoftReference ; 11 import java.text.CharacterIterator ; 12 import java.text.StringCharacterIterator ; 13 import java.util.Locale ; 14 import java.util.MissingResourceException ; 15 16 import com.ibm.icu.impl.ICUDebug; 17 import com.ibm.icu.util.ULocale; 18 19 203 204 public abstract class BreakIterator implements Cloneable  205 { 206 207 private static final boolean DEBUG = ICUDebug.enabled("breakiterator"); 208 209 214 protected BreakIterator() 215 { 216 } 217 218 224 public Object clone() 225 { 226 try { 227 return super.clone(); 228 } 229 catch (CloneNotSupportedException e) { 230 throw new IllegalStateException (); 232 } 234 } 235 236 241 public static final int DONE = -1; 242 243 253 public abstract int first(); 254 255 265 public abstract int last(); 266 267 281 public abstract int next(int n); 282 283 294 public abstract int next(); 295 296 307 public abstract int previous(); 308 309 322 public abstract int following(int offset); 323 324 337 public int preceding(int offset) { 338 int pos = following(offset); 342 while (pos >= offset && pos != DONE) 343 pos = previous(); 344 return pos; 345 } 346 347 356 public boolean isBoundary(int offset) { 357 if (offset == 0) { 361 return true; 362 } 363 else 364 return following(offset - 1) == offset; 365 } 366 367 372 public abstract int current(); 373 374 385 public abstract CharacterIterator getText(); 386 387 396 public void setText(String newText) 397 { 398 setText(new StringCharacterIterator(newText)); 399 } 400 401 412 public abstract void setText(CharacterIterator newText); 413 414 415 public static final int KIND_CHARACTER = 0; 416 417 public static final int KIND_WORD = 1; 418 419 public static final int KIND_LINE = 2; 420 421 public static final int KIND_SENTENCE = 3; 422 423 public static final int KIND_TITLE = 4; 424 425 426 private static final int KIND_COUNT = 5; 427 428 429 private static final SoftReference [] iterCache = new SoftReference [5]; 430 431 438 public static BreakIterator getWordInstance() 439 { 440 return getWordInstance(ULocale.getDefault()); 441 } 442 443 450 public static BreakIterator getWordInstance(Locale where) 451 { 452 return getBreakInstance(ULocale.forLocale(where), KIND_WORD); 453 } 454 455 463 public static BreakIterator getWordInstance(ULocale where) 464 { 465 return getBreakInstance(where, KIND_WORD); 466 } 467 468 476 public static BreakIterator getLineInstance() 477 { 478 return getLineInstance(ULocale.getDefault()); 479 } 480 481 489 public static BreakIterator getLineInstance(Locale where) 490 { 491 return getBreakInstance(ULocale.forLocale(where), KIND_LINE); 492 } 493 494 503 public static BreakIterator getLineInstance(ULocale where) 504 { 505 return getBreakInstance(where, KIND_LINE); 506 } 507 508 516 public static BreakIterator getCharacterInstance() 517 { 518 return getCharacterInstance(ULocale.getDefault()); 519 } 520 521 529 public static BreakIterator getCharacterInstance(Locale where) 530 { 531 return getBreakInstance(ULocale.forLocale(where), KIND_CHARACTER); 532 } 533 534 543 public static BreakIterator getCharacterInstance(ULocale where) 544 { 545 return getBreakInstance(where, KIND_CHARACTER); 546 } 547 548 555 public static BreakIterator getSentenceInstance() 556 { 557 return getSentenceInstance(ULocale.getDefault()); 558 } 559 560 566 public static BreakIterator getSentenceInstance(Locale where) 567 { 568 return getBreakInstance(ULocale.forLocale(where), KIND_SENTENCE); 569 } 570 571 578 public static BreakIterator getSentenceInstance(ULocale where) 579 { 580 return getBreakInstance(where, KIND_SENTENCE); 581 } 582 583 592 public static BreakIterator getTitleInstance() 593 { 594 return getTitleInstance(ULocale.getDefault()); 595 } 596 597 606 public static BreakIterator getTitleInstance(Locale where) 607 { 608 return getBreakInstance(ULocale.forLocale(where), KIND_TITLE); 609 } 610 611 621 public static BreakIterator getTitleInstance(ULocale where) 622 { 623 return getBreakInstance(where, KIND_TITLE); 624 } 625 626 637 public static Object registerInstance(BreakIterator iter, Locale locale, int kind) { 638 return registerInstance(iter, ULocale.forLocale(locale), kind); 639 } 640 641 653 public static Object registerInstance(BreakIterator iter, ULocale locale, int kind) { 654 if (iterCache[kind] != null) { 657 BreakIteratorCache cache = (BreakIteratorCache) iterCache[kind].get(); 658 if (cache != null) { 659 if (cache.getLocale().equals(locale)) { 660 iterCache[kind] = null; 661 } 662 } 663 } 664 return getShim().registerInstance(iter, locale, kind); 665 } 666 667 674 public static boolean unregister(Object key) { 675 if (key == null) { 676 throw new IllegalArgumentException ("registry key must not be null"); 677 } 678 if (shim != null) { 688 for (int kind=0; kind<KIND_COUNT; ++kind) { 693 iterCache[kind] = null; 694 } 695 return shim.unregister(key); 696 } 697 return false; 698 } 700 701 703 709 public static BreakIterator getBreakInstance(ULocale where, int kind) { 710 711 if (iterCache[kind] != null) { 712 BreakIteratorCache cache = (BreakIteratorCache) iterCache[kind].get(); 713 if (cache != null) { 714 if (cache.getLocale().equals(where)) { 715 return cache.createBreakInstance(); 716 } 717 } 718 } 719 720 BreakIterator result = getShim().createBreakIterator(where, kind); 722 723 BreakIteratorCache cache = new BreakIteratorCache(where, result); 724 iterCache[kind] = new SoftReference (cache); 725 return result; 726 } 727 728 729 735 public static synchronized Locale [] getAvailableLocales() 736 { 737 return getShim().getAvailableLocales(); 739 } 740 741 748 public static synchronized ULocale[] getAvailableULocales() 749 { 750 return getShim().getAvailableULocales(); 752 } 753 754 private static final class BreakIteratorCache { 755 756 private BreakIterator iter; 757 private ULocale where; 758 759 BreakIteratorCache(ULocale where, BreakIterator iter) { 760 this.where = where; 761 this.iter = (BreakIterator) iter.clone(); 762 } 763 764 ULocale getLocale() { 765 return where; 766 } 767 768 BreakIterator createBreakInstance() { 769 return (BreakIterator) iter.clone(); 770 } 771 } 772 773 static abstract class BreakIteratorServiceShim { 774 public abstract Object registerInstance(BreakIterator iter, ULocale l, int k); 775 public abstract boolean unregister(Object key); 776 public abstract Locale [] getAvailableLocales(); 777 public abstract ULocale[] getAvailableULocales(); 778 public abstract BreakIterator createBreakIterator(ULocale l, int k); 779 } 780 781 private static BreakIteratorServiceShim shim; 782 private static BreakIteratorServiceShim getShim() { 783 if (shim == null) { 788 try { 789 Class cls = Class.forName("com.ibm.icu.text.BreakIteratorFactory"); 790 shim = (BreakIteratorServiceShim)cls.newInstance(); 791 } 792 catch (MissingResourceException e) 793 { 794 throw e; 795 } 796 catch (Exception e) { 797 if(DEBUG){ 799 e.printStackTrace(); 800 } 801 throw new RuntimeException (e.getMessage()); 802 } 804 } 805 return shim; 806 } 807 808 810 834 public final ULocale getLocale(ULocale.Type type) { 835 return type == ULocale.ACTUAL_LOCALE ? 836 this.actualLocale : this.validLocale; 837 } 838 839 856 final void setLocale(ULocale valid, ULocale actual) { 857 if ((valid == null) != (actual == null)) { 859 throw new IllegalArgumentException (); 861 } 863 this.validLocale = valid; 866 this.actualLocale = actual; 867 } 868 869 874 private ULocale validLocale; 875 876 882 private ULocale actualLocale; 883 884 } 886 | Popular Tags |