org.javers.core.metamodel.type.ValueObjectType 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.reflection.ReflectionUtil;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
/**
* ValueObject class in client's domain model.
*
*
* Has list of mutable properties but no unique identifier.
*
*
* Two ValueObjects are compared property by property.
*
*
* Example:
*
* class Address {
* private String city;
* private String street;
* private String zip;
* ...
* }
*
*
* @author bartosz walacik
*/
public class ValueObjectType extends ManagedType{
private final boolean defaultType;
private final Optional> toStringFunction = Optional.empty();
ValueObjectType(ManagedClass valueObject){
super(valueObject);
this.defaultType = false;
}
public ValueObjectType(Class baseJavaClass, List allProperties){
this(new ManagedClass(baseJavaClass, allProperties, Collections.emptyList(), ManagedPropertiesFilter.empty()));
}
ValueObjectType(ManagedClass valueObject, Optional typeName, boolean isDefault) {
super(valueObject, typeName);
this.defaultType = isDefault;
}
@Override
ValueObjectType spawn(ManagedClass managedClass, Optional typeName) {
return new ValueObjectType(managedClass, typeName, defaultType);
}
@Override
public boolean canBePrototype() {
return !defaultType;
}
public String smartToString(Object value) {
if (value == null) {
return "";
}
return toStringFunction
.map(f -> f.apply(value))
.orElse(ReflectionUtil.reflectiveToString(value));
}
}