ProbabilityInfo.java revision 8ffc631826b108423f98e3ff4d987f067cbc4e0c
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 17package com.android.inputmethod.latin.makedict; 18 19import com.android.inputmethod.latin.BinaryDictionary; 20import com.android.inputmethod.latin.utils.CombinedFormatUtils; 21 22import java.util.Arrays; 23 24public final class ProbabilityInfo { 25 public final int mProbability; 26 // mTimestamp, mLevel and mCount are historical info. These values are depend on the 27 // implementation in native code; thus, we must not use them and have any assumptions about 28 // them except for tests. 29 public final int mTimestamp; 30 public final int mLevel; 31 public final int mCount; 32 33 public static ProbabilityInfo max(final ProbabilityInfo probabilityInfo1, 34 final ProbabilityInfo probabilityInfo2) { 35 if (probabilityInfo1 == null) { 36 return probabilityInfo2; 37 } 38 if (probabilityInfo2 == null) { 39 return probabilityInfo1; 40 } 41 if (probabilityInfo1.mProbability > probabilityInfo2.mProbability) { 42 return probabilityInfo1; 43 } else { 44 return probabilityInfo2; 45 } 46 } 47 48 public ProbabilityInfo(final int probability) { 49 this(probability, BinaryDictionary.NOT_A_VALID_TIMESTAMP, 0, 0); 50 } 51 52 public ProbabilityInfo(final int probability, final int timestamp, final int level, 53 final int count) { 54 mProbability = probability; 55 mTimestamp = timestamp; 56 mLevel = level; 57 mCount = count; 58 } 59 60 public boolean hasHistoricalInfo() { 61 return mTimestamp != BinaryDictionary.NOT_A_VALID_TIMESTAMP; 62 } 63 64 @Override 65 public int hashCode() { 66 if (hasHistoricalInfo()) { 67 return Arrays.hashCode(new Object[] { mProbability, mTimestamp, mLevel, mCount }); 68 } else { 69 return Arrays.hashCode(new Object[] { mProbability }); 70 } 71 } 72 73 @Override 74 public String toString() { 75 return CombinedFormatUtils.formatProbabilityInfo(this); 76 } 77 78 @Override 79 public boolean equals(Object o) { 80 if (o == this) return true; 81 if (!(o instanceof ProbabilityInfo)) return false; 82 final ProbabilityInfo p = (ProbabilityInfo)o; 83 if (!hasHistoricalInfo() && !p.hasHistoricalInfo()) { 84 return mProbability == p.mProbability; 85 } 86 return mProbability == p.mProbability && mTimestamp == p.mTimestamp && mLevel == p.mLevel 87 && mCount == p.mCount; 88 } 89}