View Javadoc

1   /*
2    * Copyright (C) The MetaClass Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.metaclass.model;
9   
10  import java.io.Serializable;
11  import java.util.Properties;
12  import java.util.Set;
13  
14  /***
15   * Attributes are the mechanism via which metadata is represented.
16   * Each Attribute is made up of
17   * <ul>
18   *   <li>name: the name of the Attribute</li>
19   *   <li>value: a simple text value of the Attribute.
20   *              Is incompatible with parameters.</li>
21   *   <li>parameters: a set of strings specifying parameters
22   *                   for the Attribute. Is incompatible with
23   *                   also having a text value.</li>
24   * </ul>
25   *
26   * @author Peter Donald
27   * @author Doug Hagan
28   * @version $Revision: 1.12 $ $Date: 2003/12/08 23:30:10 $
29   */
30  public final class Attribute
31      implements Serializable
32  {
33      /***
34       * The constant for Empty Set of attributes.
35       */
36      public static final Attribute[] EMPTY_SET = new Attribute[ 0 ];
37  
38      /***
39       * The constant for Empty Set of keys.
40       */
41      private static final String[] EMPTY_NAMES_SET = new String[ 0 ];
42  
43      /***
44       * The name of the Attribute.
45       */
46      private final String m_name;
47  
48      /***
49       * The value of the Attribute.
50       */
51      private final String m_value;
52  
53      /***
54       * The arbitrary set of parameters associated with the Attribute.
55       */
56      private final Properties m_parameters;
57  
58      /***
59       * Create a Attribute with specified name.
60       *
61       * @param name the Attribute name
62       */
63      public Attribute( final String name )
64      {
65          this( name, null, null );
66      }
67  
68      /***
69       * Create a Attribute with specified name and parameters.
70       *
71       * @param name the Attribute name
72       * @param parameters the Attribute parameters
73       */
74      public Attribute( final String name,
75                        final Properties parameters )
76      {
77          this( name, null, parameters );
78      }
79  
80      /***
81       * Create a Attribute with specified name and value.
82       *
83       * @param name the Attribute name
84       * @param value the Attribute value
85       */
86      public Attribute( final String name,
87                        final String value )
88      {
89          this( name, value, null );
90      }
91  
92      /***
93       * Create a Attribute with specified name, value
94       * and parameters. Note that it is invalid for both
95       * the value and parameters to be invalid.
96       *
97       * @param name the Attribute name
98       * @param value the Attribute value
99       * @param parameters the Attribute parameters
100      */
101     private Attribute( final String name,
102                        final String value,
103                        final Properties parameters )
104     {
105         if( null == name )
106         {
107             throw new NullPointerException( "name" );
108         }
109 
110         m_name = name;
111         m_value = value;
112         m_parameters = parameters;
113     }
114 
115     /***
116      * Return the name of the Attribute.
117      *
118      * @return the name of the Attribute.
119      */
120     public String getName()
121     {
122         return m_name;
123     }
124 
125     /***
126      * Return the value of the Attribute.
127      * @return the value of the Attribute.
128      */
129     public String getValue()
130     {
131         return m_value;
132     }
133 
134     /***
135      * Return the number of parameters in attribute.
136      *
137      * @return the number of parameters in attribute.
138      */
139     public int getParameterCount()
140     {
141         if( null == m_parameters )
142         {
143             return 0;
144         }
145         else
146         {
147             return m_parameters.size();
148         }
149     }
150 
151     /***
152      * Return the parameter for specified key.
153      *
154      * @param key the parameters key
155      * @return the parameter for specified key.
156      */
157     public String getParameter( final String key )
158     {
159         if( null == m_parameters )
160         {
161             return null;
162         }
163         else
164         {
165             return m_parameters.getProperty( key );
166         }
167     }
168 
169     /***
170      * Return the parameter for specified key, or defaultValue if unspecified.
171      *
172      * @param key the parameters key
173      * @param defaultValue the default value if parameter unspecified
174      * @return the parameter for specified key, or defaultValue if unspecified.
175      */
176     public String getParameter( final String key,
177                                 final String defaultValue )
178     {
179         if( null == m_parameters )
180         {
181             return defaultValue;
182         }
183         else
184         {
185             return m_parameters.getProperty( key, defaultValue );
186         }
187     }
188 
189     /***
190      * Returns an array of parameter names available under this Attribute.
191      *
192      * @return an array of parameter names available under this Attribute.
193      */
194     public String[] getParameterNames()
195     {
196         if( null == m_parameters )
197         {
198             return EMPTY_NAMES_SET;
199         }
200         else
201         {
202             final Set set = m_parameters.keySet();
203             return (String[])set.toArray( EMPTY_NAMES_SET );
204         }
205     }
206 }