/**
   * 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;
  }