Home | History | Annotate | Download | only in util
      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 
     38     public MatchScore(long rawContactId, long contactId, long accountId) {
     39         this.mRawContactId = rawContactId;
     40         this.mContactId = contactId;
     41         this.mAccountId = accountId;
     42     }
     43 
     44     public MatchScore(long contactId) {
     45         this.mRawContactId = 0;
     46         this.mContactId = contactId;
     47         this.mAccountId = 0;
     48     }
     49 
     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 
     61     public void reset(long contactId) {
     62         this.reset(0l, contactId, 0l);
     63     }
     64 
     65 
     66     public long getRawContactId() {
     67         return mRawContactId;
     68     }
     69 
     70     public long getContactId() {
     71         return mContactId;
     72     }
     73 
     74     public long getAccountId() {
     75         return mAccountId;
     76     }
     77 
     78     public void updatePrimaryScore(int score) {
     79         if (score > mPrimaryScore) {
     80             mPrimaryScore = score;
     81         }
     82         mMatchCount++;
     83     }
     84 
     85     public void updateSecondaryScore(int score) {
     86         if (score > mSecondaryScore) {
     87             mSecondaryScore = score;
     88         }
     89         mMatchCount++;
     90     }
     91 
     92     public void keepIn() {
     93         mKeepIn = true;
     94     }
     95 
     96     public void keepOut() {
     97         mKeepOut = true;
     98     }
     99 
    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 
    116     public boolean isKeepIn() {
    117         return mKeepIn;
    118     }
    119 
    120     public boolean isKeepOut() {
    121         return mKeepOut;
    122     }
    123 
    124     public int getPrimaryScore() {
    125         return mPrimaryScore;
    126     }
    127 
    128     public int getSecondaryScore() {
    129         return mSecondaryScore;
    130     }
    131 
    132     public void setPrimaryScore(int mPrimaryScore) {
    133         this.mPrimaryScore = mPrimaryScore;
    134     }
    135 
    136     /**
    137      * Descending order of match score.
    138      */
    139     @Override
    140     public int compareTo(MatchScore another) {
    141         return another.getScore() - getScore();
    142     }
    143 
    144     @Override
    145     public String toString() {
    146         return mRawContactId + "/" + mContactId + "/" + mAccountId + ": " + mPrimaryScore +
    147                 "/" + mSecondaryScore + "(" + mMatchCount + ")";
    148     }
    149 }
    150