Home | History | Annotate | Download | only in datasources
      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 
     17 package com.android.dialer.calllog.datasources;
     18 
     19 import android.content.ContentValues;
     20 import android.util.ArrayMap;
     21 import android.util.ArraySet;
     22 import com.android.dialer.common.Assert;
     23 
     24 /** A collection of mutations to the annotated call log. */
     25 public final class CallLogMutations {
     26 
     27   private final ArrayMap<Long, ContentValues> inserts = new ArrayMap<>();
     28   private final ArrayMap<Long, ContentValues> updates = new ArrayMap<>();
     29   private final ArraySet<Long> deletes = new ArraySet<>();
     30 
     31   /**
     32    * @param contentValues an entire row not including the ID
     33    * @throws IllegalStateException if this {@link CallLogMutations} already contains an insert,
     34    *     update, or delete with the provided id
     35    */
     36   public void insert(long id, ContentValues contentValues) {
     37     Assert.checkArgument(!inserts.containsKey(id), "Can't insert row already scheduled for insert");
     38     Assert.checkArgument(!updates.containsKey(id), "Can't insert row scheduled for update");
     39     Assert.checkArgument(!deletes.contains(id), "Can't insert row scheduled for delete");
     40 
     41     inserts.put(id, contentValues);
     42   }
     43 
     44   /**
     45    * Stores a database update using the provided ID and content values. If this {@link
     46    * CallLogMutations} object already contains an update with the specified ID, the existing content
     47    * values are merged with the provided ones, with the provided ones overwriting the existing ones
     48    * for values with the same key.
     49    *
     50    * @param contentValues the specific columns to update, not including the ID.
     51    * @throws IllegalStateException if this {@link CallLogMutations} already contains an insert or
     52    *     delete with the provided id
     53    */
     54   public void update(long id, ContentValues contentValues) {
     55     Assert.checkArgument(!inserts.containsKey(id), "Can't update row scheduled for insert");
     56     Assert.checkArgument(!deletes.contains(id), "Can't delete row scheduled for delete");
     57 
     58     ContentValues existingContentValues = updates.get(id);
     59     if (existingContentValues != null) {
     60       existingContentValues.putAll(contentValues);
     61     } else {
     62       updates.put(id, contentValues);
     63     }
     64   }
     65 
     66   /**
     67    * @throws IllegalStateException if this {@link CallLogMutations} already contains an insert,
     68    *     update, or delete with the provided id
     69    */
     70   public void delete(long id) {
     71     Assert.checkArgument(!inserts.containsKey(id), "Can't delete row scheduled for insert");
     72     Assert.checkArgument(!updates.containsKey(id), "Can't delete row scheduled for update");
     73     Assert.checkArgument(!deletes.contains(id), "Can't delete row already scheduled for delete");
     74 
     75     deletes.add(id);
     76   }
     77 
     78   public boolean isEmpty() {
     79     return inserts.isEmpty() && updates.isEmpty() && deletes.isEmpty();
     80   }
     81 
     82   /**
     83    * Get the pending inserts.
     84    *
     85    * @return the pending inserts where the key is the annotated call log database ID and the values
     86    *     are values to be inserted (not including the ID)
     87    */
     88   public ArrayMap<Long, ContentValues> getInserts() {
     89     return inserts;
     90   }
     91 
     92   /**
     93    * Get the pending updates.
     94    *
     95    * @return the pending updates where the key is the annotated call log database ID and the values
     96    *     are values to be updated (not including the ID)
     97    */
     98   public ArrayMap<Long, ContentValues> getUpdates() {
     99     return updates;
    100   }
    101 
    102   /**
    103    * Get the pending deletes.
    104    *
    105    * @return the annotated call log database IDs corresponding to the rows to be deleted
    106    */
    107   public ArraySet<Long> getDeletes() {
    108     return deletes;
    109   }
    110 }
    111