use of com.mongodb.lang.Nullable in project morphia by mongodb.
the class Mapper method getWriteConcern.
/**
* Gets the write concern for entity or returns the default write concern for this datastore
*
* @param clazz the class to use when looking up the WriteConcern
* @return the write concern for the type
* @morphia.internal
*/
@Nullable
public WriteConcern getWriteConcern(Class clazz) {
WriteConcern wc = null;
EntityModel entityModel = getEntityModel(clazz);
if (entityModel != null) {
final Entity entityAnn = entityModel.getEntityAnnotation();
if (entityAnn != null && !entityAnn.concern().isEmpty()) {
wc = WriteConcern.valueOf(entityAnn.concern());
}
}
return wc;
}
use of com.mongodb.lang.Nullable in project morphia by mongodb.
the class ArrayFieldAccessor method convert.
@Nullable
private Object convert(Object o, Class<?> type) {
if (o instanceof List) {
List list = (List) o;
final Object newArray = Array.newInstance(type.getComponentType(), list.size());
for (int i = 0; i < list.size(); i++) {
Object converted = convert(list.get(i), type.getComponentType());
try {
Array.set(newArray, i, converted);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(format("Can't set %s with a value type of %s", getField(), converted.getClass()));
}
}
return newArray;
}
return Conversions.convert(o, type);
}
use of com.mongodb.lang.Nullable in project morphia by mongodb.
the class Conversions method convert.
/**
* Attempts to convert a value to the given type
*
* @param value the value to convert
* @param target the target type
* @param <T> the target type
* @return the potentially converted value
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Nullable
public static <T> T convert(@Nullable Object value, Class<T> target) {
if (value == null) {
return (T) convertNull(target);
}
final Class<?> fromType = value.getClass();
if (fromType.equals(target)) {
return (T) value;
}
final Function function = CONVERSIONS.computeIfAbsent(fromType, (f) -> new HashMap<>()).get(target);
if (function == null) {
if (target.equals(String.class)) {
return (T) value.toString();
}
if (target.isEnum() && fromType.equals(String.class)) {
return (T) Enum.valueOf((Class<? extends Enum>) target, (String) value);
}
return (T) value;
}
return (T) function.apply(value);
}
use of com.mongodb.lang.Nullable in project morphia by mongodb.
the class MorphiaCodecProvider method get.
@Nullable
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Codec<T> get(Class<T> type, CodecRegistry registry) {
MorphiaCodec<T> codec = (MorphiaCodec<T>) codecs.get(type);
if (codec == null && (mapper.isMapped(type) || mapper.isMappable(type))) {
EntityModel model = mapper.getEntityModel(type);
codec = new MorphiaCodec<>(datastore, model, propertyCodecProviders, mapper.getDiscriminatorLookup(), registry);
if (model.hasLifecycle(PostPersist.class) || model.hasLifecycle(PrePersist.class) || mapper.hasInterceptors()) {
codec.setEncoder(new LifecycleEncoder(codec));
}
if (model.hasLifecycle(PreLoad.class) || model.hasLifecycle(PostLoad.class) || mapper.hasInterceptors()) {
codec.setDecoder(new LifecycleDecoder(codec));
}
codecs.put(type, codec);
}
return codec;
}
Aggregations