Search in sources :

Example 41 with Collection

use of java.util.Collection in project head by mifos.

the class BranchReportLoanArrearsAgingHelperIntegrationTest method assertLoanArrearsAgingPopulated.

private void assertLoanArrearsAgingPopulated(Set<BranchReportLoanArrearsAgingBO> loanArrearsAgingSummaries) {
    Assert.assertNotNull(loanArrearsAgingSummaries);
    Collection foundPeriods = CollectionUtils.collect(loanArrearsAgingSummaries, new Transformer() {

        @Override
        public Object transform(Object input) {
            return ((BranchReportLoanArrearsAgingBO) input).getAgingPeriod();
        }
    });
    assertSameCollections(expectedPeriods, foundPeriods);
}
Also used : Transformer(org.apache.commons.collections.Transformer) Collection(java.util.Collection)

Example 42 with Collection

use of java.util.Collection in project morphia by mongodb.

the class Mapper method fromDb.

/**
     * Converts a DBObject back to a type-safe java object (POJO)
     *
     * @param <T>       the type of the entity
     * @param datastore the Datastore to use when fetching this reference
     * @param dbObject  the DBObject containing the document from mongodb
     * @param entity    the instance to populate
     * @param cache     the EntityCache to use
     * @return the entity
     */
public <T> T fromDb(final Datastore datastore, final DBObject dbObject, final T entity, final EntityCache cache) {
    //hack to bypass things and just read the value.
    if (entity instanceof MappedField) {
        readMappedField(datastore, (MappedField) entity, entity, cache, dbObject);
        return entity;
    }
    if (dbObject.containsField(ID_KEY) && getMappedClass(entity).getIdField() != null && getMappedClass(entity).getEntityAnnotation() != null) {
        final Key<T> key = new Key(entity.getClass(), getCollectionName(entity.getClass()), dbObject.get(ID_KEY));
        final T cachedInstance = cache.getEntity(key);
        if (cachedInstance != null) {
            return cachedInstance;
        } else {
            // to avoid stackOverflow in recursive refs
            cache.putEntity(key, entity);
        }
    }
    if (entity instanceof Map) {
        Map<String, Object> map = (Map<String, Object>) entity;
        for (String key : dbObject.keySet()) {
            Object o = dbObject.get(key);
            map.put(key, (o instanceof DBObject) ? fromDBObject(datastore, (DBObject) o) : o);
        }
    } else if (entity instanceof Collection) {
        Collection<Object> collection = (Collection<Object>) entity;
        for (Object o : ((List) dbObject)) {
            collection.add((o instanceof DBObject) ? fromDBObject(datastore, (DBObject) o) : o);
        }
    } else {
        final MappedClass mc = getMappedClass(entity);
        final DBObject updated = mc.callLifecycleMethods(PreLoad.class, entity, dbObject, this);
        try {
            for (final MappedField mf : mc.getPersistenceFields()) {
                readMappedField(datastore, mf, entity, cache, updated);
            }
        } catch (final MappingException e) {
            Object id = dbObject.get(ID_KEY);
            String entityName = entity.getClass().getName();
            throw new MappingException(format("Could not map %s with ID: %s in database '%s'", entityName, id, datastore.getDB().getName()), e);
        }
        if (updated.containsField(ID_KEY) && getMappedClass(entity).getIdField() != null) {
            final Key key = new Key(entity.getClass(), getCollectionName(entity.getClass()), updated.get(ID_KEY));
            cache.putEntity(key, entity);
        }
        mc.callLifecycleMethods(PostLoad.class, entity, updated, this);
    }
    return entity;
}
Also used : PreLoad(org.mongodb.morphia.annotations.PreLoad) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) PostLoad(org.mongodb.morphia.annotations.PostLoad) Collection(java.util.Collection) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Key(org.mongodb.morphia.Key)

Example 43 with Collection

use of java.util.Collection in project morphia by mongodb.

the class ReferenceMapper method readCollection.

private void readCollection(final Datastore datastore, final Mapper mapper, final DBObject dbObject, final MappedField mf, final Object entity, final Reference refAnn, final EntityCache cache) {
    // multiple references in a List
    final Class referenceObjClass = mf.getSubClass();
    // load reference class.  this "fixes" #816
    mapper.getMappedClass(referenceObjClass);
    Collection references = mf.isSet() ? mapper.getOptions().getObjectFactory().createSet(mf) : mapper.getOptions().getObjectFactory().createList(mf);
    if (refAnn.lazy() && LazyFeatureDependencies.assertDependencyFullFilled()) {
        final Object dbVal = mf.getDbObjectValue(dbObject);
        if (dbVal != null) {
            references = mapper.getProxyFactory().createListProxy(datastore, references, referenceObjClass, refAnn.ignoreMissing());
            final ProxiedEntityReferenceList referencesAsProxy = (ProxiedEntityReferenceList) references;
            if (dbVal instanceof List) {
                referencesAsProxy.__addAll(refAnn.idOnly() ? mapper.getKeysByManualRefs(referenceObjClass, (List) dbVal) : mapper.getKeysByRefs((List) dbVal));
            } else {
                referencesAsProxy.__add(refAnn.idOnly() ? mapper.manualRefToKey(referenceObjClass, dbVal) : mapper.refToKey((DBRef) dbVal));
            }
        }
    } else {
        final Object dbVal = mf.getDbObjectValue(dbObject);
        final Collection refs = references;
        new IterHelper<String, Object>().loopOrSingle(dbVal, new IterCallback<Object>() {

            @Override
            public void eval(final Object val) {
                final Object ent = resolveObject(datastore, mapper, cache, mf, refAnn.idOnly(), val);
                if (ent == null) {
                    LOG.warning("Null reference found when retrieving value for " + mf.getFullName());
                } else {
                    refs.add(ent);
                }
            }
        });
    }
    if (mf.getType().isArray()) {
        mf.setFieldValue(entity, ReflectionUtils.convertToArray(mf.getSubClass(), ReflectionUtils.iterToList(references)));
    } else {
        mf.setFieldValue(entity, references);
    }
}
Also used : Collection(java.util.Collection) DBCollection(com.mongodb.DBCollection) DBObject(com.mongodb.DBObject) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) ArrayList(java.util.ArrayList) ProxiedEntityReferenceList(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReferenceList) List(java.util.List)

Example 44 with Collection

use of java.util.Collection in project feign by OpenFeign.

the class JacksonJaxbCodecTest method notFoundDecodesToEmpty.

/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundDecodesToEmpty() throws Exception {
    Response response = Response.builder().status(404).reason("NOT FOUND").headers(Collections.<String, Collection<String>>emptyMap()).build();
    assertThat((byte[]) new JacksonJaxbJsonDecoder().decode(response, byte[].class)).isEmpty();
}
Also used : Response(feign.Response) Collection(java.util.Collection) Test(org.junit.Test)

Example 45 with Collection

use of java.util.Collection in project feign by OpenFeign.

the class JacksonCodecTest method notFoundDecodesToEmpty.

/** Enabled via {@link feign.Feign.Builder#decode404()} */
@Test
public void notFoundDecodesToEmpty() throws Exception {
    Response response = Response.builder().status(404).reason("NOT FOUND").headers(Collections.<String, Collection<String>>emptyMap()).build();
    assertThat((byte[]) new JacksonDecoder().decode(response, byte[].class)).isEmpty();
}
Also used : Response(feign.Response) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

Collection (java.util.Collection)8532 ArrayList (java.util.ArrayList)2646 Map (java.util.Map)2119 List (java.util.List)1956 HashMap (java.util.HashMap)1657 Test (org.junit.Test)1476 Set (java.util.Set)1137 Iterator (java.util.Iterator)1090 HashSet (java.util.HashSet)1085 Collectors (java.util.stream.Collectors)951 IOException (java.io.IOException)823 Collections (java.util.Collections)660 Arrays (java.util.Arrays)446 Optional (java.util.Optional)423 File (java.io.File)386 Logger (org.slf4j.Logger)344 LoggerFactory (org.slf4j.LoggerFactory)330 PersistenceManager (javax.jdo.PersistenceManager)309 LinkedHashMap (java.util.LinkedHashMap)299 Query (javax.jdo.Query)294