• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.inputmethod.latin.makedict;
18 
19 import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
20 
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 
24 /**
25  * Utility class for a word with a frequency.
26  *
27  * This is chiefly used to iterate a dictionary.
28  */
29 public class Word implements Comparable<Word> {
30     final String mWord;
31     final int mFrequency;
32     final ArrayList<WeightedString> mShortcutTargets;
33     final ArrayList<WeightedString> mBigrams;
34 
35     private int mHashCode = 0;
36 
Word(final String word, final int frequency, final ArrayList<WeightedString> shortcutTargets, final ArrayList<WeightedString> bigrams)37     public Word(final String word, final int frequency,
38             final ArrayList<WeightedString> shortcutTargets,
39             final ArrayList<WeightedString> bigrams) {
40         mWord = word;
41         mFrequency = frequency;
42         mShortcutTargets = shortcutTargets;
43         mBigrams = bigrams;
44     }
45 
computeHashCode(Word word)46     private static int computeHashCode(Word word) {
47         return Arrays.hashCode(new Object[] {
48                 word.mWord,
49                 word.mFrequency,
50                 word.mShortcutTargets.hashCode(),
51                 word.mBigrams.hashCode()
52         });
53     }
54 
55     /**
56      * Three-way comparison.
57      *
58      * A Word x is greater than a word y if x has a higher frequency. If they have the same
59      * frequency, they are sorted in lexicographic order.
60      */
61     @Override
compareTo(Word w)62     public int compareTo(Word w) {
63         if (mFrequency < w.mFrequency) return 1;
64         if (mFrequency > w.mFrequency) return -1;
65         return mWord.compareTo(w.mWord);
66     }
67 
68     /**
69      * Equality test.
70      *
71      * Words are equal if they have the same frequency, the same spellings, and the same
72      * attributes.
73      */
74     @Override
equals(Object o)75     public boolean equals(Object o) {
76         if (o == this) return true;
77         if (!(o instanceof Word)) return false;
78         Word w = (Word)o;
79         return mFrequency == w.mFrequency && mWord.equals(w.mWord)
80                 && mShortcutTargets.equals(w.mShortcutTargets)
81                 && mBigrams.equals(w.mBigrams);
82     }
83 
84     @Override
hashCode()85     public int hashCode() {
86         if (mHashCode == 0) {
87             mHashCode = computeHashCode(this);
88         }
89         return mHashCode;
90     }
91 }
92