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

eu.mais_h.mathsync.FullContent Maven / Gradle / Ivy

The newest version!
package eu.mais_h.mathsync;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * Summary implementation simply using items content.
 */
public class FullContent implements Summary {

  static final Summary EMPTY = new FullContent(Collections.emptySet(), Collections.emptySet());

  private final Set added;
  private final Set removed;

  /**
   * Deserializes a summary from its JSON representation.
   *
   * @param tokener the JSON content.
   */
  FullContent(JSONTokener tokener) {
    JSONObject deserialized = new JSONObject(tokener);
    added = deserializeArray(deserialized.getJSONArray("added"));
    removed = deserializeArray(deserialized.getJSONArray("removed"));
  }

  private FullContent(Set added, Set removed) {
    this.added = added;
    this.removed = removed;
  }

  @Override
  public Summary plus(byte[] item) {
    Set addedCopy = new HashSet<>(added);
    Set removedCopy = new HashSet<>(removed);
    insertOrRemove(addedCopy, removedCopy, item);
    return new FullContent(addedCopy, removedCopy);
  }

  @Override
  public Summary plus(Iterator items) {
    Set addedCopy = new HashSet<>(added);
    Set removedCopy = new HashSet<>(removed);
    while (items.hasNext()) {
      insertOrRemove(addedCopy, removedCopy, items.next());
    }
    return new FullContent(addedCopy, removedCopy);
  }

  @Override
  public Summary minus(byte[] item) {
    Set addedCopy = new HashSet<>(added);
    Set removedCopy = new HashSet<>(removed);
    insertOrRemove(removedCopy, addedCopy, item);
    return new FullContent(addedCopy, removedCopy);
  }

  @Override
  public Summary minus(Iterator items) {
    Set addedCopy = new HashSet<>(added);
    Set removedCopy = new HashSet<>(removed);
    while (items.hasNext()) {
      insertOrRemove(removedCopy, addedCopy, items.next());
    }
    return new FullContent(addedCopy, removedCopy);
  }

  @Override
  public String toJSON() {
    JSONObject object = new JSONObject();
    object.put("added", serializeSet(added));
    object.put("removed", serializeSet(removed));
    return object.toString();
  }

  @Override
  public Difference toDifference() {
    return new SerializedDifference(unwrap(added), unwrap(removed));
  }

  private void insertOrRemove(Set mayInsert, Set mayRemove, byte[] item) {
    EquatableArray wrapped = new EquatableArray(item);
    if (mayRemove.contains(wrapped)) {
      mayRemove.remove(wrapped);
    } else {
      mayInsert.add(wrapped);
    }
  }

  private JSONArray serializeSet(Set set) {
    JSONArray array = new JSONArray();
    for (EquatableArray ea : set) {
      array.put(ea.toString());
    }
    return array;
  }

  private Set unwrap(Set wrapped) {
    Set unwrapped = new HashSet<>();
    for (EquatableArray ea : wrapped) {
      unwrapped.add(ea.content);
    }
    return unwrapped;
  }

  private Set deserializeArray(JSONArray array) {
    Set deserialized = new HashSet<>();
    for (int i = 0; i < array.length(); i++) {
      deserialized.add(new EquatableArray(array.getString(i)));
    }
    return deserialized;
  }

  private static final class EquatableArray {

    private final byte[] content;
    private final int hashCode;

    private EquatableArray(byte[] content) {
      this.content = content;
      hashCode = Arrays.hashCode(content);
    }

    private EquatableArray(String serialized) {
      this(Defaults.deserialize(serialized));
    }

    @Override
    public int hashCode() {
      return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
      if (this == obj) {
        return true;
      }
      if (!(obj instanceof EquatableArray)) {
        return false;
      }
      EquatableArray other = (EquatableArray)obj;
      if (!Arrays.equals(content, other.content)) {
        return false;
      }
      return true;
    }

    @Override
    public String toString() {
      return Defaults.serialize(content);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy