org.javers.core.metamodel.type.CustomType 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.metamodel.type;
import org.javers.common.validation.Validate;
import org.javers.core.JaversBuilder;
import org.javers.core.diff.changetype.PropertyChange;
import org.javers.core.diff.changetype.PropertyChangeMetadata;
import org.javers.core.diff.custom.CustomPropertyComparator;
import org.javers.core.metamodel.property.Property;
import java.lang.reflect.Type;
import java.util.Optional;
/**
*
* Custom Types are not easy to manage, use it as a last resort,
* only for corner cases like comparing custom Collection types.
*
*
* JaVers treats a Custom Type as a black box
* and doesn't take any assumptions about its content or behaviour.
* It's a "not modeled" type, somehow similar to unbounded wildcard {@code }.
*
*
* Objects of Custom Type are compared by a {@link CustomPropertyComparator}.
* Registering this comparator is the only way to map a Custom Type.
*
*
* Custom Types are serialized to JSON using Gson defaults.
*
* @param Custom Type
* @see JaversBuilder#registerCustomType(Class, CustomPropertyComparator)
*/
public class CustomType extends ClassType implements CustomComparableType {
private final CustomPropertyComparatorNullSafe comparator;
public CustomType(Type baseJavaType, CustomPropertyComparator comparator) {
super(baseJavaType);
Validate.argumentIsNotNull(comparator);
this.comparator = new CustomPropertyComparatorNullSafe(comparator);
}
@Override
public boolean equals(Object left, Object right) {
return comparator.equals((T)left, (T)right);
}
CustomPropertyComparator getComparator() {
return comparator;
}
@Override
public boolean hasCustomValueComparator() {
return true;
}
@Override
public String valueToString(Object value) {
return comparator.toString((T) value);
}
private static class CustomPropertyComparatorNullSafe
extends CustomValueComparatorNullSafe
implements CustomPropertyComparator
{
private final CustomPropertyComparator delegate;
public CustomPropertyComparatorNullSafe(CustomPropertyComparator delegate) {
super(delegate);
this.delegate = delegate;
}
@Override
public Optional compare(T left, T right, PropertyChangeMetadata metadata, Property property) {
return delegate.compare(left, right, metadata, property);
}
}
}