1 /* 2 * Copyright (C) 2015 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 package com.android.providers.contacts.aggregation.util; 17 18 /** 19 * Captures the max score and match count for a specific raw contact or contact. 20 */ 21 public class MatchScore implements Comparable<MatchScore> { 22 // Scores a multiplied by this number to allow room for "fractional" scores 23 public static final int SCORE_SCALE = 1000; 24 // Best possible match score 25 public static final int MAX_SCORE = 100; 26 27 private long mRawContactId; 28 private long mContactId; 29 private long mAccountId; 30 31 private boolean mKeepIn; 32 private boolean mKeepOut; 33 34 private int mPrimaryScore; 35 private int mSecondaryScore; 36 private int mMatchCount; 37 MatchScore(long rawContactId, long contactId, long accountId)38 public MatchScore(long rawContactId, long contactId, long accountId) { 39 this.mRawContactId = rawContactId; 40 this.mContactId = contactId; 41 this.mAccountId = accountId; 42 } 43 MatchScore(long contactId)44 public MatchScore(long contactId) { 45 this.mRawContactId = 0; 46 this.mContactId = contactId; 47 this.mAccountId = 0; 48 } 49 reset(long rawContactId, long contactId, long accountId)50 public void reset(long rawContactId, long contactId, long accountId) { 51 this.mRawContactId = rawContactId; 52 this.mContactId = contactId; 53 this.mAccountId = accountId; 54 mKeepIn = false; 55 mKeepOut = false; 56 mPrimaryScore = 0; 57 mSecondaryScore = 0; 58 mMatchCount = 0; 59 } 60 reset(long contactId)61 public void reset(long contactId) { 62 this.reset(0l, contactId, 0l); 63 } 64 65 getRawContactId()66 public long getRawContactId() { 67 return mRawContactId; 68 } 69 getContactId()70 public long getContactId() { 71 return mContactId; 72 } 73 getAccountId()74 public long getAccountId() { 75 return mAccountId; 76 } 77 updatePrimaryScore(int score)78 public void updatePrimaryScore(int score) { 79 if (score > mPrimaryScore) { 80 mPrimaryScore = score; 81 } 82 mMatchCount++; 83 } 84 updateSecondaryScore(int score)85 public void updateSecondaryScore(int score) { 86 if (score > mSecondaryScore) { 87 mSecondaryScore = score; 88 } 89 mMatchCount++; 90 } 91 keepIn()92 public void keepIn() { 93 mKeepIn = true; 94 } 95 keepOut()96 public void keepOut() { 97 mKeepOut = true; 98 } 99 getScore()100 public int getScore() { 101 if (mKeepOut) { 102 return 0; 103 } 104 105 if (mKeepIn) { 106 return MAX_SCORE; 107 } 108 109 int score = (mPrimaryScore > mSecondaryScore ? mPrimaryScore : mSecondaryScore); 110 111 // Ensure that of two contacts with the same match score the one with more matching 112 // data elements wins. 113 return score * SCORE_SCALE + mMatchCount; 114 } 115 isKeepIn()116 public boolean isKeepIn() { 117 return mKeepIn; 118 } 119 isKeepOut()120 public boolean isKeepOut() { 121 return mKeepOut; 122 } 123 getPrimaryScore()124 public int getPrimaryScore() { 125 return mPrimaryScore; 126 } 127 getSecondaryScore()128 public int getSecondaryScore() { 129 return mSecondaryScore; 130 } 131 setPrimaryScore(int mPrimaryScore)132 public void setPrimaryScore(int mPrimaryScore) { 133 this.mPrimaryScore = mPrimaryScore; 134 } 135 136 /** 137 * Descending order of match score. 138 */ 139 @Override compareTo(MatchScore another)140 public int compareTo(MatchScore another) { 141 return another.getScore() - getScore(); 142 } 143 144 @Override toString()145 public String toString() { 146 return mRawContactId + "/" + mContactId + "/" + mAccountId + ": " + mPrimaryScore + 147 "/" + mSecondaryScore + "(" + mMatchCount + ")"; 148 } 149 } 150