• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) 2016, International Business Machines Corporation and         *
7  * others. All Rights Reserved.                                                *
8  *******************************************************************************
9  */
10 package ohos.global.icu.text;
11 
12 import java.text.CharacterIterator;
13 
14 import ohos.global.icu.impl.CharacterIteration;
15 import ohos.global.icu.lang.UCharacter;
16 import ohos.global.icu.lang.UProperty;
17 
18 final class UnhandledBreakEngine implements LanguageBreakEngine {
19     // TODO: Use two UnicodeSets, one with all frozen sets, one with unfrozen.
20     // in handleChar(), update the unfrozen version, clone, freeze, replace the frozen one.
21 
22     // Note on concurrency: A single instance of UnhandledBreakEngine is shared across all
23     // RuleBasedBreakIterators in a process. They may make arbitrary concurrent calls.
24     // If handleChar() is updating the set of unhandled characters at the same time
25     // findBreaks() or handles() is referencing it, the referencing functions must see
26     // a consistent set. It doesn't matter whether they see it before or after the update,
27     // but they should not see an inconsistent, changing set.
28     //
29     // To do this, an update is made by cloning the old set, updating the clone, then
30     // replacing the old with the new. Once made visible, each set remains constant.
31 
32     // TODO: it's odd that findBreaks() can produce different results, depending
33     // on which scripts have been previously seen by handleChar(). (This is not a
34     // threading specific issue). Possibly stop on script boundaries?
35 
36     volatile UnicodeSet fHandled = new UnicodeSet();
UnhandledBreakEngine()37     public UnhandledBreakEngine() {
38     }
39 
40     @Override
handles(int c)41     public boolean handles(int c) {
42         return fHandled.contains(c);
43     }
44 
45     @Override
findBreaks(CharacterIterator text, int startPos, int endPos, DictionaryBreakEngine.DequeI foundBreaks)46     public int findBreaks(CharacterIterator text, int startPos, int endPos,
47             DictionaryBreakEngine.DequeI foundBreaks) {
48 
49         UnicodeSet uniset = fHandled;
50         int c = CharacterIteration.current32(text);
51         while (text.getIndex() < endPos && uniset.contains(c)) {
52             CharacterIteration.next32(text);
53             c = CharacterIteration.current32(text);
54         }
55         return 0;
56     }
57 
58     /**
59      * Update the set of unhandled characters to include
60      * all that have the same script as c.
61      * May be called concurrently with handles() or findBreaks().
62      * Must not be called concurrently with itself.
63      */
handleChar(int c)64     public void handleChar(int c) {
65         UnicodeSet originalSet = fHandled;
66         if (!originalSet.contains(c)) {
67             int script = UCharacter.getIntPropertyValue(c, UProperty.SCRIPT);
68             UnicodeSet newSet = new UnicodeSet();
69             newSet.applyIntPropertyValue(UProperty.SCRIPT, script);
70             newSet.addAll(originalSet);
71             fHandled = newSet;
72         }
73     }
74 }
75