Search in sources :

Example 16 with Nullable

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;
}
Also used : ExternalEntity(dev.morphia.annotations.experimental.ExternalEntity) Entity(dev.morphia.annotations.Entity) WriteConcern(com.mongodb.WriteConcern) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Nullable(com.mongodb.lang.Nullable)

Example 17 with Nullable

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);
}
Also used : List(java.util.List) Nullable(com.mongodb.lang.Nullable)

Example 18 with Nullable

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);
}
Also used : FALSE(java.lang.Boolean.FALSE) Binary(org.bson.types.Binary) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Date(java.util.Date) MappingException(dev.morphia.mapping.MappingException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) UUID(java.util.UUID) Instant(java.time.Instant) Function(java.util.function.Function) BigDecimal(java.math.BigDecimal) Map(java.util.Map) ObjectId(org.bson.types.ObjectId) Nullable(com.mongodb.lang.Nullable) URI(java.net.URI) Function(java.util.function.Function) HashMap(java.util.HashMap) Nullable(com.mongodb.lang.Nullable)

Example 19 with Nullable

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;
}
Also used : LifecycleDecoder(dev.morphia.mapping.codec.pojo.LifecycleDecoder) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) MorphiaCodec(dev.morphia.mapping.codec.pojo.MorphiaCodec) LifecycleEncoder(dev.morphia.mapping.codec.pojo.LifecycleEncoder) Nullable(com.mongodb.lang.Nullable)

Aggregations

Nullable (com.mongodb.lang.Nullable)19 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)4 BsonDocument (org.bson.BsonDocument)4 Mono (reactor.core.publisher.Mono)4 MongoClientException (com.mongodb.MongoClientException)3 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)3 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)3 BsonString (org.bson.BsonString)3 RawBsonDocument (org.bson.RawBsonDocument)3 MongoException (com.mongodb.MongoException)2 MongoInternalException (com.mongodb.MongoInternalException)2 MongoNamespace (com.mongodb.MongoNamespace)2 ReadPreference (com.mongodb.ReadPreference)2 RequestContext (com.mongodb.RequestContext)2 ClientSession (com.mongodb.reactivestreams.client.ClientSession)2 MongoOperationPublisher.sinkToCallback (com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.sinkToCallback)2 String.format (java.lang.String.format)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2