package org.drools.lang.descr;

import org.drools.io.Resource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class CompositePackageDescr extends PackageDescr {

    private List<ImportDescr>               imports                = Collections.emptyList();
    private List<FunctionImportDescr>       functionImports        = Collections.emptyList();
    private List<AttributeDescr>            attributes             = Collections.emptyList();
    private List<GlobalDescr>               globals                = Collections.emptyList();
    private List<FunctionDescr>             functions              = Collections.emptyList();
    private List<RuleDescr>                 rules                  = Collections.emptyList();
    private List<TypeDeclarationDescr>      typeDeclarations       = Collections.emptyList();
    private Set<EntryPointDeclarationDescr> entryPointDeclarations = Collections.emptySet();
    private Set<WindowDeclarationDescr>     windowDeclarations     = Collections.emptySet();

    public CompositePackageDescr(Resource resource, PackageDescr packageDescr) {
        super(packageDescr.getNamespace(), packageDescr.getDocumentation());
        internalAdd(resource, packageDescr);
    }

    public void addPackageDescr(Resource resource, PackageDescr packageDescr) {
        if (!getNamespace().equals(packageDescr.getNamespace())) {
            throw new RuntimeException("Composing PackageDescr in different namespaces");
        }
        internalAdd(resource, packageDescr);
    }

    private void internalAdd(Resource resource, PackageDescr packageDescr) {
        List<ImportDescr> currentImports = getImports();
        for (ImportDescr descr : packageDescr.getImports()) {
            if (!currentImports.contains(descr)) {
                addImport(descr);
            }
        }

        List<FunctionImportDescr> currentFunctionImports = getFunctionImports();
        for (FunctionImportDescr descr : packageDescr.getFunctionImports()) {
            if (!currentFunctionImports.contains(descr)) {
                addFunctionImport(descr);
            }
        }

        List<AttributeDescr> currentAttributeDescrs = getAttributes();
        for (AttributeDescr descr : packageDescr.getAttributes()) {
            if (!currentAttributeDescrs.contains(descr)) {
                addAttribute(descr);
            }
        }

        List<GlobalDescr> currentGlobalDescrs = getGlobals();
        for (GlobalDescr descr : packageDescr.getGlobals()) {
            if (!currentGlobalDescrs.contains(descr)) {
                addGlobal(descr);
            }
        }

        List<FunctionDescr> currentFunctionDescrs = getFunctions();
        for (FunctionDescr descr : packageDescr.getFunctions()) {
            if (!currentFunctionDescrs.contains(descr)) {
                addFunction(descr);
            }
        }

        List<RuleDescr> ruleDescrs = getRules();
        for (RuleDescr descr : packageDescr.getRules()) {
            if (!ruleDescrs.contains(descr)) {
                addRule(descr);
                descr.setResource(resource);
            }
        }

        List<TypeDeclarationDescr> typeDeclarationDescrs = getTypeDeclarations();
        for (TypeDeclarationDescr descr : packageDescr.getTypeDeclarations()) {
            if (!typeDeclarationDescrs.contains(descr)) {
                addTypeDeclaration(descr);
                descr.setResource(resource);
            }
        }

        Set<EntryPointDeclarationDescr> entryPointDeclarationDescrs = getEntryPointDeclarations();
        for (EntryPointDeclarationDescr descr : packageDescr.getEntryPointDeclarations()) {
            if (!entryPointDeclarationDescrs.contains(descr)) {
                addEntryPointDeclaration(descr);
            }
        }

        Set<WindowDeclarationDescr> windowDeclarationDescrs = getWindowDeclarations();
        for (WindowDeclarationDescr descr : packageDescr.getWindowDeclarations()) {
            if (!windowDeclarationDescrs.contains(descr)) {
                addWindowDeclaration(descr);
            }
        }
    }
}

@@ -61,66 +61,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Collections;%0A
 impo
@@ -80,16 +80,16 @@
 l.List;%0A
+
 import j
@@ -171,846 +171,41 @@
    p
-rivate List%3CImportDescr%3E               imports                = Collections.emptyList();%0A    private List%3CFunctionImportDescr%3E       functionImports        = Collections.emptyList();%0A    private List%3CAttributeDescr%3E            attributes             = Collections.emptyList();%0A    private List%3CGlobalDescr%3E               globals                = Collections.emptyList();%0A    private List%3CFunctionDescr%3E             functions              = Collections.emptyList();%0A    private List%3CRuleDescr%3E                 rules                  = Collections.emptyList();%0A    private List%3CTypeDeclarationDescr%3E      typeDeclarations       = Collections.emptyList();%0A    private Set%3CEntryPointDeclarationDescr%3E entryPointDeclarations = Collections.emptySet();%0A    private Set%3CWindowDeclarationDescr%3E     windowDeclarations     = Collections.emptySet();
+ublic CompositePackageDescr() %7B %7D
 %0A%0A  

remove useless data structures