/*
 * Copyright 2016 The original authors.
 *
 *    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.
 */

package io.sundr.codegen;

import io.sundr.codegen.model.ClassRef;
import io.sundr.codegen.model.TypeDef;
import io.sundr.codegen.model.TypeRef;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class DefinitionRepository {

    private static DefinitionRepository INSTANCE;

    private final Map<String, TypeDef> definitions = new HashMap<String, TypeDef>();

    private DefinitionRepository() {
    }

    public static synchronized final DefinitionRepository getRepository() {
        if (INSTANCE == null) {
            INSTANCE = new DefinitionRepository();
        }
        return INSTANCE;
    }

    public TypeDef register(TypeDef definition) {
        if (definition != null) {
            definitions.put(definition.getFullyQualifiedName(), definition);
        }
        return definition;
    }

    public Set<TypeDef> getDefinitions() {
        return Collections.unmodifiableSet(new LinkedHashSet<TypeDef>(definitions.values()));
    }

    public TypeDef getDefinition(String fullyQualifiedName) {
       return definitions.get(fullyQualifiedName);
    }

    public TypeDef getDefinition(TypeRef type) {
        if (type instanceof ClassRef) {
            return definitions.get(((ClassRef)type).getDefinition().getFullyQualifiedName());
        }
        return null;
    }

    public void clear() {
        definitions.clear();
    }
}

@@ -728,24 +728,70 @@
 el.TypeDef;%0A
+import io.sundr.codegen.model.TypeDefBuilder;%0A
 import io.su
@@ -817,16 +817,16 @@
 ypeRef;%0A
-
 %0Aimport 
@@ -1567,32 +1567,33 @@
 inition;%0A    %7D%0A%0A
+%0A
     public Set%3CT
@@ -1591,131 +1591,879 @@
 lic 
-Set%3C
 TypeDef
-%3E getDefinitions() %7B%0A        return Collections.unmodifiableSet(new LinkedHashSet%3CTypeDef%3E(definitions.values())
+ register(TypeDef definition, String... flags) %7B%0A        TypeDefBuilder builder = new TypeDefBuilder(definition);%0A        for (String flag : flags) %7B%0A            builder.addToAttributes(flag, true);%0A        %7D%0A        return register(builder.build());%0A    %7D%0A%0A%0A    public Set%3CTypeDef%3E getDefinitions(String... flags) %7B%0A%0A        Set%3CTypeDef%3E result = new LinkedHashSet%3CTypeDef%3E();%0A        for (TypeDef candidate : definitions.values()) %7B%0A            boolean matches = true;%0A            for (String flag :flags) %7B%0A                if (!candidate.getAttributes().containsKey(flag) %7C%7C !((Boolean) candidate.getAttributes().get(flag))) %7B%0A                   matches = false;%0A                   break;%0A                %7D%0A            %7D%0A            if (matches) %7B%0A                result.add(candidate);%0A            %7D%0A        %7D%0A%0A        return Collections.unmodifiableSet(result
 );%0A 

Allow setting and querying using flags (boolean attributes) in the DefinitionRepository.java