001package org.andromda.core.cartridge.template;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.LinkedHashSet;
006import java.util.Set;
007import org.andromda.core.common.ExceptionUtils;
008import org.andromda.core.metafacade.MetafacadeBase;
009import org.apache.commons.lang.StringUtils;
010
011/**
012 * Defines the <modelElements/> element within a <template/> within an XML cartridge descriptor. This allows
013 * the grouping of model elements by criteria defined within the nested {@link ModelElement}instances.
014 *
015 * @author Chad Brandon
016 * @author Michail Plushnikov
017 * @see Template
018 * @see ModelElement
019 */
020public class ModelElements
021{
022    private String variable;
023    private final Collection<ModelElement> modelElements = new ArrayList<ModelElement>();
024
025    /**
026     * The variable name to make the model element available to the template engine. For example if you have the
027     * modelElement &lt;&lt;entity&gt;&gt; defined within your &lt;&lt;modelElements&gt;&gt; element you may want to
028     * define this value as <code>entity</code>. If on the other hand the outputToSingleFile flag is set to true you'd
029     * probably want to make it available as <code>entities</code>.
030     *
031     * @return Returns the variable.
032     */
033    public String getVariable()
034    {
035        return variable;
036    }
037
038    /**
039     * @param variable The variable to set.
040     */
041    public void setVariable(String variable)
042    {
043        variable = StringUtils.trimToEmpty(variable);
044        ExceptionUtils.checkEmpty("variable", variable);
045        this.variable = variable;
046    }
047
048    /**
049     * Adds a modelElement to the collection of <code>modelElements</code>.
050     *
051     * @param modelElement the new ModelElement to add.
052     */
053    public void addModelElement(final ModelElement modelElement)
054    {
055        ExceptionUtils.checkNull("modelElement", modelElement);
056        modelElements.add(modelElement);
057    }
058
059    /**
060     * Gets all metafacade instances from each ModelElement belonging to this ModelElements instance.
061     *
062     * @return Collection of all metafacades.
063     */
064    public Set<MetafacadeBase> getAllMetafacades()
065    {
066        final Set<MetafacadeBase> allMetafacades = new LinkedHashSet<MetafacadeBase>();
067        for (ModelElement modelElement : modelElements)
068        {
069            allMetafacades.addAll(modelElement.getMetafacades());
070        }
071        return allMetafacades;
072    }
073
074    /**
075     * Returns all model elements belonging to this model elements instance.
076     *
077     * @return Collection of all {@link ModelElement}instances.
078     */
079    public Collection<ModelElement> getModelElements()
080    {
081        return this.modelElements;
082    }
083
084    /**
085     * Returns true if this instance has no <code>modelElements</code> stored within it.
086     *
087     * @return true/false
088     */
089    public boolean isEmpty()
090    {
091        return this.modelElements.isEmpty();
092    }
093}