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

com.parse.ParseAddOperation Maven / Gradle / Ivy

Go to download

A library that gives you access to the powerful Parse cloud platform from your Android app.

There is a newer version: 1.17.3
Show newest version
/*
 * Copyright (c) 2015-present, Parse, LLC.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
package com.parse;

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

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

/**
 * An operation that adds a new element to an array field.
 */
/** package */ class ParseAddOperation implements ParseFieldOperation {
  protected final ArrayList objects = new ArrayList<>();

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

  @Override
  public JSONObject encode(ParseEncoder objectEncoder) throws JSONException {
    JSONObject output = new JSONObject();
    output.put("__op", "Add");
    output.put("objects", objectEncoder.encode(objects));
    return output;
  }

  @Override
  public ParseFieldOperation mergeWithPrevious(ParseFieldOperation previous) {
    if (previous == null) {
      return this;
    } else if (previous instanceof ParseDeleteOperation) {
      return new ParseSetOperation(objects);
    } else if (previous instanceof ParseSetOperation) {
      Object value = ((ParseSetOperation) previous).getValue();
      if (value instanceof JSONArray) {
        ArrayList result = ParseFieldOperations.jsonArrayAsArrayList((JSONArray) value);
        result.addAll(objects);
        return new ParseSetOperation(new JSONArray(result));
      } else if (value instanceof List) {
        ArrayList result = new ArrayList<>((List) value);
        result.addAll(objects);
        return new ParseSetOperation(result);
      } else {
        throw new IllegalArgumentException("You can only add an item to a List or JSONArray.");
      }
    } else if (previous instanceof ParseAddOperation) {
      ArrayList result = new ArrayList<>(((ParseAddOperation) previous).objects);
      result.addAll(objects);
      return new ParseAddOperation(result);
    } else {
      throw new IllegalArgumentException("Operation is invalid after previous operation.");
    }
  }

  @Override
  public Object apply(Object oldValue, String key) {
    if (oldValue == null) {
      return objects;
    } else if (oldValue instanceof JSONArray) {
      ArrayList old = ParseFieldOperations.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.addAll(objects);
      return result;
    } else {
      throw new IllegalArgumentException("Operation is invalid after previous operation.");
    }
  }
}