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

cn.leancloud.ops.CompoundOperation Maven / Gradle / Ivy

package cn.leancloud.ops;

import cn.leancloud.LCObject;

import java.util.*;

public class CompoundOperation extends BaseOperation {
  private List operations = new LinkedList();

  public CompoundOperation(String field) {
    super("Compound", field, null, false);
  }
  public CompoundOperation(String field, ObjectFieldOperation... ops) {
    this(field);
    operations.addAll(Arrays.asList(ops));
  }

  public List getSubOperations() {
    return this.operations;
  }

  @Override
  public boolean checkCircleReference(Map markMap) {
    boolean result = false;
    for (ObjectFieldOperation op : operations) {
      result = result || op.checkCircleReference(markMap);
    }
    return result;
  }

  public Object apply(Object obj) {
    for (ObjectFieldOperation op: operations) {
      obj = op.apply(obj);
    }
    return obj;
  }

  protected ObjectFieldOperation mergeWithPrevious(ObjectFieldOperation previous) {
    operations.add(previous);
    return this;
  }

  public List> encodeRestOp(LCObject parent) {
    List> result = new ArrayList>();
    if (null == parent) {
      return result;
    }
    String requestEndPoint = parent.getRequestRawEndpoint();
    String requestMethod = parent.getRequestMethod();
    for (int i = 1; i < this.operations.size(); i++) {
      ObjectFieldOperation tmp = this.operations.get(i);
      Map tmpOp = tmp.encode();

      Map tmpResult = Utils.makeCompletedRequest(parent.getObjectId(), requestEndPoint, requestMethod, tmpOp);
      if (null != tmpResult) {
        result.add(tmpResult);
      }
    }
    return result;
  }

  private Map encodeHeadOp() {
    if (this.operations.size() < 1) {
      return null;
    }
    // just return the first Operation.
    return this.operations.get(0).encode();
  }

  public Map encode() {
    return encodeHeadOp();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy