Home | History | Annotate | Download | only in makedict
      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 final class Word implements Comparable<Word> {
     30     public final String mWord;
     31     public final int mFrequency;
     32     public final ArrayList<WeightedString> mShortcutTargets;
     33     public final ArrayList<WeightedString> mBigrams;
     34     public final boolean mIsNotAWord;
     35     public final boolean mIsBlacklistEntry;
     36 
     37     private int mHashCode = 0;
     38 
     39     public Word(final String word, final int frequency,
     40             final ArrayList<WeightedString> shortcutTargets,
     41             final ArrayList<WeightedString> bigrams,
     42             final boolean isNotAWord, final boolean isBlacklistEntry) {
     43         mWord = word;
     44         mFrequency = frequency;
     45         mShortcutTargets = shortcutTargets;
     46         mBigrams = bigrams;
     47         mIsNotAWord = isNotAWord;
     48         mIsBlacklistEntry = isBlacklistEntry;
     49     }
     50 
     51     private static int computeHashCode(Word word) {
     52         return Arrays.hashCode(new Object[] {
     53                 word.mWord,
     54                 word.mFrequency,
     55                 word.mShortcutTargets.hashCode(),
     56                 word.mBigrams.hashCode(),
     57                 word.mIsNotAWord,
     58                 word.mIsBlacklistEntry
     59         });
     60     }
     61 
     62     /**
     63      * Three-way comparison.
     64      *
     65      * A Word x is greater than a word y if x has a higher frequency. If they have the same
     66      * frequency, they are sorted in lexicographic order.
     67      */
     68     @Override
     69     public int compareTo(Word w) {
     70         if (mFrequency < w.mFrequency) return 1;
     71         if (mFrequency > w.mFrequency) return -1;
     72         return mWord.compareTo(w.mWord);
     73     }
     74 
     75     /**
     76      * Equality test.
     77      *
     78      * Words are equal if they have the same frequency, the same spellings, and the same
     79      * attributes.
     80      */
     81     @Override
     82     public boolean equals(Object o) {
     83         if (o == this) return true;
     84         if (!(o instanceof Word)) return false;
     85         Word w = (Word)o;
     86         return mFrequency == w.mFrequency && mWord.equals(w.mWord)
     87                 && mShortcutTargets.equals(w.mShortcutTargets)
     88                 && mBigrams.equals(w.mBigrams)
     89                 && mIsNotAWord == w.mIsNotAWord
     90                 && mIsBlacklistEntry == w.mIsBlacklistEntry;
     91     }
     92 
     93     @Override
     94     public int hashCode() {
     95         if (mHashCode == 0) {
     96             mHashCode = computeHashCode(this);
     97         }
     98         return mHashCode;
     99     }
    100 }
    101