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 17package com.android.providers.contacts.aggregation.util; 18 19import java.util.ArrayList; 20import java.util.List; 21import java.util.Map; 22import java.util.Set; 23 24import static com.android.internal.util.Preconditions.checkNotNull; 25 26import android.util.ArrayMap; 27import android.util.ArraySet; 28 29/** 30 * Matching candidates for a raw contact, used in the contact aggregator. 31 */ 32public 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 38 public RawContactMatchingCandidates(List<MatchScore> mBestMatches) { 39 checkNotNull(mBestMatches); 40 this.mBestMatches = mBestMatches; 41 } 42 43 public RawContactMatchingCandidates() { 44 mBestMatches = new ArrayList<MatchScore>(); 45 } 46 47 public int getCount() { 48 return mBestMatches.size(); 49 } 50 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 64 public Set<Long> getRawContactIdSet() { 65 if (mRawContactIds == null) { 66 createRawContactIdSet(); 67 } 68 return mRawContactIds; 69 } 70 71 public Map<Long, Long> getRawContactToAccount() { 72 if (mRawContactToAccount == null) { 73 createRawContactToAccountMap(); 74 } 75 return mRawContactToAccount; 76 } 77 78 public Long getContactId(Long rawContactId) { 79 if (mRawContactToContact == null) { 80 createRawContactToContactMap(); 81 } 82 return mRawContactToContact.get(rawContactId); 83 } 84 85 public Long getAccountId(Long rawContactId) { 86 if (mRawContactToAccount == null) { 87 createRawContactToAccountMap(); 88 } 89 return mRawContactToAccount.get(rawContactId); 90 } 91 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 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 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