Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/cerberus/component/descriptor/ComponentInstance.java


1   /*
2    * ComponentInstance.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.cerberus.component.descriptor;
11  
12  import java.util.HashMap;
13  import java.util.Collection;
14  import java.util.Iterator;
15  
16  /**
17   * <p>Represents an instance of a {@link ComponentDefinition}.</p>
18   *
19   * @author  Trevor Milne
20   *
21   */
22  
23  public class ComponentInstance
24  {
25    /** The name of this component. */
26    protected String name;
27  
28    /** The component definition of the component instance. */
29    protected String definition;
30  
31    /** The messages for this component. */
32    protected HashMap messages;
33  
34    /** The attributes for this component. */
35    protected HashMap attributes;
36  
37  
38    /* Constructors. */
39  
40  
41    /**
42     * Constructs a <code>ComponentInstance</code>.
43     *
44     */
45  
46    public ComponentInstance()
47    {
48      name = null;
49      definition = null;
50  
51      messages = new HashMap();
52      attributes = new HashMap();
53    }
54  
55    /**
56     * Constructs a <code>ComponentInstance</code> as a copy of the one supplied.
57     *
58     * @param    setName            The name of this component.
59     *
60     */
61  
62    public ComponentInstance(ComponentInstance instance)
63    {
64      name = instance.getName();
65      definition = instance.getDefinition();
66  
67      messages = new HashMap();
68      attributes = new HashMap();
69  
70      // copy messages
71      Iterator messageIterator = instance.getMessages().iterator();
72  
73      while (messageIterator.hasNext())
74      {
75        ComponentMessage copyMessage = (ComponentMessage)messageIterator.next();
76        ComponentMessage newMessage = new ComponentMessage(copyMessage);
77  
78        addMessage(newMessage);
79      }
80  
81      // copy attributes
82      Iterator attributeIterator = instance.getAttributes().iterator();
83  
84      while (attributeIterator.hasNext())
85      {
86        ComponentAttribute copyAttribute = (ComponentAttribute)attributeIterator.next();
87        ComponentAttribute newAttribute = new ComponentAttribute(copyAttribute);
88  
89        addAttribute(newAttribute);
90      }
91    }
92  
93  
94    /* Accessors. */
95  
96  
97    /* Name. */
98  
99    /**
100    * Sets the name of this component.
101    *
102    * @param    setName            The name of this component.
103    *
104    */
105 
106   public void setName(String setName)
107   {
108     name = setName;
109   }
110 
111   /**
112    * Returns the name of this component.
113    *
114    * @return                  The name of this component.
115    *
116    */
117 
118   public String getName()
119   {
120     return name;
121   }
122 
123   /* Definition. */
124 
125   /**
126    * Sets the definition for this component.
127    *
128    * @param    setDefinition        The definition for this component.
129    *
130    */
131 
132   public void setDefinition(String setDefinition)
133   {
134     definition = setDefinition;
135   }
136 
137   /**
138    * Returns the definition for this component.
139    *
140    * @return                  The definition for this component.
141    *
142    */
143 
144   public String getDefinition()
145   {
146     return definition;
147   }
148 
149   /* Attributes. */
150 
151   /**
152    * Adds an attribute to this component.
153    *
154    * @param    attribute          An attribute for this component.
155    *
156    */
157 
158   public void addAttribute(ComponentAttribute attribute)
159   {
160     attributes.put(attribute.getName(), attribute);
161   }
162 
163   /**
164    * Updates the specified attribute. This allows the attribute to be renamed.
165    *
166    * @param    name            The name of the attribute to update.
167    * @param    attribute          A new attribute for this component.
168    *
169    */
170 
171   public void updateAttribute(String name, ComponentAttribute attribute)
172   {
173     // remove old attribute
174     attributes.remove(name);
175 
176     // add new attribute
177     addAttribute(attribute);
178   }
179 
180   /**
181    * Returns the specified attribute.
182    *
183    * @param    name            The name of the attribute.
184    *
185    * @return                  The {@link ComponentAttribute} having the supplied name.
186    *
187    */
188 
189   public ComponentAttribute getAttribute(String name)
190   {
191     return (ComponentAttribute)attributes.get(name);
192   }
193 
194   /**
195    * Returns the attributes of this component.
196    *
197    * @return                  A <code>Collection</code> of {@link ComponentAttribute ComponentAttributes}.
198    *
199    */
200 
201   public Collection getAttributes()
202   {
203     return attributes.values();
204   }
205 
206   /* Messages. */
207 
208   /**
209    * Adds a message to this component.
210    *
211    * @param    message            A message for this component.
212    *
213    */
214 
215   public void addMessage(ComponentMessage message)
216   {
217     messages.put(message.getName(), message);
218   }
219 
220   /**
221    * Updates the specified message. This allows the message to be renamed.
222    *
223    * @param    name            The name of the message to update.
224    * @param    message            A new message for this component.
225    *
226    */
227 
228   public void updateMessage(String name, ComponentMessage message)
229   {
230     // remove old message
231     messages.remove(name);
232 
233     // add new message
234     addMessage(message);
235   }
236 
237   /**
238    * Returns the specified message.
239    *
240    * @param    name            The name of the message.
241    *
242    * @return                  The {@link ComponentMessage} having the supplied name.
243    *
244    */
245 
246   public ComponentMessage getMessage(String name)
247   {
248     return (ComponentMessage)messages.get(name);
249   }
250 
251   /**
252    * Returns the messages of this component.
253    *
254    * @return                  A <code>Collection</code> of {@link ComponentMessage ComponentMessages}.
255    *
256    */
257 
258   public Collection getMessages()
259   {
260     return messages.values();
261   }
262 
263 
264   /* Debug. */
265 
266 
267   public String toString()
268   {
269     StringBuffer buffer = new StringBuffer();
270 
271     buffer.append("ComponentInstance=[");
272 
273     buffer.append("name=");
274     buffer.append(name);
275 
276     buffer.append("; definition=");
277     buffer.append(definition);
278 
279     buffer.append("; attributes=[");
280 
281     Iterator attributeIterator = attributes.values().iterator();
282 
283     while (attributeIterator.hasNext())
284     {
285       ComponentAttribute attribute = (ComponentAttribute)attributeIterator.next();
286 
287       buffer.append(attribute.getName());
288       buffer.append("=");
289       buffer.append(attribute.getValue());
290 
291       if (attributeIterator.hasNext())
292       {
293         buffer.append("; ");
294       }
295     }
296 
297     buffer.append("]; messages=[");
298 
299     Iterator messageIterator = messages.values().iterator();
300 
301     while (messageIterator.hasNext())
302     {
303       ComponentMessage message = (ComponentMessage)messageIterator.next();
304 
305       buffer.append(message.getName());
306       buffer.append("=");
307       buffer.append(message.getTopic());
308 
309       if (messageIterator.hasNext())
310       {
311         buffer.append("; ");
312       }
313     }
314 
315     buffer.append("]]");
316 
317     return buffer.toString();
318   }
319 }
320