/**
  * Search all contained {@link EntityDelta} for the first one with an existing {@link
  * RawContacts#_ID} value. Usually used when creating {@link AggregationExceptions} during an
  * update.
  */
 public long findRawContactId() {
   for (EntityDelta delta : this) {
     final Long rawContactId = delta.getValues().getAsLong(RawContacts._ID);
     if (rawContactId != null && rawContactId >= 0) {
       return rawContactId;
     }
   }
   return -1;
 }
 /** Find {@link RawContacts#_ID} of the requested {@link EntityDelta}. */
 public Long getRawContactId(int index) {
   if (index >= 0 && index < this.size()) {
     final EntityDelta delta = this.get(index);
     final ValuesDelta values = delta.getValues();
     if (values.isVisible()) {
       return values.getAsLong(RawContacts._ID);
     }
   }
   return null;
 }
  /**
   * Merge the "after" values from the given {@link EntitySet}, discarding any previous "after"
   * states. This is typically used when re-parenting user edits onto an updated {@link EntitySet}.
   */
  public static EntitySet mergeAfter(EntitySet local, EntitySet remote) {
    if (local == null) local = new EntitySet();

    // For each entity in the remote set, try matching over existing
    for (EntityDelta remoteEntity : remote) {
      final Long rawContactId = remoteEntity.getValues().getId();

      // Find or create local match and merge
      final EntityDelta localEntity = local.getByRawContactId(rawContactId);
      final EntityDelta merged = EntityDelta.mergeAfter(localEntity, remoteEntity);

      if (localEntity == null && merged != null) {
        // No local entry before, so insert
        local.add(merged);
      }
    }

    return local;
  }