All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.easimart.EasimartRemoveOperation Maven / Gradle / Ivy

package co.easimart;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * An operation that removes every instance of an element from an array field.
 */
/** package */ class EasimartRemoveOperation implements EasimartFieldOperation {
  protected final HashSet objects = new HashSet<>();

  public EasimartRemoveOperation(Collection coll) {
    objects.addAll(coll);
  }

  @Override
  public JSONObject encode(EasimartEncoder objectEncoder) throws JSONException {
    JSONObject output = new JSONObject();
    output.put("__op", "Remove");
    output.put("objects", objectEncoder.encode(new ArrayList<>(objects)));
    return output;
  }

  @Override
  public EasimartFieldOperation mergeWithPrevious(EasimartFieldOperation previous) {
    if (previous == null) {
      return this;
    } else if (previous instanceof EasimartDeleteOperation) {
      return new EasimartSetOperation(objects);
    } else if (previous instanceof EasimartSetOperation) {
      Object value = ((EasimartSetOperation) previous).getValue();
      if (value instanceof JSONArray || value instanceof List) {
        return new EasimartSetOperation(this.apply(value, null));
      } else {
        throw new IllegalArgumentException("You can only add an item to a List or JSONArray.");
      }
    } else if (previous instanceof EasimartRemoveOperation) {
      HashSet result = new HashSet<>(((EasimartRemoveOperation) previous).objects);
      result.addAll(objects);
      return new EasimartRemoveOperation(result);
    } else {
      throw new IllegalArgumentException("Operation is invalid after previous operation.");
    }
  }

  @Override
  public Object apply(Object oldValue, String key) {
    if (oldValue == null) {
      return new ArrayList<>();
    } else if (oldValue instanceof JSONArray) {
      ArrayList old = EasimartFieldOperations.jsonArrayAsArrayList((JSONArray) oldValue);
      @SuppressWarnings("unchecked")
      ArrayList newValue = (ArrayList) this.apply(old, key);
      return new JSONArray(newValue);
    } else if (oldValue instanceof List) {
      ArrayList result = new ArrayList<>((List) oldValue);
      result.removeAll(objects);

      // Remove the removed objects from "objects" -- the items remaining
      // should be ones that weren't removed by object equality.
      ArrayList objectsToBeRemoved = new ArrayList<>(objects);
      objectsToBeRemoved.removeAll(result);

      // Build up set of object IDs for any EasimartObjects in the remaining objects-to-be-removed
      HashSet objectIds = new HashSet<>();
      for (Object obj : objectsToBeRemoved) {
        if (obj instanceof EasimartObject) {
          objectIds.add(((EasimartObject) obj).getObjectId());
        }
      }

      // And iterate over "result" to see if any other EasimartObjects need to be removed
      Iterator resultIterator = result.iterator();
      while (resultIterator.hasNext()) {
        Object obj = resultIterator.next();
        if (obj instanceof EasimartObject && objectIds.contains(((EasimartObject) obj).getObjectId())) {
          resultIterator.remove();
        }
      }
      return result;
    } else {
      throw new IllegalArgumentException("Operation is invalid after previous operation.");
    }
  }
}