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 
     17 package com.android.providers.contacts.aggregation.util;
     18 
     19 import java.util.ArrayList;
     20 import java.util.HashMap;
     21 import java.util.HashSet;
     22 import java.util.List;
     23 import java.util.Map;
     24 import java.util.Set;
     25 
     26 import static com.android.internal.util.Preconditions.checkNotNull;
     27 
     28 /**
     29  * Matching candidates for a raw contact, used in the contact aggregator.
     30  */
     31 public class RawContactMatchingCandidates {
     32     private List<MatchScore> mBestMatches;
     33     private Set<Long> mRawContactIds = null;
     34     private Map<Long, Long> mRawContactToContact = null;
     35     private Map<Long, Long> mRawContactToAccount = null;
     36 
     37     public RawContactMatchingCandidates(List<MatchScore> mBestMatches) {
     38         checkNotNull(mBestMatches);
     39         this.mBestMatches = mBestMatches;
     40     }
     41 
     42     public RawContactMatchingCandidates() {
     43         mBestMatches = new ArrayList<MatchScore>();
     44     }
     45 
     46     public int getCount() {
     47         return mBestMatches.size();
     48     }
     49 
     50     public void add(MatchScore score) {
     51         mBestMatches.add(score);
     52         if (mRawContactIds != null) {
     53             mRawContactIds.add(score.getRawContactId());
     54         }
     55         if (mRawContactToAccount != null) {
     56             mRawContactToAccount.put(score.getRawContactId(), score.getAccountId());
     57         }
     58         if (mRawContactToContact != null) {
     59             mRawContactToContact.put(score.getRawContactId(), score.getContactId());
     60         }
     61     }
     62 
     63     public Set<Long> getRawContactIdSet() {
     64         if (mRawContactIds == null) {
     65             createRawContactIdSet();
     66         }
     67         return mRawContactIds;
     68     }
     69 
     70     public Map<Long, Long> getRawContactToAccount() {
     71         if (mRawContactToAccount == null) {
     72             createRawContactToAccountMap();
     73         }
     74         return mRawContactToAccount;
     75     }
     76 
     77     public Long getContactId(Long rawContactId) {
     78         if (mRawContactToContact == null) {
     79             createRawContactToContactMap();
     80         }
     81         return mRawContactToContact.get(rawContactId);
     82     }
     83 
     84     public Long getAccountId(Long rawContactId) {
     85         if (mRawContactToAccount == null) {
     86             createRawContactToAccountMap();
     87         }
     88         return mRawContactToAccount.get(rawContactId);
     89     }
     90 
     91     private void createRawContactToContactMap() {
     92         mRawContactToContact = new HashMap<Long, Long>();
     93         for (int i = 0; i < mBestMatches.size(); i++) {
     94             mRawContactToContact.put(mBestMatches.get(i).getRawContactId(),
     95                     mBestMatches.get(i).getContactId());
     96         }
     97     }
     98 
     99     private void createRawContactToAccountMap() {
    100         mRawContactToAccount = new HashMap<Long, Long>();
    101         for (int i = 0; i <  mBestMatches.size(); i++) {
    102             mRawContactToAccount.put(mBestMatches.get(i).getRawContactId(),
    103                     mBestMatches.get(i).getAccountId());
    104         }
    105     }
    106 
    107     private void createRawContactIdSet() {
    108         mRawContactIds = new HashSet<Long>();
    109         for (int i = 0; i < mBestMatches.size(); i++) {
    110             mRawContactIds.add(mBestMatches.get(i).getRawContactId());
    111         }
    112     }
    113 }
    114