Home | History | Annotate | Download | only in makedict
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin.makedict;
     18 
     19 import com.android.inputmethod.annotations.UsedForTesting;
     20 import com.android.inputmethod.latin.BinaryDictionary;
     21 import com.android.inputmethod.latin.utils.CombinedFormatUtils;
     22 
     23 import java.util.Arrays;
     24 
     25 public final class ProbabilityInfo {
     26     public final int mProbability;
     27     // mTimestamp, mLevel and mCount are historical info. These values are depend on the
     28     // implementation in native code; thus, we must not use them and have any assumptions about
     29     // them except for tests.
     30     public final int mTimestamp;
     31     public final int mLevel;
     32     public final int mCount;
     33 
     34     @UsedForTesting
     35     public static ProbabilityInfo max(final ProbabilityInfo probabilityInfo1,
     36             final ProbabilityInfo probabilityInfo2) {
     37         if (probabilityInfo1 == null) {
     38             return probabilityInfo2;
     39         }
     40         if (probabilityInfo2 == null) {
     41             return probabilityInfo1;
     42         }
     43         if (probabilityInfo1.mProbability > probabilityInfo2.mProbability) {
     44             return probabilityInfo1;
     45         } else {
     46             return probabilityInfo2;
     47         }
     48     }
     49 
     50     public ProbabilityInfo(final int probability) {
     51         this(probability, BinaryDictionary.NOT_A_VALID_TIMESTAMP, 0, 0);
     52     }
     53 
     54     public ProbabilityInfo(final int probability, final int timestamp, final int level,
     55             final int count) {
     56         mProbability = probability;
     57         mTimestamp = timestamp;
     58         mLevel = level;
     59         mCount = count;
     60     }
     61 
     62     public boolean hasHistoricalInfo() {
     63         return mTimestamp != BinaryDictionary.NOT_A_VALID_TIMESTAMP;
     64     }
     65 
     66     @Override
     67     public int hashCode() {
     68         if (hasHistoricalInfo()) {
     69             return Arrays.hashCode(new Object[] { mProbability, mTimestamp, mLevel, mCount });
     70         } else {
     71             return Arrays.hashCode(new Object[] { mProbability });
     72         }
     73     }
     74 
     75     @Override
     76     public String toString() {
     77         return CombinedFormatUtils.formatProbabilityInfo(this);
     78     }
     79 
     80     @Override
     81     public boolean equals(Object o) {
     82         if (o == this) return true;
     83         if (!(o instanceof ProbabilityInfo)) return false;
     84         final ProbabilityInfo p = (ProbabilityInfo)o;
     85         if (!hasHistoricalInfo() && !p.hasHistoricalInfo()) {
     86             return mProbability == p.mProbability;
     87         }
     88         return mProbability == p.mProbability && mTimestamp == p.mTimestamp && mLevel == p.mLevel
     89                 && mCount == p.mCount;
     90     }
     91 }