001package org.andromda.core.namespace;
002
003import java.util.Arrays;
004import java.util.Collection;
005import java.util.LinkedHashSet;
006import org.andromda.core.common.ClassUtils;
007
008/**
009 * Stores information about a namespace component.
010 *
011 * @author Chad Brandon
012 */
013public class Component
014{
015    /**
016     * The name of the component
017     */
018    private String name;
019
020    /**
021     * Gets the name of the component.
022     *
023     * @return the component name.
024     */
025    public String getName()
026    {
027        return name;
028    }
029
030    /**
031     * Sets the name of the component.
032     *
033     * @param name the component's name.
034     */
035    public void setName(final String name)
036    {
037        this.name = name;
038    }
039
040    /**
041     * The path to the compoment's descriptor.
042     */
043    private final Collection<String> paths = new LinkedHashSet<String>();
044
045    /**
046     * Gets the component's descriptor paths (these are the paths
047     * where the component's descriptor may be found).
048     *
049     * @return the path to the component's descriptor.
050     */
051    public String[] getPaths()
052    {
053        return paths.toArray(new String[paths.size()]);
054    }
055
056    /**
057     * Adds a path to the component's descriptor.
058     *
059     * @param path that path to the component's descriptor.
060     */
061    public void addPath(final String path)
062    {
063        this.paths.add(path);
064    }
065
066    /**
067     * Adds the given <code>paths</code> to the existing paths
068     * contained within this component.
069     *
070     * @param paths the paths to add.
071     */
072    final void addPaths(final String[] paths)
073    {
074        if (paths != null && paths.length > 0)
075        {
076            this.paths.addAll(Arrays.asList(paths));
077        }
078    }
079
080    /**
081     * Stores the interface name that defines this component.
082     */
083    private Class type;
084
085    /**
086     * Sets the type class  that defines this component.
087     *
088     * @param typeClass the name of the type.
089     */
090    public void setTypeClass(final String typeClass)
091    {
092        final Class type = ClassUtils.loadClass(typeClass);
093        if (!NamespaceComponent.class.isAssignableFrom(type))
094        {
095            throw new NamespaceComponentsException(
096                "namespace component '" + type + "' must implement --> '" + NamespaceComponent.class.getName() + '\'');
097        }
098        this.type = type;
099    }
100
101    /**
102     * Gets the class that defines this component.
103     *
104     * @return the  class that defines this component.
105     */
106    public Class getType()
107    {
108        return this.type;
109    }
110}