Home | History | Annotate | Download | only in util
      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 import java.util.Objects;
     23 
     24 /** Convenience class for aggregating row values. */
     25 public class RowCombiner {
     26   private final List<ContentValues> individualRowsSortedByTimestampDesc;
     27   private final ContentValues combinedRow = new ContentValues();
     28 
     29   public RowCombiner(List<ContentValues> individualRowsSortedByTimestampDesc) {
     30     Assert.checkArgument(!individualRowsSortedByTimestampDesc.isEmpty());
     31     this.individualRowsSortedByTimestampDesc = individualRowsSortedByTimestampDesc;
     32   }
     33 
     34   /** Use the most recent value for the specified column. */
     35   public RowCombiner useMostRecentInt(String columnName) {
     36     combinedRow.put(
     37         columnName, individualRowsSortedByTimestampDesc.get(0).getAsInteger(columnName));
     38     return this;
     39   }
     40 
     41   /** Use the most recent value for the specified column. */
     42   public RowCombiner useMostRecentLong(String columnName) {
     43     combinedRow.put(columnName, individualRowsSortedByTimestampDesc.get(0).getAsLong(columnName));
     44     return this;
     45   }
     46 
     47   /** Use the most recent value for the specified column. */
     48   public RowCombiner useMostRecentString(String columnName) {
     49     combinedRow.put(columnName, individualRowsSortedByTimestampDesc.get(0).getAsString(columnName));
     50     return this;
     51   }
     52 
     53   public RowCombiner useMostRecentBlob(String columnName) {
     54     combinedRow.put(
     55         columnName, individualRowsSortedByTimestampDesc.get(0).getAsByteArray(columnName));
     56     return this;
     57   }
     58 
     59   /** Asserts that all column values for the given column name are the same, and uses it. */
     60   public RowCombiner useSingleValueString(String columnName) {
     61     Iterator<ContentValues> iterator = individualRowsSortedByTimestampDesc.iterator();
     62     String singleValue = iterator.next().getAsString(columnName);
     63     while (iterator.hasNext()) {
     64       String current = iterator.next().getAsString(columnName);
     65       Assert.checkState(Objects.equals(singleValue, current), "Values different for " + columnName);
     66     }
     67     combinedRow.put(columnName, singleValue);
     68     return this;
     69   }
     70 
     71   /** Asserts that all column values for the given column name are the same, and uses it. */
     72   public RowCombiner useSingleValueLong(String columnName) {
     73     Iterator<ContentValues> iterator = individualRowsSortedByTimestampDesc.iterator();
     74     Long singleValue = iterator.next().getAsLong(columnName);
     75     while (iterator.hasNext()) {
     76       Long current = iterator.next().getAsLong(columnName);
     77       Assert.checkState(Objects.equals(singleValue, current), "Values different for " + columnName);
     78     }
     79     combinedRow.put(columnName, singleValue);
     80     return this;
     81   }
     82 
     83   /** Asserts that all column values for the given column name are the same, and uses it. */
     84   public RowCombiner useSingleValueInt(String columnName) {
     85     Iterator<ContentValues> iterator = individualRowsSortedByTimestampDesc.iterator();
     86     Integer singleValue = iterator.next().getAsInteger(columnName);
     87     while (iterator.hasNext()) {
     88       Integer current = iterator.next().getAsInteger(columnName);
     89       Assert.checkState(Objects.equals(singleValue, current), "Values different for " + columnName);
     90     }
     91     combinedRow.put(columnName, singleValue);
     92     return this;
     93   }
     94 
     95   /** Performs a bitwise OR on the specified column and yields the result. */
     96   public RowCombiner bitwiseOr(String columnName) {
     97     int combinedValue = 0;
     98     for (ContentValues val : individualRowsSortedByTimestampDesc) {
     99       combinedValue |= val.getAsInteger(columnName);
    100     }
    101     combinedRow.put(columnName, combinedValue);
    102     return this;
    103   }
    104 
    105   public ContentValues combine() {
    106     return combinedRow;
    107   }
    108 }
    109