diff options
author | Elliott Hughes <enh@google.com> | 2010-03-18 18:25:22 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-03-19 14:29:35 -0700 |
commit | 9672b4887f2972c1b7c5f3d1a6cf882deccf857f (patch) | |
tree | 281cee3f9379a3f79370ae0ec400ee14d74bd9d0 /icu/src/main/java | |
parent | 69f16dc7ebc4caff5c67245b2ea034dfc5ee4217 (diff) | |
download | libcore-9672b4887f2972c1b7c5f3d1a6cf882deccf857f.zip libcore-9672b4887f2972c1b7c5f3d1a6cf882deccf857f.tar.gz libcore-9672b4887f2972c1b7c5f3d1a6cf882deccf857f.tar.bz2 |
Clean up the Java side of the ICU interface a bit.
My original intention was just to add the missing "final" on a few classes,
but our BreakIterator implementation struck me as excessively bloated and
confusing.
Change-Id: I2d2dccafe8ec91124f3c83909c9ec647cc2d51e2
Diffstat (limited to 'icu/src/main/java')
8 files changed, 136 insertions, 272 deletions
diff --git a/icu/src/main/java/com/ibm/icu4jni/lang/UCharacter.java b/icu/src/main/java/com/ibm/icu4jni/lang/UCharacter.java index 2839ac3..dc351f4 100644 --- a/icu/src/main/java/com/ibm/icu4jni/lang/UCharacter.java +++ b/icu/src/main/java/com/ibm/icu4jni/lang/UCharacter.java @@ -18,7 +18,7 @@ package com.ibm.icu4jni.lang; import java.lang.Character.UnicodeBlock; -public class UCharacter { +public final class UCharacter { public static native boolean isDefined(int codePoint); public static native boolean isDigit(int codePoint); diff --git a/icu/src/main/java/com/ibm/icu4jni/regex/NativeRegEx.java b/icu/src/main/java/com/ibm/icu4jni/regex/NativeRegEx.java index bdfff5b..789c75b 100644 --- a/icu/src/main/java/com/ibm/icu4jni/regex/NativeRegEx.java +++ b/icu/src/main/java/com/ibm/icu4jni/regex/NativeRegEx.java @@ -16,7 +16,7 @@ package com.ibm.icu4jni.regex; -public class NativeRegEx { +public final class NativeRegEx { /** * Opens (compiles) an ICU regular expression. diff --git a/icu/src/main/java/com/ibm/icu4jni/text/BreakIterator.java b/icu/src/main/java/com/ibm/icu4jni/text/BreakIterator.java deleted file mode 100644 index aa925aa..0000000 --- a/icu/src/main/java/com/ibm/icu4jni/text/BreakIterator.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.ibm.icu4jni.text; - -import com.ibm.icu4jni.util.Resources; -import java.text.CharacterIterator; -import java.text.StringCharacterIterator; -import java.util.Locale; - -public abstract class BreakIterator implements Cloneable -{ - protected static final int BI_CHAR_INSTANCE = 1; - protected static final int BI_WORD_INSTANCE = 2; - protected static final int BI_LINE_INSTANCE = 3; - protected static final int BI_SENT_INSTANCE = 4; - - protected int type = 0; - - public static Locale[] getAvailableLocales() { - return Resources.localesFromStrings(NativeBreakIterator.getAvailableLocalesImpl()); - } - - public static BreakIterator getCharacterInstance() { - int iter = NativeBreakIterator.getCharacterInstanceImpl(""); - return new RuleBasedBreakIterator(iter, BI_CHAR_INSTANCE); - } - - public static BreakIterator getCharacterInstance(Locale where) { - int iter = NativeBreakIterator.getCharacterInstanceImpl(where.toString()); - return new RuleBasedBreakIterator(iter, BI_CHAR_INSTANCE); - } - - public static BreakIterator getLineInstance() { - int iter = NativeBreakIterator.getLineInstanceImpl(""); - return new RuleBasedBreakIterator(iter, BI_LINE_INSTANCE); - } - - public static BreakIterator getLineInstance(Locale where) { - int iter = NativeBreakIterator.getLineInstanceImpl(where.toString()); - return new RuleBasedBreakIterator(iter, BI_LINE_INSTANCE); - } - - public static BreakIterator getSentenceInstance() { - int iter = NativeBreakIterator.getSentenceInstanceImpl(""); - return new RuleBasedBreakIterator(iter, BI_SENT_INSTANCE); - } - - public static BreakIterator getSentenceInstance(Locale where) { - int iter = NativeBreakIterator.getSentenceInstanceImpl(where.toString()); - return new RuleBasedBreakIterator(iter, BI_SENT_INSTANCE); - } - - public static BreakIterator getWordInstance() { - int iter = NativeBreakIterator.getWordInstanceImpl(""); - return new RuleBasedBreakIterator(iter, BI_WORD_INSTANCE); - } - - public static BreakIterator getWordInstance(Locale where) { - int iter = NativeBreakIterator.getWordInstanceImpl(where.toString()); - return new RuleBasedBreakIterator(iter, BI_WORD_INSTANCE); - } - - public void setText(String newText) { - setText(new StringCharacterIterator(newText)); - } - - public abstract boolean isBoundary(int offset); - - public abstract int preceding(int offset); - - public abstract Object clone(); - - public abstract int current(); - - public abstract int first(); - - public abstract int following(int offset); - - public abstract CharacterIterator getText(); - - public abstract int last(); - - public abstract int next(int n); - - public abstract int next(); - - public abstract int previous(); - - public abstract void setText(CharacterIterator newText); - -} diff --git a/icu/src/main/java/com/ibm/icu4jni/text/NativeBreakIterator.java b/icu/src/main/java/com/ibm/icu4jni/text/NativeBreakIterator.java index e318e47..161f542 100644 --- a/icu/src/main/java/com/ibm/icu4jni/text/NativeBreakIterator.java +++ b/icu/src/main/java/com/ibm/icu4jni/text/NativeBreakIterator.java @@ -16,39 +16,146 @@ package com.ibm.icu4jni.text; -final class NativeBreakIterator { - private NativeBreakIterator() { +import com.ibm.icu4jni.util.Resources; +import java.text.CharacterIterator; +import java.text.StringCharacterIterator; +import java.util.Locale; + +public final class NativeBreakIterator implements Cloneable { + // Acceptable values for the 'type' field. + private static final int BI_CHAR_INSTANCE = 1; + private static final int BI_WORD_INSTANCE = 2; + private static final int BI_LINE_INSTANCE = 3; + private static final int BI_SENT_INSTANCE = 4; + + private final int addr; + private final int type; + private CharacterIterator charIter; + + private NativeBreakIterator(int iterAddr, int type) { + this.addr = iterAddr; + this.type = type; + this.charIter = new StringCharacterIterator(""); + } + + @Override + public Object clone() { + int cloneAddr = cloneImpl(this.addr); + NativeBreakIterator clone = new NativeBreakIterator(cloneAddr, this.type); + // The RI doesn't clone the CharacterIterator. + clone.charIter = this.charIter; + return clone; + } + + @Override + public boolean equals(Object object) { + if (object == null || !(object instanceof NativeBreakIterator)) { + return false; + } + // TODO: is this sufficient? shouldn't we be checking the underlying rules? + NativeBreakIterator rhs = (NativeBreakIterator) object; + return type == rhs.type && charIter.equals(rhs.charIter); + } + + @Override + public int hashCode() { + return 42; // No-one uses BreakIterator as a hash key. + } + + @Override + protected void finalize() { + closeBreakIteratorImpl(this.addr); + } + + public int current() { + return currentImpl(this.addr); + } + + public int first() { + return firstImpl(this.addr); + } + + public int following(int offset) { + return followingImpl(this.addr, offset); + } + + public CharacterIterator getText() { + int newLoc = currentImpl(this.addr); + this.charIter.setIndex(newLoc); + return this.charIter; + } + + public int last() { + return lastImpl(this.addr); + } + + public int next(int n) { + return nextImpl(this.addr, n); + } + + public int next() { + return nextImpl(this.addr, 1); + } + + public int previous() { + return previousImpl(this.addr); } - static native String[] getAvailableLocalesImpl(); + public void setText(CharacterIterator newText) { + this.charIter = newText; + StringBuilder sb = new StringBuilder(); + for (char c = newText.first(); c != CharacterIterator.DONE; c = newText.next()) { + sb.append(c); + } + setTextImpl(this.addr, sb.toString()); + } - static native int getCharacterInstanceImpl(String locale); - - static native int getWordInstanceImpl(String locale); - - static native int getLineInstanceImpl(String locale); - - static native int getSentenceInstanceImpl(String locale); + public void setText(String newText) { + setText(new StringCharacterIterator(newText)); + } - static native void closeBreakIteratorImpl(int biaddress); - - static native void setTextImpl(int biaddress, String text); - - static native int cloneImpl(int biaddress); - - static native int precedingImpl(int biaddress, int offset); + public boolean isBoundary(int offset) { + return isBoundaryImpl(this.addr, offset); + } - static native boolean isBoundaryImpl(int biaddress, int offset); + public int preceding(int offset) { + return precedingImpl(this.addr, offset); + } - static native int nextImpl(int biaddress, int n); + public static Locale[] getAvailableLocales() { + return Resources.localesFromStrings(getAvailableLocalesImpl()); + } - static native int previousImpl(int biaddress); + public static NativeBreakIterator getCharacterInstance(Locale where) { + return new NativeBreakIterator(getCharacterInstanceImpl(where.toString()), BI_CHAR_INSTANCE); + } - static native int currentImpl(int biaddress); + public static NativeBreakIterator getLineInstance(Locale where) { + return new NativeBreakIterator(getLineInstanceImpl(where.toString()), BI_LINE_INSTANCE); + } - static native int firstImpl(int biaddress); + public static NativeBreakIterator getSentenceInstance(Locale where) { + return new NativeBreakIterator(getSentenceInstanceImpl(where.toString()), BI_SENT_INSTANCE); + } - static native int followingImpl(int biaddress, int offset); + public static NativeBreakIterator getWordInstance(Locale where) { + return new NativeBreakIterator(getWordInstanceImpl(where.toString()), BI_WORD_INSTANCE); + } - static native int lastImpl(int biaddress); + private static native String[] getAvailableLocalesImpl(); + private static native int getCharacterInstanceImpl(String locale); + private static native int getWordInstanceImpl(String locale); + private static native int getLineInstanceImpl(String locale); + private static native int getSentenceInstanceImpl(String locale); + private static native void closeBreakIteratorImpl(int addr); + private static native void setTextImpl(int addr, String text); + private static native int cloneImpl(int addr); + private static native int precedingImpl(int addr, int offset); + private static native boolean isBoundaryImpl(int addr, int offset); + private static native int nextImpl(int addr, int n); + private static native int previousImpl(int addr); + private static native int currentImpl(int addr); + private static native int firstImpl(int addr); + private static native int followingImpl(int addr, int offset); + private static native int lastImpl(int addr); } diff --git a/icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java b/icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java index d46c2ec..6f751d5 100644 --- a/icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java +++ b/icu/src/main/java/com/ibm/icu4jni/text/NativeDecimalFormat.java @@ -31,7 +31,7 @@ import java.text.ParsePosition; import java.util.Currency; import java.util.Locale; -public class NativeDecimalFormat { +public final class NativeDecimalFormat { /** * Constants corresponding to the native type UNumberFormatSymbol, for setSymbol. */ diff --git a/icu/src/main/java/com/ibm/icu4jni/text/RuleBasedBreakIterator.java b/icu/src/main/java/com/ibm/icu4jni/text/RuleBasedBreakIterator.java deleted file mode 100644 index e532ac4..0000000 --- a/icu/src/main/java/com/ibm/icu4jni/text/RuleBasedBreakIterator.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.ibm.icu4jni.text; - -import java.text.CharacterIterator; -import java.text.StringCharacterIterator; - -public class RuleBasedBreakIterator extends BreakIterator { - - private CharacterIterator charIter; - - private int addr; - - RuleBasedBreakIterator(int iterAddr, int type) { - this.addr = iterAddr; - this.type = type; - this.charIter = new StringCharacterIterator(""); - } - - @Override - public Object clone() { - int cloneAddr = NativeBreakIterator.cloneImpl(this.addr); - RuleBasedBreakIterator rbbi = - new RuleBasedBreakIterator(cloneAddr, this.type); - - rbbi.charIter = this.charIter; - - return rbbi; - } - - @Override - public boolean equals(Object object) { - if(object == null) { - return false; - } - - if(!(object instanceof RuleBasedBreakIterator)) { - return false; - } - - CharacterIterator iter = ((RuleBasedBreakIterator) object).charIter; - - boolean result = this.type == ((RuleBasedBreakIterator) object).type; - - return result && iter.equals(this.charIter); - } - - @Override - public int hashCode() { - return 42; // No-one uses RuleBasedBreakIterator as a hash key. - } - - @Override - public int current() { - return NativeBreakIterator.currentImpl(this.addr); - } - - @Override - public int first() { - return NativeBreakIterator.firstImpl(this.addr); - } - - @Override - public int following(int offset) { - return NativeBreakIterator.followingImpl(this.addr, offset); - } - - @Override - public CharacterIterator getText() { - int newLoc = NativeBreakIterator.currentImpl(this.addr); - this.charIter.setIndex(newLoc); - return this.charIter; - } - - @Override - public int last() { - return NativeBreakIterator.lastImpl(this.addr); - } - - @Override - public int next(int n) { - return NativeBreakIterator.nextImpl(this.addr, n); - } - - @Override - public int next() { - return NativeBreakIterator.nextImpl(this.addr, 1); - } - - @Override - public int previous() { - return NativeBreakIterator.previousImpl(this.addr); - } - - @Override - public void setText(CharacterIterator newText) { - this.charIter = newText; - - StringBuilder sb = new StringBuilder(); - - char c = newText.first(); - while(c != CharacterIterator.DONE) { - sb.append(c); - c = newText.next(); - } - - NativeBreakIterator.setTextImpl(this.addr, sb.toString()); - } - - protected void finalize() { - NativeBreakIterator.closeBreakIteratorImpl(this.addr); - } - - @Override - public boolean isBoundary(int offset) { - return NativeBreakIterator.isBoundaryImpl(this.addr, offset); - } - - @Override - public int preceding(int offset) { - return NativeBreakIterator.precedingImpl(this.addr, offset); - } - -} diff --git a/icu/src/main/java/com/ibm/icu4jni/util/LocaleData.java b/icu/src/main/java/com/ibm/icu4jni/util/LocaleData.java index 87f9bc2..109183d 100644 --- a/icu/src/main/java/com/ibm/icu4jni/util/LocaleData.java +++ b/icu/src/main/java/com/ibm/icu4jni/util/LocaleData.java @@ -26,7 +26,7 @@ import java.util.Arrays; * in the case of arrays. If you ever expose any of these things to user code, you must give * them a clone rather than the original. */ -public class LocaleData { +public final class LocaleData { public Integer firstDayOfWeek; public Integer minimalDaysInFirstWeek; diff --git a/icu/src/main/java/com/ibm/icu4jni/util/Resources.java b/icu/src/main/java/com/ibm/icu4jni/util/Resources.java index 4a91d62..aae2ff4 100644 --- a/icu/src/main/java/com/ibm/icu4jni/util/Resources.java +++ b/icu/src/main/java/com/ibm/icu4jni/util/Resources.java @@ -31,7 +31,7 @@ import java.util.logging.Logger; * * TODO: move the LocaleData stuff into LocaleData and rename this class. */ -public class Resources { +public final class Resources { // A cache for the locale-specific data. private static final HashMap<String, LocaleData> localeDataCache = new HashMap<String, LocaleData>(); |