package org.jtheque.modules.utils;

/*
 * Copyright JTheque (Baptiste Wicht)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.jtheque.utils.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

/**
 * A cache for the resource of the modules.
 *
 * @author Baptiste Wicht
 */
public final class ModuleResourceCache {
    private static final Map<String, Map<Class<?>, Set<Object>>> CACHE = new HashMap<String, Map<Class<?>, Set<Object>>>(8);

    /**
     * Utility class, not instantiable.
     */
    private ModuleResourceCache() {
        throw new AssertionError();
    }

    /**
     * Add the resources to the cache.
     *
     * @param id           The id of the module.
     * @param resourceType The type of resource.
     * @param resource     The resource to add.
     * @param <T>          The type of resource.
     */
    public static <T> void addResource(String id, Class<T> resourceType, T resource) {
        if (StringUtils.isNotEmpty(id)) {
            if (!CACHE.containsKey(id)) {
                CACHE.put(id, new IdentityHashMap<Class<?>, Set<Object>>(8));
            }

            Map<Class<?>, Set<Object>> resources = CACHE.get(id);

            if (!resources.containsKey(resourceType)) {
                resources.put(resourceType, new HashSet<Object>(5));
            }

            resources.get(resourceType).add(resource);
        }
    }

    /**
     * Return all the resources of the given type for the given module.
     *
     * @param id           The id of the module.
     * @param resourceType The resource type.
     * @param <T>          The type of resource.
     *
     * @return A Set containing all the resources of the given type for the given module.
     */
    public static <T> Set<T> getResource(String id, Class<T> resourceType) {
        if (CACHE.containsKey(id) && CACHE.get(id).containsKey(resourceType)) {
            return (Set<T>) CACHE.get(id).get(resourceType);
        }

        return Collections.emptySet();
    }

    /**
     * Remove the module from the cache.
     *
     * @param id The id of the module to remove from the cache.
     */
    public static void removeModule(String id) {
        CACHE.remove(id);
    }

    /**
     * Remove all the resource of the given type for the given resource.
     *
     * @param id           The module id.
     * @param resourceType The resource type.
     * @param <T>          The type of resource.
     */
    public static <T> void removeResourceOfType(String id, Class<T> resourceType) {
        if (CACHE.containsKey(id)) {
            CACHE.get(id).remove(resourceType);
        }
    }
}
@@ -668,16 +668,45 @@
 Utils;%0A%0A
+import java.util.Collection;%0A
 import j
@@ -1233,32 +1233,298 @@
 Error();%0A    %7D%0A%0A
+    public static %3CT%3E void addAllResource(String id, Class%3CT%3E resourceType, Collection%3CT%3E resources) %7B%0A        if(StringUtils.isNotEmpty(id))%7B%0A            Set%3CT%3E resourceCache = check(id, resourceType);%0A%0A            resourceCache.addAll(resources);%0A        %7D%0A    %7D%0A%0A
     /**%0A     * A
@@ -1889,32 +1889,219 @@
 mpty(id)) %7B%0A    
+        Set%3CT%3E resourceCache = check(id, resourceType);%0A%0A            resourceCache.add(resource);%0A        %7D%0A    %7D%0A%0A    private static %3CT%3E Set%3CT%3E check(String id, Class%3CT%3E resourceType) %7B%0A
         if (!CAC
@@ -2130,28 +2130,24 @@
             
-   
- 
 CACHE.put(id
@@ -2200,35 +2200,27 @@
 ));%0A        
-    %7D%0A%0A    
+%7D%0A%0A
         Map%3C
@@ -2250,17 +2250,21 @@
 resource
-s
+Cache
  = CACHE
@@ -2278,28 +2278,24 @@
 );%0A%0A        
-  
-  
 if (!resourc
@@ -2295,17 +2295,21 @@
 resource
-s
+Cache
 .contain
@@ -2342,28 +2342,24 @@
         
-    
 resource
 s.put(re
@@ -2350,17 +2350,21 @@
 resource
-s
+Cache
 .put(res
@@ -2403,28 +2403,24 @@
 ));%0A        
-    
 %7D%0A%0A         
@@ -2410,35 +2410,47 @@
      %7D%0A%0A        
-   
+return (Set%3CT%3E)
  resources.get(r
@@ -2442,17 +2442,21 @@
 resource
-s
+Cache
 .get(res
@@ -2469,33 +2469,9 @@
 ype)
-.add(resource);%0A        %7D
+;
 %0A   

Add feature to add a collection of resources