Skip to content

Commit

Permalink
ICU-22564 update interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankYFTang committed Dec 5, 2023
1 parent c967994 commit d14c134
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public int hashCode() {
}

@Override
public boolean handles(int c, ULocale locale) {
public boolean handles(int c) {
int script = UCharacter.getIntPropertyValue(c, UProperty.SCRIPT);
return (script == UScript.MYANMAR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ public DictionaryBreakEngine() {
}

@Override
public boolean handles(int c, ULocale locale) {
public boolean isFor(ULocale locale) {
return true; // by default, we handle all locales.
}

@Override
public boolean handles(int c) {
return fSet.contains(c); // we recognize the character
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public int hashCode() {
}

@Override
public boolean handles(int c, ULocale locale) {
public boolean handles(int c) {
int script = UCharacter.getIntPropertyValue(c, UProperty.SCRIPT);
return (script == UScript.KHMER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public int hashCode() {
}

@Override
public boolean handles(int c, ULocale locale) {
public boolean handles(int c) {
return fScript == UCharacter.getIntPropertyValue(c, UProperty.SCRIPT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
public interface LanguageBreakEngine {
/**
* @param c A Unicode codepoint value
* @param locale A locale
* @return true if the engine can handle this character, false otherwise
*/
boolean handles(int c, ULocale locale);
boolean handles(int c);

/**
* @param locale A locale
* @return true if the engine is for this Locale, false otherwise
*/
boolean isFor(ULocale locale);

/**
* Implements the actual breaking logic. Find any breaks within a run in the supplied text.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public int hashCode() {
}

@Override
public boolean handles(int c, ULocale locale) {
public boolean handles(int c) {
int script = UCharacter.getIntPropertyValue(c, UProperty.SCRIPT);
return (script == UScript.LAO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public int hashCode() {
}

@Override
public boolean handles(int c, ULocale locale) {
public boolean handles(int c) {
int script = UCharacter.getIntPropertyValue(c, UProperty.SCRIPT);
return (script == UScript.THAI);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public UnhandledBreakEngine() {
}

@Override
public boolean handles(int c, ULocale locale) {
public boolean isFor(ULocale locale) {
return true; // Handle all locales
}
@Override
public boolean handles(int c) {
return fHandled.contains(c);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ private LanguageBreakEngine getLanguageBreakEngine(int c) {
// We have a dictionary character.
// Does an already instantiated break engine handle it?
for (LanguageBreakEngine candidate : fBreakEngines) {
if (candidate.handles(c, getRequestedLocale())) {
if (candidate.isFor(getRequestedLocale()) && candidate.handles(c)) {
return candidate;
}
}
Expand All @@ -738,7 +738,7 @@ private LanguageBreakEngine getLanguageBreakEngine(int c) {
// Check the global list, another break iterator may have instantiated the
// desired engine.
for (LanguageBreakEngine candidate : gAllBreakEngines) {
if (candidate.handles(c, getRequestedLocale())) {
if (candidate.isFor(getRequestedLocale()) && candidate.handles(c)) {
fBreakEngines.add(candidate);
return candidate;
}
Expand Down Expand Up @@ -1094,8 +1094,12 @@ public static void registerExternalBreakEngine(ExternalBreakEngine engine) {
synchronized(gAllBreakEngines) {
gAllBreakEngines.add(0, new LanguageBreakEngine() {
@Override
public boolean handles(int c, ULocale locale) {
return engine.isFor(c, locale);
public boolean handles(int c) {
return engine.handles(c);
}
@Override
public boolean isFor(ULocale locale) {
return engine.isFor(locale);
}
@Override
public int findBreaks(CharacterIterator text, int startPos, int endPos,
Expand Down Expand Up @@ -1917,15 +1921,14 @@ void dumpCache() {

public interface ExternalBreakEngine {
/**
* <p>Indicate whether this engine handles a particular character when
* the RuleBasedBreakIterator is used for a particular locale. This method is used
* by the RuleBasedBreakIterator to find a break engine.</p>
* @param c A character which begins a run that the engine might handle.
* <p>Indicate whether this engine is used for a particular locale.
* This method is used by the RuleBasedBreakIterator to find a break engine.</p>
*
* @param locale The locale.
* @return true if this engine handles the particular character for that locale.
* @internal ICU 75 technology preview
*/
public boolean isFor(int c, ULocale locale);
public boolean isFor(ULocale locale);

/**
* <p>Indicate whether this engine handles a particular character.This method is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,10 @@ public void TestExternalBreakEngineWithFakeYue() {
RuleBasedBreakIterator.registerExternalBreakEngine(
new RuleBasedBreakIterator.ExternalBreakEngine() {
UnicodeSet block = new UnicodeSet(0x4e00, 0x9FFF);
public boolean isFor(int c, ULocale locale) {
public boolean isFor(ULocale locale) {
// We implmement this for any locale with "yue" such as
// "yue", "yue-CN", "yue-Hant-CN", etc.
return handles(c) && locale.getLanguage().equals("yue");
return locale.getLanguage().equals("yue");
}
public boolean handles(int c) {
return block.contains(c);
Expand Down Expand Up @@ -1076,8 +1076,8 @@ public void TestExternalBreakEngineWithFakeTaiLe() {
new RuleBasedBreakIterator.ExternalBreakEngine() {
UnicodeSet block = new UnicodeSet(0x1950, 0x197f);
UnicodeSet tones = new UnicodeSet(0x1970, 0x1974);
public boolean isFor(int c, ULocale locale) {
return handles(c);
public boolean isFor(ULocale locale) {
return true; // Handle all locales
}
public boolean handles(int c) {
return block.contains(c);
Expand Down

0 comments on commit d14c134

Please sign in to comment.