org.openlca.git.model.Change Maven / Gradle / Ivy
package org.openlca.git.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Change extends ModelRef {
public final ChangeType changeType;
private Change(ChangeType changeType, ModelRef ref) {
super(ref);
this.changeType = changeType;
}
@Override
protected String fieldsToString() {
var s = super.fieldsToString();
return s + ", changeType=" + changeType;
}
public static Change add(ModelRef ref) {
return new Change(ChangeType.ADD, ref);
}
public static Change modify(ModelRef ref) {
return new Change(ChangeType.MODIFY, ref);
}
public static Change delete(ModelRef ref) {
return new Change(ChangeType.DELETE, ref);
}
public static List move(ModelRef oldRef, ModelRef newRef) {
return Arrays.asList(delete(oldRef), add(newRef));
}
public static List of(Diff diff) {
return of(Arrays.asList(diff));
}
public static List of(List diffs) {
var changes = new ArrayList();
for (var diff : diffs) {
if (diff.diffType == DiffType.ADDED) {
changes.add(add(diff.newRef));
} else if (diff.diffType == DiffType.MODIFIED) {
changes.add(modify(diff.newRef));
} else if (diff.diffType == DiffType.DELETED) {
changes.add(delete(diff.oldRef));
} else if (diff.diffType == DiffType.MOVED) {
changes.addAll(move(diff.oldRef, diff.newRef));
}
}
return changes;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Change c))
return false;
if (c.changeType != changeType)
return false;
return super.equals(o);
}
@Override
public int hashCode() {
return (changeType.name() + "/" + path).hashCode();
}
public enum ChangeType {
ADD,
MODIFY,
DELETE;
}
}