org.javers.core.ChangesByObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javers-core Show documentation
Show all versions of javers-core Show documentation
JaVers - object auditing and diff framework for Java
package org.javers.core;
import org.javers.common.string.PrettyValuePrinter;
import org.javers.core.diff.Change;
import org.javers.core.diff.changetype.NewObject;
import org.javers.core.diff.changetype.ObjectRemoved;
import org.javers.core.diff.changetype.PropertyChange;
import org.javers.core.metamodel.object.GlobalId;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* List of Changes done on a specific object.
*
*
* {@link #getGlobalId()}} changed object Id
* {@link #get()} list of Changes
*
*/
public final class ChangesByObject {
private final List changes;
private final GlobalId globalId;
private final transient PrettyValuePrinter valuePrinter;
ChangesByObject(GlobalId globalId, List changes, PrettyValuePrinter valuePrinter) {
this.changes = changes;
this.globalId = globalId;
this.valuePrinter = valuePrinter;
}
public List get() {
return Collections.unmodifiableList(changes);
}
/**
* Id of a changed Entity.
*
* For Value Objects — Id of an owning Entity.
*/
public GlobalId getGlobalId() {
return globalId;
}
public List getNewObjects() {
return (List) changes.stream().filter(c -> c instanceof NewObject)
.sorted(Comparator.comparingInt(it -> it.getAffectedGlobalId().value().length()))
.collect(Collectors.toList());
}
public List getObjectsRemoved() {
return (List) changes.stream().filter(c -> c instanceof ObjectRemoved)
.sorted(Comparator.comparingInt(it -> it.getAffectedGlobalId().value().length()))
.collect(Collectors.toList());
}
public List getPropertyChanges() {
return (List) changes.stream().filter(c -> c instanceof PropertyChange)
.sorted(Comparator.comparing(a -> ((PropertyChange) a).getPropertyNameWithPath()))
.collect(Collectors.toList());
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
getNewObjects().forEach(c ->
b.append("* " + c.prettyPrint(valuePrinter) + "\n")
);
getObjectsRemoved().forEach(c ->
b.append("* " + c.prettyPrint(valuePrinter) + "\n")
);
if (!getPropertyChanges().isEmpty()) {
b.append("* changes on " + globalId.value() + " :\n");
}
getPropertyChanges().forEach(c ->
b.append(" - " + c.prettyPrint(valuePrinter).replace("\n", "\n ") + "\n")
);
return b.toString();
}
}