1 /* 2 * Copyright (C) 2017 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.dialer.calllog.datasources.util; 17 18 import android.content.ContentValues; 19 import com.android.dialer.common.Assert; 20 import java.util.Iterator; 21 import java.util.List; 22 23 /** Convenience class for aggregating row values. */ 24 public class RowCombiner { 25 private final List<ContentValues> individualRowsSortedByTimestampDesc; 26 private final ContentValues combinedRow = new ContentValues(); 27 RowCombiner(List<ContentValues> individualRowsSortedByTimestampDesc)28 public RowCombiner(List<ContentValues> individualRowsSortedByTimestampDesc) { 29 Assert.checkArgument(!individualRowsSortedByTimestampDesc.isEmpty()); 30 this.individualRowsSortedByTimestampDesc = individualRowsSortedByTimestampDesc; 31 } 32 33 /** Use the most recent value for the specified column. */ useMostRecentLong(String columnName)34 public RowCombiner useMostRecentLong(String columnName) { 35 combinedRow.put(columnName, individualRowsSortedByTimestampDesc.get(0).getAsLong(columnName)); 36 return this; 37 } 38 39 /** Asserts that all column values for the given column name are the same, and uses it. */ useSingleValueString(String columnName)40 public RowCombiner useSingleValueString(String columnName) { 41 Iterator<ContentValues> iterator = individualRowsSortedByTimestampDesc.iterator(); 42 String singleValue = iterator.next().getAsString(columnName); 43 while (iterator.hasNext()) { 44 Assert.checkState(iterator.next().getAsString(columnName).equals(singleValue)); 45 } 46 combinedRow.put(columnName, singleValue); 47 return this; 48 } 49 combine()50 public ContentValues combine() { 51 return combinedRow; 52 } 53 } 54