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

com.avos.avoscloud.ops.AddUniqueOp Maven / Gradle / Ivy

package com.avos.avoscloud.ops;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUtils;

public class AddUniqueOp extends CollectionAddOp {
  private Set values = new HashSet();

  public Set getValues() {
    return values;
  }

  public AddUniqueOp() {
    super();
  }

  public AddUniqueOp(String key, Object... values) {
    super(key, OpType.AddUnique);
    if (values != null) {
      for (Object obj : values) {
        this.values.add(obj);
      }
    }
  }

  @Override
  public Map encodeOp() {
    return AVUtils.createArrayOpMap(key, this.type.name(), getParsedValues());
  }

  public Object apply(Object oldValue) {
    Set result = new HashSet();
    if (oldValue != null) {
      result.addAll((Collection) oldValue);
    }
    if (getValues() != null) {
      result.addAll(getValues());
    }
    return new LinkedList(result);
  }

  @Override
  public AVOp merge(AVOp other) {
    assertKeyEquals(other);
    switch (other.type()) {
      case Null:
        return this;
      case Set:
        return other;
      case AddUnique:
        this.values.addAll(other.cast(AddUniqueOp.class).values);
        return this;
      case AddRelation:
      case Remove:
      case Add:
      case RemoveRelation:
        return new CompoundOp(key, this, other);
      case Increment:
        throw new UnsupportedOperationException("Could not increment an non-numberic value.");
      case Delete:
        return other;
      case Compound:
        other.cast(CompoundOp.class).addFirst(this);
        return other;
      default:
        throw new IllegalStateException("Unknow op type " + other.type());
    }
  }
}