001package org.andromda.core.configuration;
002
003import java.io.Serializable;
004import java.util.ArrayList;
005import java.util.Collection;
006import java.util.LinkedHashMap;
007import java.util.Map;
008
009/**
010 * A configurable namespace object. These are passed to Plugin instances (Cartridges, etc.).
011 *
012 * @author Chad Brandon
013 */
014public class Namespace
015    implements Serializable
016{
017    private static final long serialVersionUID = 34L;
018
019    /**
020     * The namespace name.
021     */
022    private String name;
023
024    /**
025     * Returns name of this Namespace. Will correspond to a Plugin name (or it can be be 'default' if we want it's
026     * settings to be used everywhere).
027     *
028     * @return String
029     */
030    public String getName()
031    {
032        return this.name;
033    }
034
035    /**
036     * Sets the name of this Namespace.
037     *
038     * @param name The name to set
039     */
040    public void setName(final String name)
041    {
042        this.name = name;
043    }
044
045    /**
046     * Stores the collected properties
047     */
048    private final Map<String, Collection<Property>> properties = new LinkedHashMap<String, Collection<Property>>();
049
050    /**
051     * Adds a property to this Namespace object. A property must correspond to a java bean property name on a Plugin in
052     * order for it to be set during processing. Otherwise the property will just be ignored.
053     *
054     * @param property the property to add to this namespace.
055     */
056    public void addProperty(final Property property)
057    {
058        if (property != null)
059        {
060            Collection<Property> properties = this.properties.get(property.getName());
061            if (properties == null)
062            {
063                properties = new ArrayList<Property>();
064                this.properties.put(
065                    property.getName(),
066                    properties);
067            }
068            properties.add(property);
069        }
070    }
071
072    /**
073     * Retrieves the properties with the specified name.
074     *
075     * @param name the name of the property.
076     *
077     * @return the property
078     */
079    public Collection<Property> getProperties(final String name)
080    {
081        return this.properties.get(name);
082    }
083
084    /**
085     * Retrieves the property (the first one found) with the specified name.
086     *
087     * @param name the name of the property.
088     *
089     * @return the property
090     */
091    public Property getProperty(final String name)
092    {
093        final Collection<Property> properties = this.getProperties(name);
094        return properties == null || properties.isEmpty() ?
095            null : properties.iterator().next();
096    }
097
098    /**
099     * Gets all namespaces belonging to this namespaces instance.
100     *
101     * @return all namespaces.
102     */
103    public Collection<Collection<Property>> getProperties()
104    {
105        return this.properties.values();
106    }
107
108    /**
109     * @see Object#toString()
110     */
111    public String toString()
112    {
113        return super.toString() + '[' + this.name + ']';
114    }
115}