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