📄 breakiterator.java
字号:
* @return The first boundary after the specified offset. */ public abstract int following(int offset); /** * Return the last boundary preceding the specfied offset. * The value returned is always less than the offset or the value * BreakIterator.DONE. * @param offset the offset to begin scanning. Valid values are * determined by the CharacterIterator passed to setText(). * Invalid values cause an IllegalArgumentException to be thrown. * @return The last boundary before the specified offset. * @since 1.2 */ public int preceding(int offset) { // NOTE: This implementation is here solely because we can't add new // abstract methods to an existing class. There is almost ALWAYS a // better, faster way to do this. int pos = following(offset); while (pos >= offset && pos != DONE) pos = previous(); return pos; } /** * Return true if the specified position is a boundary position. * @param offset the offset to check. * @return True if "offset" is a boundary position. * @since 1.2 */ public boolean isBoundary(int offset) { // NOTE: This implementation probably is wrong for most situations // because it fails to take into account the possibility that a // CharacterIterator passed to setText() may not have a begin offset // of 0. But since the abstract BreakIterator doesn't have that // knowledge, it assumes the begin offset is 0. If you subclass // BreakIterator, copy the SimpleTextBoundary implementation of this // function into your subclass. [This should have been abstract at // this level, but it's too late to fix that now.] if (offset == 0) return true; else return following(offset - 1) == offset; } /** * Return character index of the text boundary that was most recently * returned by next(), previous(), first(), or last() * @return The boundary most recently returned. */ public abstract int current(); /** * Get the text being scanned * @return the text being scanned */ public abstract CharacterIterator getText(); /** * Set a new text string to be scanned. The current scan * position is reset to first(). * @param newText new text to scan. */ public void setText(String newText) { setText(new StringCharacterIterator(newText)); } /** * Set a new text for scanning. The current scan * position is reset to first(). * @param newText new text to scan. */ public abstract void setText(CharacterIterator newText); private static final int CHARACTER_INDEX = 0; private static final int WORD_INDEX = 1; private static final int LINE_INDEX = 2; private static final int SENTENCE_INDEX = 3; private static final SoftReference[] iterCache = new SoftReference[4]; /** * Create BreakIterator for word-breaks using default locale. * Returns an instance of a BreakIterator implementing word breaks. * WordBreak is usefull for word selection (ex. double click) * @return A BreakIterator for word-breaks * @see java.util.Locale#getDefault */ public static BreakIterator getWordInstance() { return getWordInstance(Locale.getDefault()); } /** * Create BreakIterator for word-breaks using specified locale. * Returns an instance of a BreakIterator implementing word breaks. * WordBreak is usefull for word selection (ex. double click) * @param where the local. If a specific WordBreak is not * avaliable for the specified locale, a default WordBreak is returned. * @return A BreakIterator for word-breaks */ public static BreakIterator getWordInstance(Locale where) { return getBreakInstance(where, WORD_INDEX, "WordBreakRules", "WordBreakDictionary"); } /** * Create BreakIterator for line-breaks using default locale. * Returns an instance of a BreakIterator implementing line breaks. Line * breaks are logically possible line breaks, actual line breaks are * usually determined based on display width. * LineBreak is useful for word wrapping text. * @return A BreakIterator for line-breaks * @see java.util.Locale#getDefault */ public static BreakIterator getLineInstance() { return getLineInstance(Locale.getDefault()); } /** * Create BreakIterator for line-breaks using specified locale. * Returns an instance of a BreakIterator implementing line breaks. Line * breaks are logically possible line breaks, actual line breaks are * usually determined based on display width. * LineBreak is useful for word wrapping text. * @param where the local. If a specific LineBreak is not * avaliable for the specified locale, a default LineBreak is returned. * @return A BreakIterator for line-breaks */ public static BreakIterator getLineInstance(Locale where) { return getBreakInstance(where, LINE_INDEX, "LineBreakRules", "LineBreakDictionary"); } /** * Create BreakIterator for character-breaks using default locale * Returns an instance of a BreakIterator implementing character breaks. * Character breaks are boundaries of combining character sequences. * @return A BreakIterator for character-breaks * @see Locale#getDefault */ public static BreakIterator getCharacterInstance() { return getCharacterInstance(Locale.getDefault()); } /** * Create BreakIterator for character-breaks using specified locale * Returns an instance of a BreakIterator implementing character breaks. * Character breaks are boundaries of combining character sequences. * @param where the local. If a specific character break is not * avaliable for the specified local, a default character break is returned. * @return A BreakIterator for character-breaks */ public static BreakIterator getCharacterInstance(Locale where) { return getBreakInstance(where, CHARACTER_INDEX, "CharacterBreakRules", "CharacterBreakDictionary"); } /** * Create BreakIterator for sentence-breaks using default locale * Returns an instance of a BreakIterator implementing sentence breaks. * @return A BreakIterator for sentence-breaks * @see java.util.Locale#getDefault */ public static BreakIterator getSentenceInstance() { return getSentenceInstance(Locale.getDefault()); } /** * Create BreakIterator for sentence-breaks using specified locale * Returns an instance of a BreakIterator implementing sentence breaks. * @param where the local. If a specific SentenceBreak is not * avaliable for the specified local, a default SentenceBreak is returned. * @return A BreakIterator for sentence-breaks */ public static BreakIterator getSentenceInstance(Locale where) { return getBreakInstance(where, SENTENCE_INDEX, "SentenceBreakRules", "SentenceBreakDictionary"); } private static BreakIterator getBreakInstance(Locale where, int type, String rulesName, String dictionaryName) { if (iterCache[type] != null) { BreakIteratorCache cache = (BreakIteratorCache) iterCache[type].get(); if (cache != null) { if (cache.getLocale().equals(where)) { return cache.createBreakInstance(); } } } BreakIterator result = createBreakInstance(where, type, rulesName, dictionaryName); BreakIteratorCache cache = new BreakIteratorCache(where, result); iterCache[type] = new SoftReference(cache); return result; } private static ResourceBundle getBundle(final String baseName, final Locale locale) { return (ResourceBundle) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return ResourceBundle.getBundle(baseName, locale); } }); } private static BreakIterator createBreakInstance(Locale where, int type, String rulesName, String dictionaryName) { ResourceBundle bundle = getBundle( "sun.text.resources.BreakIteratorRules", where); String[] classNames = bundle.getStringArray("BreakIteratorClasses"); String rules = bundle.getString(rulesName); if (classNames[type].equals("RuleBasedBreakIterator")) { return new RuleBasedBreakIterator(rules); } else if (classNames[type].equals("DictionaryBasedBreakIterator")) { try { URL url = (URL) bundle.getObject(dictionaryName); InputStream dictionary = url.openStream(); return new DictionaryBasedBreakIterator(rules, dictionary); } catch(IOException e) { } catch(MissingResourceException e) { } return new RuleBasedBreakIterator(rules); } else throw new IllegalArgumentException("Invalid break iterator class \"" + classNames[type] + "\""); } /** * Get the set of Locales for which BreakIterators are installed * @return available locales */ public static synchronized Locale[] getAvailableLocales() { //FIX ME - this is a known bug. It should return //all locales. return LocaleData.getAvailableLocales("NumberPatterns"); } private static final class BreakIteratorCache { private BreakIterator iter; private Locale where; BreakIteratorCache(Locale where, BreakIterator iter) { this.where = where; this.iter = (BreakIterator) iter.clone(); } Locale getLocale() { return where; } BreakIterator createBreakInstance() { return (BreakIterator) iter.clone(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -