package org.bson.codecs.pojo;
import org.bson.codecs.Codec;
public final class PropertyModel<T> {
private final String name;
private final String readName;
private final String writeName;
private final TypeData<T> typeData;
private final Codec<T> codec;
private final PropertySerialization<T> propertySerialization;
private final Boolean useDiscriminator;
private final PropertyAccessor<T> propertyAccessor;
private final String error;
private volatile Codec<T> cachedCodec;
PropertyModel(final String name, final String readName, final String writeName, final TypeData<T> typeData,
final Codec<T> codec, final PropertySerialization<T> propertySerialization, final Boolean useDiscriminator,
final PropertyAccessor<T> propertyAccessor, final String error) {
this.name = name;
this.readName = readName;
this.writeName = writeName;
this.typeData = typeData;
this.codec = codec;
this.cachedCodec = codec;
this.propertySerialization = propertySerialization;
this.useDiscriminator = useDiscriminator;
this.propertyAccessor = propertyAccessor;
this.error = error;
}
public static <T> PropertyModelBuilder<T> builder() {
return new PropertyModelBuilder<T>();
}
public String getName() {
return name;
}
public String getWriteName() {
return writeName;
}
public String getReadName() {
return readName;
}
public boolean isWritable() {
return writeName != null;
}
public boolean isReadable() {
return readName != null;
}
public TypeData<T> getTypeData() {
return typeData;
}
public Codec<T> getCodec() {
return codec;
}
public boolean shouldSerialize(final T value) {
return propertySerialization.shouldSerialize(value);
}
public PropertyAccessor<T> getPropertyAccessor() {
return propertyAccessor;
}
public Boolean useDiscriminator() {
return useDiscriminator;
}
@Override
public String toString() {
return "PropertyModel{"
+ "propertyName='" + name + "'"
+ ", readName='" + readName + "'"
+ ", writeName='" + writeName + "'"
+ ", typeData=" + typeData
+ "}";
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PropertyModel<?> that = (PropertyModel<?>) o;
if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null) {
return false;
}
if (getReadName() != null ? !getReadName().equals(that.getReadName()) : that.getReadName() != null) {
return false;
}
if (getWriteName() != null ? !getWriteName().equals(that.getWriteName()) : that.getWriteName() != null) {
return false;
}
if (getTypeData() != null ? !getTypeData().equals(that.getTypeData()) : that.getTypeData() != null) {
return false;
}
if (getCodec() != null ? !getCodec().equals(that.getCodec()) : that.getCodec() != null) {
return false;
}
if (getPropertySerialization() != null ? !getPropertySerialization().equals(that.getPropertySerialization()) : that
.getPropertySerialization() != null) {
return false;
}
if (useDiscriminator != null ? !useDiscriminator.equals(that.useDiscriminator) : that.useDiscriminator != null) {
return false;
}
if (getPropertyAccessor() != null ? !getPropertyAccessor().equals(that.getPropertyAccessor())
: that.getPropertyAccessor() != null) {
return false;
}
if (getError() != null ? !getError().equals(that.getError()) : that.getError() != null) {
return false;
}
if (getCachedCodec() != null ? !getCachedCodec().equals(that.getCachedCodec()) : that.getCachedCodec() != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = getName() != null ? getName().hashCode() : 0;
result = 31 * result + (getReadName() != null ? getReadName().hashCode() : 0);
result = 31 * result + (getWriteName() != null ? getWriteName().hashCode() : 0);
result = 31 * result + (getTypeData() != null ? getTypeData().hashCode() : 0);
result = 31 * result + (getCodec() != null ? getCodec().hashCode() : 0);
result = 31 * result + (getPropertySerialization() != null ? getPropertySerialization().hashCode() : 0);
result = 31 * result + (useDiscriminator != null ? useDiscriminator.hashCode() : 0);
result = 31 * result + (getPropertyAccessor() != null ? getPropertyAccessor().hashCode() : 0);
result = 31 * result + (getError() != null ? getError().hashCode() : 0);
result = 31 * result + (getCachedCodec() != null ? getCachedCodec().hashCode() : 0);
return result;
}
boolean hasError() {
return error != null;
}
String getError() {
return error;
}
PropertySerialization<T> getPropertySerialization() {
return propertySerialization;
}
void cachedCodec(final Codec<T> codec) {
this.cachedCodec = codec;
}
Codec<T> getCachedCodec() {
return cachedCodec;
}
}