org.javers.core.diff.GraphPair Maven / Gradle / Ivy
package org.javers.core.diff;
import org.javers.core.commit.CommitMetadata;
import org.javers.core.graph.ObjectGraph;
import org.javers.core.graph.ObjectNode;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import static org.javers.common.collections.Sets.difference;
/**
* @author bartosz walacik
*/
public class GraphPair {
private final ObjectGraph leftGraph;
private final ObjectGraph rightGraph;
private final Collection onlyOnLeft;
private final Collection onlyOnRight;
private final Optional commitMetadata;
GraphPair(ObjectGraph leftGraph, ObjectGraph rightGraph) {
this(leftGraph, rightGraph, Optional.empty());
}
public GraphPair(ObjectGraph leftGraph, ObjectGraph rightGraph, Optional commitMetadata) {
this.leftGraph = leftGraph;
this.rightGraph = rightGraph;
Function hasher = objectNode -> objectNode.cdoHashCode();
this.onlyOnLeft = difference(leftGraph.nodes(), rightGraph.nodes(), hasher);
this.onlyOnRight = difference(rightGraph.nodes(), leftGraph.nodes(), hasher);
this.commitMetadata = commitMetadata;
}
//for initial
public GraphPair(ObjectGraph currentGraph) {
this.leftGraph = new EmptyGraph();
this.rightGraph = currentGraph;
this.onlyOnLeft = Collections.emptySet();
this.onlyOnRight = rightGraph.nodes();
this.commitMetadata = Optional.empty();
}
public Collection getOnlyOnLeft() {
return onlyOnLeft;
}
public Collection getOnlyOnRight() {
return onlyOnRight;
}
public Set getLeftNodeSet() {
return leftGraph.nodes();
}
public Set getRightNodeSet() {
return rightGraph.nodes();
}
public Optional getCommitMetadata() {
return commitMetadata;
}
}