Add missing Word.hashCode()

Some cleanups too.

bug: 6209651
Change-Id: I94e2e29c92e90e554e4952d277d590e093766c4f
This commit is contained in:
Ken Wakasa 2012-03-22 18:09:19 +09:00
parent e7cfe43652
commit 9f0ea52a5d
4 changed files with 66 additions and 31 deletions

View file

@ -180,7 +180,7 @@ public class Key {
mY = y; mY = y;
mHitBox.set(x, y, x + width + 1, y + height); mHitBox.set(x, y, x + width + 1, y + height);
mHashCode = hashCode(this); mHashCode = computeHashCode(this);
} }
/** /**
@ -334,7 +334,7 @@ public class Key {
mAltCode = adjustCaseOfCodeForKeyboardId(style.getInt(keyAttr, mAltCode = adjustCaseOfCodeForKeyboardId(style.getInt(keyAttr,
R.styleable.Keyboard_Key_altCode, Keyboard.CODE_UNSPECIFIED), preserveCase, R.styleable.Keyboard_Key_altCode, Keyboard.CODE_UNSPECIFIED), preserveCase,
params.mId); params.mId);
mHashCode = hashCode(this); mHashCode = computeHashCode(this);
keyAttr.recycle(); keyAttr.recycle();
@ -366,7 +366,7 @@ public class Key {
} }
} }
private static int hashCode(Key key) { private static int computeHashCode(Key key) {
return Arrays.hashCode(new Object[] { return Arrays.hashCode(new Object[] {
key.mX, key.mX,
key.mY, key.mY,

View file

@ -70,23 +70,23 @@ public class KeyboardId {
public KeyboardId(int elementId, Locale locale, int orientation, int width, int mode, public KeyboardId(int elementId, Locale locale, int orientation, int width, int mode,
EditorInfo editorInfo, boolean clobberSettingsKey, boolean shortcutKeyEnabled, EditorInfo editorInfo, boolean clobberSettingsKey, boolean shortcutKeyEnabled,
boolean hasShortcutKey, boolean languageSwitchKeyEnabled) { boolean hasShortcutKey, boolean languageSwitchKeyEnabled) {
this.mLocale = locale; mLocale = locale;
this.mOrientation = orientation; mOrientation = orientation;
this.mWidth = width; mWidth = width;
this.mMode = mode; mMode = mode;
this.mElementId = elementId; mElementId = elementId;
this.mEditorInfo = editorInfo; mEditorInfo = editorInfo;
this.mClobberSettingsKey = clobberSettingsKey; mClobberSettingsKey = clobberSettingsKey;
this.mShortcutKeyEnabled = shortcutKeyEnabled; mShortcutKeyEnabled = shortcutKeyEnabled;
this.mHasShortcutKey = hasShortcutKey; mHasShortcutKey = hasShortcutKey;
this.mLanguageSwitchKeyEnabled = languageSwitchKeyEnabled; mLanguageSwitchKeyEnabled = languageSwitchKeyEnabled;
this.mCustomActionLabel = (editorInfo.actionLabel != null) mCustomActionLabel = (editorInfo.actionLabel != null)
? editorInfo.actionLabel.toString() : null; ? editorInfo.actionLabel.toString() : null;
this.mHashCode = hashCode(this); mHashCode = computeHashCode(this);
} }
private static int hashCode(KeyboardId id) { private static int computeHashCode(KeyboardId id) {
return Arrays.hashCode(new Object[] { return Arrays.hashCode(new Object[] {
id.mOrientation, id.mOrientation,
id.mElementId, id.mElementId,
@ -109,21 +109,21 @@ public class KeyboardId {
private boolean equals(KeyboardId other) { private boolean equals(KeyboardId other) {
if (other == this) if (other == this)
return true; return true;
return other.mOrientation == this.mOrientation return other.mOrientation == mOrientation
&& other.mElementId == this.mElementId && other.mElementId == mElementId
&& other.mMode == this.mMode && other.mMode == mMode
&& other.mWidth == this.mWidth && other.mWidth == mWidth
&& other.passwordInput() == this.passwordInput() && other.passwordInput() == passwordInput()
&& other.mClobberSettingsKey == this.mClobberSettingsKey && other.mClobberSettingsKey == mClobberSettingsKey
&& other.mShortcutKeyEnabled == this.mShortcutKeyEnabled && other.mShortcutKeyEnabled == mShortcutKeyEnabled
&& other.mHasShortcutKey == this.mHasShortcutKey && other.mHasShortcutKey == mHasShortcutKey
&& other.mLanguageSwitchKeyEnabled == this.mLanguageSwitchKeyEnabled && other.mLanguageSwitchKeyEnabled == mLanguageSwitchKeyEnabled
&& other.isMultiLine() == this.isMultiLine() && other.isMultiLine() == isMultiLine()
&& other.imeAction() == this.imeAction() && other.imeAction() == imeAction()
&& TextUtils.equals(other.mCustomActionLabel, this.mCustomActionLabel) && TextUtils.equals(other.mCustomActionLabel, mCustomActionLabel)
&& other.navigateNext() == this.navigateNext() && other.navigateNext() == navigateNext()
&& other.navigatePrevious() == this.navigatePrevious() && other.navigatePrevious() == navigatePrevious()
&& other.mLocale.equals(this.mLocale); && other.mLocale.equals(mLocale);
} }
public boolean isAlphabetKeyboard() { public boolean isAlphabetKeyboard() {

View file

@ -64,6 +64,19 @@ public class FusionDictionary implements Iterable<Word> {
mWord = word; mWord = word;
mFrequency = frequency; mFrequency = frequency;
} }
@Override
public int hashCode() {
return Arrays.hashCode(new Object[] { mWord, mFrequency });
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof WeightedString)) return false;
WeightedString w = (WeightedString)o;
return mWord.equals(w.mWord) && mFrequency == w.mFrequency;
}
} }
/** /**

View file

@ -19,6 +19,7 @@ package com.android.inputmethod.latin.makedict;
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
/** /**
* Utility class for a word with a frequency. * Utility class for a word with a frequency.
@ -32,6 +33,8 @@ public class Word implements Comparable<Word> {
final ArrayList<WeightedString> mShortcutTargets; final ArrayList<WeightedString> mShortcutTargets;
final ArrayList<WeightedString> mBigrams; final ArrayList<WeightedString> mBigrams;
private int mHashCode = 0;
public Word(final String word, final int frequency, public Word(final String word, final int frequency,
final ArrayList<WeightedString> shortcutTargets, final ArrayList<WeightedString> shortcutTargets,
final ArrayList<WeightedString> bigrams, final boolean isShortcutOnly) { final ArrayList<WeightedString> bigrams, final boolean isShortcutOnly) {
@ -42,6 +45,16 @@ public class Word implements Comparable<Word> {
mIsShortcutOnly = isShortcutOnly; mIsShortcutOnly = isShortcutOnly;
} }
private static int computeHashCode(Word word) {
return Arrays.hashCode(new Object[] {
word.mWord,
word.mFrequency,
word.mIsShortcutOnly,
word.mShortcutTargets.hashCode(),
word.mBigrams.hashCode()
});
}
/** /**
* Three-way comparison. * Three-way comparison.
* *
@ -63,10 +76,19 @@ public class Word implements Comparable<Word> {
*/ */
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Word)) return false; if (!(o instanceof Word)) return false;
Word w = (Word)o; Word w = (Word)o;
return mFrequency == w.mFrequency && mWord.equals(w.mWord) return mFrequency == w.mFrequency && mWord.equals(w.mWord)
&& mShortcutTargets.equals(w.mShortcutTargets) && mShortcutTargets.equals(w.mShortcutTargets)
&& mBigrams.equals(w.mBigrams); && mBigrams.equals(w.mBigrams);
} }
@Override
public int hashCode() {
if (mHashCode == 0) {
mHashCode = computeHashCode(this);
}
return mHashCode;
}
} }