org.bson.codecs.pojo.PropertyModelBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-java-driver Show documentation
Show all versions of mongo-java-driver Show documentation
The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bson.codecs.pojo;
import org.bson.codecs.Codec;
import java.lang.annotation.Annotation;
import java.util.List;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static org.bson.assertions.Assertions.notNull;
import static org.bson.codecs.pojo.PojoBuilderHelper.stateNotNull;
/**
* A builder for programmatically creating {@code PropertyModels}.
*
* @param the type of the property
* @since 3.5
* @see PropertyModel
*/
public final class PropertyModelBuilder {
private String name;
private String readName;
private String writeName;
private TypeData typeData;
private PropertySerialization propertySerialization;
private Codec codec;
private PropertyAccessor propertyAccessor;
private List readAnnotations = emptyList();
private List writeAnnotations = emptyList();
private Boolean discriminatorEnabled;
PropertyModelBuilder() {
}
/**
* @return the property name
*/
public String getName() {
return name;
}
/**
* @return the name of the property to use as the key when deserializing the data from BSON.
*/
public String getReadName() {
return readName;
}
/**
* Sets the readName, the key for this property when deserializing the data from BSON.
*
* Note: A null means this property will not used when deserializing.
*
* @param readName the name of the property to use as the key when deserializing the data from BSON.
* @return this
*/
public PropertyModelBuilder readName(final String readName) {
this.readName = readName;
return this;
}
/**
* @return the name of the property to use as the key when serializing the data into BSON.
*/
public String getWriteName() {
return writeName;
}
/**
* Sets the writeName, the key for this property when serializing the data into BSON.
*
* Note: A null means this property will not be serialized.
*
* @param writeName the name of the property to use as the key when serializing the data into BSON.
* @return this
*/
public PropertyModelBuilder writeName(final String writeName) {
this.writeName = writeName;
return this;
}
/**
* Sets a custom codec for the property
*
* @param codec the custom codec for the property
* @return this
*/
public PropertyModelBuilder codec(final Codec codec) {
this.codec = codec;
return this;
}
/**
* @return the custom codec to use if set or null
*/
Codec getCodec() {
return codec;
}
/**
* Sets the {@link PropertySerialization} checker
*
* @param propertySerialization checks if a property should be serialized
* @return this
*/
public PropertyModelBuilder propertySerialization(final PropertySerialization propertySerialization) {
this.propertySerialization = notNull("propertySerialization", propertySerialization);
return this;
}
/**
* @return the {@link PropertySerialization} checker
*/
public PropertySerialization getPropertySerialization() {
return propertySerialization;
}
/**
* Returns the read annotations, to be applied when serializing to BSON
*
* @return the read annotations
*/
public List getReadAnnotations() {
return readAnnotations;
}
/**
* Sets the read annotations, to be applied when serializing to BSON
*
* @param annotations the read annotations
* @return this
*/
public PropertyModelBuilder readAnnotations(final List annotations) {
this.readAnnotations = unmodifiableList(notNull("annotations", annotations));
return this;
}
/**
* Returns the write annotations, to be applied when deserializing from BSON
*
* @return the write annotations
*/
public List getWriteAnnotations() {
return writeAnnotations;
}
/**
* Sets the writeAnnotations, to be applied when deserializing from BSON
*
* @param writeAnnotations the writeAnnotations
* @return this
*/
public PropertyModelBuilder writeAnnotations(final List writeAnnotations) {
this.writeAnnotations = writeAnnotations;
return this;
}
/**
* Property is writable.
*
* @return true if can be deserialized from BSON
*/
public boolean isWritable() {
return writeName != null;
}
/**
* Property is readable.
*
* @return true if can be serialized to BSON
*/
public boolean isReadable() {
return readName != null;
}
/**
* @return true or false if a discriminator should be used when serializing or null if not set
*/
public Boolean isDiscriminatorEnabled() {
return discriminatorEnabled;
}
/**
* Enables or disables the use of a discriminator when serializing
*
* @param discriminatorEnabled the useDiscriminator value
* @return this
*/
public PropertyModelBuilder discriminatorEnabled(final boolean discriminatorEnabled) {
this.discriminatorEnabled = discriminatorEnabled;
return this;
}
/**
* Returns the {@link PropertyAccessor}
*
* @return the PropertyAccessor
*/
public PropertyAccessor getPropertyAccessor() {
return propertyAccessor;
}
/**
* Sets the {@link PropertyAccessor}
*
* @param propertyAccessor the PropertyAccessor
* @return this
*/
public PropertyModelBuilder propertyAccessor(final PropertyAccessor propertyAccessor) {
this.propertyAccessor = propertyAccessor;
return this;
}
/**
* Creates the {@link PropertyModel}.
*
* @return the PropertyModel
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public PropertyModel build() {
if (!isReadable() && !isWritable()) {
throw new IllegalStateException(format("Invalid PropertyModel '%s', neither readable or writable,", name));
}
return new PropertyModel(
stateNotNull("propertyName", name),
readName,
writeName,
stateNotNull("typeData", typeData),
codec,
stateNotNull("propertySerialization", propertySerialization),
discriminatorEnabled,
stateNotNull("propertyAccessor", propertyAccessor));
}
@Override
public String toString() {
return format("PropertyModelBuilder{propertyName=%s, typeData=%s}", name, typeData);
}
PropertyModelBuilder propertyName(final String propertyName) {
this.name = notNull("propertyName", propertyName);
return this;
}
TypeData getTypeData() {
return typeData;
}
PropertyModelBuilder typeData(final TypeData typeData) {
this.typeData = notNull("typeData", typeData);
return this;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy