package org.bson.codecs.pojo;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public final class ClassModel<T> {
private final String name;
private final Class<T> type;
private final boolean hasTypeParameters;
private final InstanceCreatorFactory<T> instanceCreatorFactory;
private final boolean discriminatorEnabled;
private final String discriminatorKey;
private final String discriminator;
private final IdPropertyModelHolder<?> idPropertyModelHolder;
private final List<PropertyModel<?>> propertyModels;
private final Map<String, TypeParameterMap> propertyNameToTypeParameterMap;
ClassModel(final Class<T> clazz, final Map<String, TypeParameterMap> propertyNameToTypeParameterMap,
final InstanceCreatorFactory<T> instanceCreatorFactory, final Boolean discriminatorEnabled, final String discriminatorKey,
final String discriminator, final IdPropertyModelHolder<?> idPropertyModelHolder,
final List<PropertyModel<?>> propertyModels) {
this.name = clazz.getSimpleName();
this.type = clazz;
this.hasTypeParameters = clazz.getTypeParameters().length > 0;
this.propertyNameToTypeParameterMap = Collections.unmodifiableMap(propertyNameToTypeParameterMap);
this.instanceCreatorFactory = instanceCreatorFactory;
this.discriminatorEnabled = discriminatorEnabled;
this.discriminatorKey = discriminatorKey;
this.discriminator = discriminator;
this.idPropertyModelHolder = idPropertyModelHolder;
this.propertyModels = Collections.unmodifiableList(propertyModels);
}
public static <S> ClassModelBuilder<S> builder(final Class<S> type) {
return new ClassModelBuilder<S>(type);
}
InstanceCreator<T> getInstanceCreator() {
return instanceCreatorFactory.create();
}
public Class<T> getType() {
return type;
}
public boolean hasTypeParameters() {
return hasTypeParameters;
}
public boolean useDiscriminator() {
return discriminatorEnabled;
}
public String getDiscriminatorKey() {
return discriminatorKey;
}
public String getDiscriminator() {
return discriminator;
}
public PropertyModel<?> getPropertyModel(final String propertyName) {
for (PropertyModel<?> propertyModel : propertyModels) {
if (propertyModel.getName().equals(propertyName)) {
return propertyModel;
}
}
return null;
}
public List<PropertyModel<?>> getPropertyModels() {
return propertyModels;
}
public PropertyModel<?> getIdPropertyModel() {
return idPropertyModelHolder != null ? idPropertyModelHolder.getPropertyModel() : null;
}
IdPropertyModelHolder<?> getIdPropertyModelHolder() {
return idPropertyModelHolder;
}
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<T> getInstanceCreatorFactory() {
return instanceCreatorFactory;
}
Map<String, TypeParameterMap> getPropertyNameToTypeParameterMap() {
return propertyNameToTypeParameterMap;
}
}