View Javadoc

1   /**
2    * Project MI+
3    *
4    * Copyright (c) 2013, Syed Muhammad Humayun - smhumayun@gmail.com
5    *
6    * This project includes software developed by Syed Muhammad Humayun - smhumayun@gmail.com
7    * http://www.smhumayun.com
8    *
9    * Licensed under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License. You may obtain a copy of the License at:
12   *
13   * http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on
17   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied. See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  package com.smhumayun.mi_plus.impl;
23  
24  import com.smhumayun.mi_plus.*;
25  
26  import java.util.HashMap;
27  import java.util.HashSet;
28  import java.util.LinkedHashMap;
29  
30  /**
31   * HashMap based In-Memory cache implementation of {@link com.smhumayun.mi_plus.MIObjectCache}
32   *
33   * @author smhumayun
34   * @since 1.0
35   */
36  public final class MIObjectInMemoryCache implements MIObjectCache {
37  
38      /**
39       * Internal cache to hold singleton MI Container Proxy objects against an MI Container class
40       */
41      private final HashMap<Class, Object> containerProxySingletonCache = new HashMap<Class, Object>();
42  
43      /**
44       * Internal cache to hold singleton Composed objects against an MI Container class
45       */
46      private final HashMap<Class, LinkedHashMap<Class, Object>> composedSingletonCache = new HashMap<Class, LinkedHashMap<Class, Object>>();
47  
48      /**
49       * Internal cache to hold validated classes
50       */
51      private final HashSet<Class> validatedClassesCache = new HashSet<Class>();
52  
53      /**
54       * Get cached MI Container Proxy object against given <code>miContainerClass</code>
55       *
56       * @param miContainerClass MI Container class
57       * @return cached MI Container Proxy object against given <code>miContainerClass</code>
58       * @throws com.smhumayun.mi_plus.MIException
59       *          {@link com.smhumayun.mi_plus.MIException}
60       */
61      public Object getCachedMiContainerProxy(Class miContainerClass) throws MIException {
62          return containerProxySingletonCache.get(miContainerClass);
63      }
64  
65      /**
66       * Cache given <code>miContainerProxy</code> object against given <code>miContainerClass</code>
67       *
68       * @param miContainerClass MI Container class
69       * @param miContainerProxyObj      MI Container Proxy object to cache
70       * @throws com.smhumayun.mi_plus.MIException
71       *          {@link com.smhumayun.mi_plus.MIException}
72       */
73      public void cacheMiContainerProxy(Class miContainerClass, Object miContainerProxyObj) throws MIException {
74          containerProxySingletonCache.put(miContainerClass, miContainerProxyObj);
75      }
76  
77      /**
78       * Get cached Composed Objects against given <code>miContainerClass</code> - access underlying cache in a
79       * synchronized, multi thread-safe way
80       *
81       * @param miContainerClass MI Container class
82       * @return cached Composed Objects against given <code>miContainerClass</code>
83       * @throws com.smhumayun.mi_plus.MIException
84       *          {@link com.smhumayun.mi_plus.MIException}
85       */
86      public LinkedHashMap<Class, Object> getCachedComposedObjects(Class miContainerClass) throws MIException {
87          return composedSingletonCache.get(miContainerClass);
88      }
89  
90      /**
91       * Cache given <code>composedObjects</code> against given <code>miContainerClass</code> - access underlying cache
92       * in a synchronized, multi thread-safe way
93       *
94       * @param miContainerClass MI Container class
95       * @param composedObjects  composed objects to cache
96       * @throws com.smhumayun.mi_plus.MIException
97       *          {@link com.smhumayun.mi_plus.MIException}
98       */
99      public void cacheComposedObjects(Class miContainerClass, LinkedHashMap<Class, Object> composedObjects) throws MIException {
100         composedSingletonCache.put(miContainerClass, composedObjects);
101     }
102 
103     /**
104      * Returns true of the <code>classToValidate</code> has already been validated
105      *
106      * @param classToValidate class to
107      * @return true if validates, false otherwise
108      * @throws com.smhumayun.mi_plus.MIException
109      *          {@link com.smhumayun.mi_plus.MIException}
110      */
111     @Override
112     public boolean isValidated(Class classToValidate) throws MIException {
113         return validatedClassesCache.contains(classToValidate);
114     }
115 
116     /**
117      * Cache this <code>validatedClass</code> and mark it as validated
118      *
119      * @param validatedClass validated class to cache
120      * @throws com.smhumayun.mi_plus.MIException
121      *          {@link com.smhumayun.mi_plus.MIException}
122      */
123     @Override
124     public void validated(Class validatedClass) throws MIException {
125         validatedClassesCache.add(validatedClass);
126     }
127 
128 }