/*
 * #%L
 * fujion
 * %%
 * Copyright (C) 2018 Fujion Framework
 * %%
 * 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.
 *
 * #L%
 */
package org.fujion.ancillary;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.fujion.annotation.ComponentDefinition;
import org.fujion.common.AbstractRegistry;
import org.fujion.common.MiscUtil;
import org.fujion.common.RegistryMap.DuplicateAction;
import org.fujion.component.BaseComponent;

/**
 * Registry of component definitions indexed by their tag name and implementing class.
 */
public class ComponentRegistry extends AbstractRegistry<String, ComponentDefinition> {

    private static final ComponentRegistry instance = new ComponentRegistry();

    private final Map<Class<? extends BaseComponent>, ComponentDefinition> classToDefinition = new HashMap<>();

    /**
     * Returns the singleton instance of the component registry.
     *
     * @return Component registry instance.
     */
    public static ComponentRegistry getInstance() {
        return instance;
    }

    private ComponentRegistry() {
        super(DuplicateAction.ERROR);
    }

    /**
     * Adds a component definition to the registry.
     *
     * @param item Item to add.
     */
    @Override
    public void register(ComponentDefinition item) {
        super.register(item);
        classToDefinition.put(item.getComponentClass(), item);
    }

    @Override
    protected String getKey(ComponentDefinition item) {
        return item.getTag();
    }

    @SuppressWarnings("unlikely-arg-type")
    @Override
    public ComponentDefinition unregisterByKey(String key) {
        return classToDefinition.remove(super.unregisterByKey(key));
    }

    /**
     * Returns a component definition given a component class. If no direct association occurs for
     * the specified class, will check each ancestor class in turn.
     *
     * @param componentClass The component class.
     * @return The associated component definition, or null if none found.
     */
    public ComponentDefinition get(Class<? extends BaseComponent> componentClass) {
        ComponentDefinition def = null;
        Class<?> clazz = componentClass;

        do {
            def = classToDefinition.get(clazz);
        } while (def == null && BaseComponent.class.isAssignableFrom((clazz = clazz.getSuperclass())));

        return def;
    }
    
    /**
     * Returns a list of all component definitions whose tags match the specified pattern.
     *
     * @param pattern A glob pattern for matching tags.
     * @return A list of matching component definitions (never null).
     */
    public Collection<ComponentDefinition> getMatching(String pattern) {
        if ("*".equals(pattern)) {
            return Collections.unmodifiableCollection(map.values());
        }
        
        ComponentDefinition def = get(pattern);
        
        if (def != null) {
            return Collections.singleton(def);
        }
        
        List<ComponentDefinition> matches = new ArrayList<>();
        Pattern regex = MiscUtil.globToRegex(pattern);
        
        for (String tag : map.keySet()) {
            if (regex.matcher(tag).matches()) {
                matches.add(map.get(tag));
            }
        }
        
        return matches;
    }

}

@@ -1264,17 +1264,21 @@
 tion%3E %7B%0A
+    
 %0A
-
     priv
@@ -1344,25 +1344,29 @@
 Registry();%0A
+    
 %0A
-
     private 
@@ -1465,17 +1465,21 @@
 ap%3C%3E();%0A
+    
 %0A
-
     /**%0A
@@ -1681,25 +1681,29 @@
 ance;%0A    %7D%0A
+    
 %0A
-
     private 
@@ -1760,33 +1760,37 @@
 n.ERROR);%0A    %7D%0A
+    
 %0A
-
     /**%0A     * A
@@ -2042,25 +2042,29 @@
 tem);%0A    %7D%0A
+    
 %0A
-
     @Overrid
@@ -2152,24 +2152,98 @@
 Tag();%0A    %7D
+%0A    %0A    @Override%0A    public void clear() %7B%0A        super.clear();%0A    %7D
 %0A%0A    @Suppr
@@ -2421,25 +2421,29 @@
 ey));%0A    %7D%0A
+    
 %0A
-
     /**%0A    
@@ -2910,17 +2910,25 @@
 tClass;%0A
+        
 %0A
-
         
@@ -3084,17 +3084,25 @@
 s())));%0A
+        
 %0A
-
         
@@ -3111,37 +3111,33 @@
 turn def;%0A    %7D%0A
-    
 %0A
+
     /**%0A     * R
@@ -3543,33 +3543,25 @@
 ;%0A        %7D%0A
-        
 %0A
+
         Comp
@@ -3592,33 +3592,25 @@
 t(pattern);%0A
-        
 %0A
+
         if (
@@ -3677,33 +3677,25 @@
 ;%0A        %7D%0A
-        
 %0A
+
         List
@@ -3800,25 +3800,17 @@
 ttern);%0A
-        
 %0A
+
         
@@ -3958,25 +3958,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -3989,11 +3989,15 @@
 ;%0A    %7D%0A
+    
 %0A%7D%0A

Make ComponentRegistry.clear public.