Android Open Source - ormada Entity Cache






From Project

Back to project page ormada.

License

The source code is released under:

Copyright (c) 2012 Jesse Rosalia Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sof...

If you think the Android project ormada listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.ormada.entity;
/*  w  ww.j  av  a  2 s .co  m*/
import java.util.HashMap;
import java.util.Map;

/**
 * This class is used to maintain a map of all results pulled within a single
 * get operation. This is both an optimization, to ensure we don't fetch the
 * same entity more than once, and a way of handling circular references (so we
 * don't continuously fetch the same objects over and over again).
 * 
 * @author Jesse Rosalia
 * 
 */
public class EntityCache {

    private Map<Class<?>, Map<Long, Object>> entityMap = new HashMap<Class<?>, Map<Long, Object>>();

    /**
     * Add an entity to the cache, identified by the class and id passed in.
     * 
     * @param clazz
     * @param id
     * @param object
     */
    public <T> void add(Class<T> clazz, long id, T object) {
        Map<Long, Object> entityForClassMap = entityMap.get(clazz);
        if (entityForClassMap == null) {
            entityForClassMap = new HashMap<Long, Object>();
            entityMap.put(clazz, entityForClassMap);
        }
        if (entityForClassMap.containsKey(id)
                && !entityForClassMap.get(id).equals(object)) {
            throw new RuntimeException(
                    "Attempting to add a duplicate object that does not equal" +
                    " the original object.  This should never happen.");
        }
        entityForClassMap.put(id, object);
    }

    /**
     * Test to see if the cache contains the object identified by cache and id.
     * 
     * @param clazz
     * @param id
     * @return
     */
    public boolean contains(Class<?> clazz, long id) {
        return entityMap.containsKey(clazz)
                && entityMap.get(clazz).containsKey(id);
    }

    /**
     * Get the object previously built, identified by the cache and id.
     * 
     * @param clazz
     * @param id
     * @return
     */
    public <T> T get(Class<T> clazz, long id) {
        if (contains(clazz, id)) {
            return (T) entityMap.get(clazz).get(id);
        } else {
            return null;
        }
    }
}




Java Source Code List

org.andrormeda.dialect.SQLiteCursor.java
org.andrormeda.dialect.SQLiteDialect.java
org.andrormeda.dialect.SQLiteValueSet.java
org.andrormeda.example.AppDataSource.java
org.andrormeda.example.ExampleActivity.java
org.andrormeda.example.model.Cat.java
org.andrormeda.example.model.Kitten.java
org.ormada.ORMDataSource.java
org.ormada.annotations.OneToMany.java
org.ormada.annotations.Owner.java
org.ormada.annotations.Reference.java
org.ormada.annotations.Text.java
org.ormada.annotations.Transient.java
org.ormada.dialect.AStandardSQLDialect.java
org.ormada.dialect.DefaultValueSet.java
org.ormada.dialect.Dialect.java
org.ormada.dialect.ForwardOnlyResultSetCursor.java
org.ormada.dialect.FullResultSetCursor.java
org.ormada.dialect.QueryCursor.java
org.ormada.dialect.ValueSet.java
org.ormada.entity.EntityBuilder.java
org.ormada.entity.EntityCache.java
org.ormada.entity.EntityMetaData.java
org.ormada.entity.Entity.java
org.ormada.exception.MixedCollectionException.java
org.ormada.exception.UnableToOpenException.java
org.ormada.exception.UnsavedReferenceException.java
org.ormada.hsql.dialect.HSQLDialect.java
org.ormada.hsql.example.AppDataSource.java
org.ormada.hsql.example.ExampleMain.java
org.ormada.hsql.example.model.Cat.java
org.ormada.hsql.example.model.Kitten.java
org.ormada.model.ORMeta.java
org.ormada.reflect.DefaultReflector.java
org.ormada.reflect.Reflector.java
org.ormada.util.Profiler.java