org.bson.codecs.pojo.ClassModel 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 java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This model represents the metadata for a class and all its properties.
*
* @param The type of the class the ClassModel represents
* @since 3.5
*/
public final class ClassModel {
private final String name;
private final Class type;
private final boolean hasTypeParameters;
private final InstanceCreatorFactory instanceCreatorFactory;
private final boolean discriminatorEnabled;
private final String discriminatorKey;
private final String discriminator;
private final IdPropertyModelHolder> idPropertyModelHolder;
private final List> propertyModels;
private final Map propertyNameToTypeParameterMap;
ClassModel(final Class clazz, final Map propertyNameToTypeParameterMap,
final InstanceCreatorFactory instanceCreatorFactory, final Boolean discriminatorEnabled, final String discriminatorKey,
final String discriminator, final IdPropertyModelHolder> idPropertyModelHolder,
final List> propertyModels) {
this.name = clazz.getSimpleName();
this.type = clazz;
this.hasTypeParameters = clazz.getTypeParameters().length > 0;
this.propertyNameToTypeParameterMap = Collections.unmodifiableMap(
new HashMap(propertyNameToTypeParameterMap));
this.instanceCreatorFactory = instanceCreatorFactory;
this.discriminatorEnabled = discriminatorEnabled;
this.discriminatorKey = discriminatorKey;
this.discriminator = discriminator;
this.idPropertyModelHolder = idPropertyModelHolder;
this.propertyModels = Collections.unmodifiableList(new ArrayList>(propertyModels));
}
/**
* Creates a new Class Model builder instance using reflection.
*
* @param type the POJO class to reflect and configure the builder with.
* @param the type of the class
* @return a new Class Model builder instance using reflection on the {@code clazz}.
*/
public static ClassModelBuilder builder(final Class type) {
return new ClassModelBuilder(type);
}
/**
* @return a new InstanceCreator instance for the ClassModel
*/
InstanceCreator getInstanceCreator() {
return instanceCreatorFactory.create();
}
/**
* @return the backing class for the ClassModel
*/
public Class getType() {
return type;
}
/**
* @return true if the underlying type has type parameters.
*/
public boolean hasTypeParameters() {
return hasTypeParameters;
}
/**
* @return true if a discriminator should be used when storing the data.
*/
public boolean useDiscriminator() {
return discriminatorEnabled;
}
/**
* Gets the value for the discriminator.
*
* @return the discriminator value or null if not set
*/
public String getDiscriminatorKey() {
return discriminatorKey;
}
/**
* Returns the discriminator key.
*
* @return the discriminator key or null if not set
*/
public String getDiscriminator() {
return discriminator;
}
/**
* Gets a {@link PropertyModel} by the property name.
*
* @param propertyName the PropertyModel's property name
* @return the PropertyModel or null if the property is not found
*/
public PropertyModel> getPropertyModel(final String propertyName) {
for (PropertyModel> propertyModel : propertyModels) {
if (propertyModel.getName().equals(propertyName)) {
return propertyModel;
}
}
return null;
}
/**
* Returns all the properties on this model
*
* @return the list of properties
*/
public List> getPropertyModels() {
return propertyModels;
}
/**
* Returns the {@link PropertyModel} mapped as the id property for this ClassModel
*
* @return the PropertyModel for the id
*/
public PropertyModel> getIdPropertyModel() {
return idPropertyModelHolder != null ? idPropertyModelHolder.getPropertyModel() : null;
}
IdPropertyModelHolder> getIdPropertyModelHolder() {
return idPropertyModelHolder;
}
/**
* Returns the name of the class represented by this ClassModel
*
* @return the name
*/
public String getName() {
return name;
}
@Override
public String toString() {
return "ClassModel{"
+ "type=" + type
+ "}";
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ClassModel> that = (ClassModel>) o;
if (discriminatorEnabled != that.discriminatorEnabled) {
return false;
}
if (!getType().equals(that.getType())) {
return false;
}
if (!getInstanceCreatorFactory().equals(that.getInstanceCreatorFactory())) {
return false;
}
if (getDiscriminatorKey() != null ? !getDiscriminatorKey().equals(that.getDiscriminatorKey())
: that.getDiscriminatorKey() != null) {
return false;
}
if (getDiscriminator() != null ? !getDiscriminator().equals(that.getDiscriminator()) : that.getDiscriminator() != null) {
return false;
}
if (idPropertyModelHolder != null ? !idPropertyModelHolder.equals(that.idPropertyModelHolder)
: that.idPropertyModelHolder != null) {
return false;
}
if (!getPropertyModels().equals(that.getPropertyModels())) {
return false;
}
if (!getPropertyNameToTypeParameterMap().equals(that.getPropertyNameToTypeParameterMap())) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = getType().hashCode();
result = 31 * result + getInstanceCreatorFactory().hashCode();
result = 31 * result + (discriminatorEnabled ? 1 : 0);
result = 31 * result + (getDiscriminatorKey() != null ? getDiscriminatorKey().hashCode() : 0);
result = 31 * result + (getDiscriminator() != null ? getDiscriminator().hashCode() : 0);
result = 31 * result + (getIdPropertyModelHolder() != null ? getIdPropertyModelHolder().hashCode() : 0);
result = 31 * result + getPropertyModels().hashCode();
result = 31 * result + getPropertyNameToTypeParameterMap().hashCode();
return result;
}
InstanceCreatorFactory getInstanceCreatorFactory() {
return instanceCreatorFactory;
}
Map getPropertyNameToTypeParameterMap() {
return propertyNameToTypeParameterMap;
}
}