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         return (probabilityInfo1.mProbability > probabilityInfo2.mProbability) ? probabilityInfo1
     44                 : probabilityInfo2;
     45     }
     46 
     47     public ProbabilityInfo(final int probability) {
     48         this(probability, BinaryDictionary.NOT_A_VALID_TIMESTAMP, 0, 0);
     49     }
     50 
     51     public ProbabilityInfo(final int probability, final int timestamp, final int level,
     52             final int count) {
     53         mProbability = probability;
     54         mTimestamp = timestamp;
     55         mLevel = level;
     56         mCount = count;
     57     }
     58 
     59     public boolean hasHistoricalInfo() {
     60         return mTimestamp != BinaryDictionary.NOT_A_VALID_TIMESTAMP;
     61     }
     62 
     63     @Override
     64     public int hashCode() {
     65         if (hasHistoricalInfo()) {
     66             return Arrays.hashCode(new Object[] { mProbability, mTimestamp, mLevel, mCount });
     67         }
     68         return Arrays.hashCode(new Object[] { mProbability });
     69     }
     70 
     71     @Override
     72     public String toString() {
     73         return CombinedFormatUtils.formatProbabilityInfo(this);
     74     }
     75 
     76     @Override
     77     public boolean equals(Object o) {
     78         if (o == this) return true;
     79         if (!(o instanceof ProbabilityInfo)) return false;
     80         final ProbabilityInfo p = (ProbabilityInfo)o;
     81         if (!hasHistoricalInfo() && !p.hasHistoricalInfo()) {
     82             return mProbability == p.mProbability;
     83         }
     84         return mProbability == p.mProbability && mTimestamp == p.mTimestamp && mLevel == p.mLevel
     85                 && mCount == p.mCount;
     86     }
     87 }