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

io.datakernel.aggregation.ot.AggregationDiff Maven / Gradle / Ivy

package io.datakernel.aggregation.ot;

import io.datakernel.aggregation.AggregationChunk;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

import static io.datakernel.util.CollectionUtils.union;

public final class AggregationDiff {
	private final Set addedChunks;
	private final Set removedChunks;

	private AggregationDiff(Set addedChunks, Set removedChunks) {
		this.addedChunks = addedChunks;
		this.removedChunks = removedChunks;
	}

	public static AggregationDiff of(Set addedChunks, Set removedChunks) {
		return new AggregationDiff(addedChunks, removedChunks);
	}

	public static AggregationDiff of(Set addedChunks) {
		return new AggregationDiff(addedChunks, Collections.emptySet());
	}

	public static AggregationDiff empty() {
		return new AggregationDiff(Collections.emptySet(), Collections.emptySet());
	}

	public Set getAddedChunks() {
		return addedChunks;
	}

	public Set getRemovedChunks() {
		return removedChunks;
	}

	public AggregationDiff inverse() {
		return new AggregationDiff(removedChunks, addedChunks);
	}

	public boolean isEmpty() {
		return addedChunks.isEmpty() && removedChunks.isEmpty();
	}

	public static AggregationDiff squash(AggregationDiff commit1, AggregationDiff commit2) {
		Set addedChunks1 = new LinkedHashSet<>(commit1.addedChunks);
		addedChunks1.removeAll(commit2.removedChunks);
		Set addedChunks2 = new LinkedHashSet<>(commit2.addedChunks);
		addedChunks2.removeAll(commit1.removedChunks);
		Set addedChunks = union(addedChunks1, addedChunks2);

		Set removedChunks1 = new LinkedHashSet<>(commit1.removedChunks);
		removedChunks1.removeAll(commit2.addedChunks);
		Set removedChunks2 = new LinkedHashSet<>(commit2.removedChunks);
		removedChunks2.removeAll(commit1.addedChunks);
		Set removedChunks = union(removedChunks1, removedChunks2);

		return of(addedChunks, removedChunks);
	}

	@Override
	public String toString() {
		return "{addedChunks:" + addedChunks.size() + ", removedChunks:" + removedChunks.size() + '}';
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		AggregationDiff that = (AggregationDiff) o;

		if (addedChunks != null ? !addedChunks.equals(that.addedChunks) : that.addedChunks != null) return false;
		return removedChunks != null ? removedChunks.equals(that.removedChunks) : that.removedChunks == null;
	}

	@Override
	public int hashCode() {
		int result = addedChunks != null ? addedChunks.hashCode() : 0;
		result = 31 * result + (removedChunks != null ? removedChunks.hashCode() : 0);
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy