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/ComponentDescriptor.java


1   /*
2    * ComponentDescriptor.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.Collection;
13  import java.util.HashMap;
14  import java.util.Iterator;
15  import java.util.Map;
16  
17  /**
18   * <p>Describes the attributes and messages of an assembled component.</p>
19   *
20   * @author  Trevor Milne
21   *
22   */
23  
24  public class ComponentDescriptor
25  {
26    /** The name of this component. */
27    protected String name;
28  
29    /** The definition component. */
30    protected String definition;
31  
32    /** The backing class of this component. */
33    protected String backingClass;
34  
35    /** Contains the attributes of this component. */
36    protected Map attributes;
37  
38    /** Contains the message of this component. */
39    protected Map messages;
40  
41  
42    /* Constructors. */
43  
44  
45    /**
46     * Constructs a <code>ComponentDescriptor</code> instance.
47     *
48     */
49  
50    public ComponentDescriptor()
51    {
52      name = "";
53      backingClass = "";
54  
55      attributes = new HashMap();
56      messages = new HashMap();
57    }
58  
59  
60    /**
61     * Constructs a <code>ComponentDescriptor</code> instance as a copy of the one supplied.
62     *
63     * @param    descriptor          The {@link ComponentDescriptor} to copy.
64     *
65     */
66  
67    public ComponentDescriptor(ComponentDescriptor descriptor)
68    {
69      name = descriptor.getName();
70      backingClass = descriptor.getBackingClass();
71  
72      attributes = new HashMap(descriptor.attributes);
73      messages = new HashMap(descriptor.messages);
74    }
75  
76  
77    /* Accessors. */
78  
79  
80    /* Name. */
81  
82    /**
83     * Returns the name of this component.
84     *
85     * @return                  The name of this component.
86     *
87     */
88  
89    public String getName()
90    {
91      return name;
92    }
93  
94    /**
95     * Sets the name of this component.
96     *
97     * @param    setName            The name of this component.
98     *
99     */
100 
101   public void setName(String setName)
102   {
103     name = setName;
104   }
105 
106   /* Class. */
107 
108   /**
109    * Sets the class of this component.
110    *
111    * @param    setClass          The component's class.
112    *
113    */
114 
115   public void setBackingClass(String setClass)
116   {
117     backingClass = setClass;
118   }
119 
120   /**
121    * Returns the class of this component.
122    *
123    * @return                  The class of this component.
124    *
125    */
126 
127   public String getBackingClass()
128   {
129     return backingClass;
130   }
131 
132   /* Definition. */
133 
134   /**
135    * Sets the definition of this component.
136    *
137    * @param    setDefinition        The component's definition.
138    *
139    */
140 
141   public void setDefinition(String setDefinition)
142   {
143     definition = setDefinition;
144   }
145 
146   /**
147    * Returns the definition of this component.
148    *
149    * @return                  The definition of this component.
150    *
151    */
152 
153   public String getDefinition()
154   {
155     return definition;
156   }
157 
158 
159   /* Attributes. */
160 
161 
162   /**
163    * Returns the specified attribute.
164    *
165    * @param    name            The name of the attribute.
166    *
167    * @return                  The {@link Attribute} of the specified name.
168    *
169    */
170 
171   public Attribute getAttribute(String name)
172   {
173     return (Attribute)attributes.get(name);
174   }
175 
176   /**
177    * Sets the value of the specified attribute.
178    *
179    * @param    name            The name of the attribute.
180    * @param    access            The access control for the attribute.
181    * @param    value            The value of the attribute.
182    *
183    */
184 
185   public void addAttribute(String name, int access, String value)
186   {
187     Attribute attribute = new Attribute(name, access, value);
188     attributes.put(name, attribute);
189   }
190 
191   /**
192    * Returns all attributes of this component.
193    *
194    * @return                  A <code>Collection</code> of {@link Attribute} objects.
195    *
196    */
197 
198   public Collection getAttributes()
199   {
200     return attributes.values();
201   }
202 
203 
204   /* Message Topics. */
205 
206 
207   /**
208    * Returns the topic of the specified message.
209    *
210    * @param    name            The name of the message.
211    *
212    * @return                  The {@link Message} of the specified name.
213    *
214    */
215 
216   public Message getMessage(String name)
217   {
218     return (Message)messages.get(name);
219   }
220 
221   /**
222    * Sets the topic of the specified message.
223    *
224    * @param    name            The name of the message.
225    * @param    type            The type of this message.
226    * @param    access            The access control for the message.
227    * @param    topic            The topic of the message.
228    * @param    signature          The signature of the message.
229    *
230    */
231 
232   public void addMessage(String name, int type, int access, String topic, MessageSignature signature)
233   {
234     Message message = new Message(name, type, access, topic, signature);
235     messages.put(name, message);
236   }
237 
238   /**
239    * Returns all message topics of this component.
240    *
241    * @return                  A <code>Collection</code> of {@link Message} objects.
242    *
243    */
244 
245   public Collection getMessages()
246   {
247     return messages.values();
248   }
249 
250 
251   /* Debug. */
252 
253 
254   public String toString()
255   {
256     StringBuffer buffer = new StringBuffer();
257 
258     buffer.append("ComponentDescriptor=[");
259 
260     buffer.append("name=");
261     buffer.append(name);
262 
263     buffer.append("; class=");
264     buffer.append(backingClass);
265 
266     buffer.append("; definition=");
267     buffer.append(definition);
268 
269     buffer.append("; attributes=[");
270 
271     Iterator attributeIterator = attributes.values().iterator();
272 
273     while (attributeIterator.hasNext())
274     {
275       Attribute attribute = (Attribute)attributeIterator.next();
276 
277       buffer.append(attribute.getName());
278       buffer.append("=");
279       buffer.append(attribute.getValue());
280 
281       if (attributeIterator.hasNext())
282       {
283         buffer.append("; ");
284       }
285     }
286 
287     buffer.append("]; messages=[");
288 
289     Iterator messageIterator = messages.values().iterator();
290 
291     while (messageIterator.hasNext())
292     {
293       Message message = (Message)messageIterator.next();
294 
295       buffer.append(message.getName());
296       buffer.append("=");
297       buffer.append(message.getTopic());
298 
299       if (messageIterator.hasNext())
300       {
301         buffer.append("; ");
302       }
303     }
304 
305     buffer.append("]]");
306 
307     return buffer.toString();
308   }
309 
310 
311   /* Support classes. */
312 
313 
314   /**
315    * Represents a single property of the component.
316    *
317    */
318 
319   protected static class Property
320   {
321     /** The name of this property. */
322     protected String name;
323 
324     /** The access of this property. */
325     protected int access;
326 
327 
328     /* Constructors. */
329 
330 
331     /**
332      * Constructs a <code>Property</code> instance.
333      *
334      * @param    setName            The name of this property.
335      * @param    setAccess          The access control for this property.
336      *
337      */
338 
339     public Property(String setName, int setAccess)
340     {
341       name = setName;
342       access = setAccess;
343     }
344 
345 
346     /* Contants. */
347 
348 
349     /** Constants for the access control of the property. */
350     public interface Access
351     {
352       /** This property has unknown access. */
353       public static int Unknown   = 0;
354 
355       /** This is a public property. */
356       public static int Public   = 1;
357 
358       /** This is a private property. */
359       public static int Private  = 2;
360     }
361 
362 
363     /* Accessors. */
364 
365 
366     /* Name. */
367 
368     /**
369      * Returns the name of this property.
370      *
371      * @return                  The name of this property.
372      *
373      */
374 
375     public String getName()
376     {
377       return name;
378     }
379 
380     /**
381      * Sets the name of this property.
382      *
383      * @param    setName            The name of this property.
384      *
385      */
386 
387     public void setName(String setName)
388     {
389       name = setName;
390     }
391 
392     /* Access control. */
393 
394     /**
395      * Sets the access control of this property.
396      *
397      * @param    setAccess          The property's access type.
398      *
399      */
400 
401     public void setAccess(int setAccess)
402     {
403       access = setAccess;
404     }
405 
406     /**
407      * Returns the access control of this property.
408      *
409      * @return                  The access type of this property. See {@link Access}.
410      *
411      */
412 
413     public int getAccess()
414     {
415       return access;
416     }
417   }
418 
419   /**
420    * Represents a single attribute of the component.
421    *
422    */
423 
424   public static class Attribute extends Property
425   {
426     /** The value of this attribute. */
427     protected String value;
428 
429 
430     /* Constructors. */
431 
432 
433     /**
434      * Constructs an <code>Attribute</code> instance.
435      *
436      * @param    setName            The name of this attribute.
437      * @param    setAccess          The access control for this attribute.
438      * @param    setValue          The value of this attribute.
439      *
440      */
441 
442     public Attribute(String setName, int setAccess, String setValue)
443     {
444       super(setName, setAccess);
445 
446       value = setValue;
447     }
448 
449 
450     /* Accessors. */
451 
452 
453     /* Value. */
454 
455     /**
456      * Returns the value of this attribute.
457      *
458      * @return                  The value of this attribute.
459      *
460      */
461 
462     public String getValue()
463     {
464       return value;
465     }
466 
467     /**
468      * Sets the value of this attribute.
469      *
470      * @param    setValue          The value of this attribute.
471      *
472      */
473 
474     public void setValue(String setValue)
475     {
476       value = setValue;
477     }
478   }
479 
480   /**
481    * Represents a single message of the component.
482    *
483    */
484 
485   public static class Message extends Property
486   {
487     /** The type of this message. See {@link Type Type} for possible values. */
488     protected int type;
489 
490     /** The topic of this message. */
491     protected String topic;
492 
493     /** The signature of this message. */
494     protected MessageSignature signature;
495 
496 
497     /* Constants. */
498 
499 
500     /** Constants for the type of the message. */
501     public interface Type
502     {
503       /** The type of this message is undefined. */
504       public static int Undefined  = 0;
505 
506       /** This message is sent. */
507       public static int Send     = 1;
508 
509       /** This message is received (listened for). */
510       public static int Receive  = 2;
511     }
512 
513 
514     /* Constructors. */
515 
516 
517     /**
518      * Constructs a <code>Message</code> instance.
519      *
520      * @param    setName            The name of this message.
521      * @param    setType            The type of this message.
522      * @param    setAccess          The access control for this message.
523      * @param    setTopic          The topic of this message.
524      * @param    setSignature        The signature of this message.
525      *
526      */
527 
528     public Message(String setName, int setType, int setAccess, String setTopic, MessageSignature setSignature)
529     {
530       super(setName, setAccess);
531 
532       type = setType;
533       topic = setTopic;
534       signature = setSignature;
535     }
536 
537 
538     /* Accessors. */
539 
540 
541     /* Type. */
542 
543     /**
544      * Sets the type of this message.
545      *
546      * @param    setType            The type of this message. See {@link Type Type}.
547      *
548      */
549 
550     public void setType(int setType)
551     {
552       type = setType;
553     }
554 
555     /**
556      * Returns the type of this message.
557      *
558      * @return                  The type of this message. See {@link Type Type}.
559      *
560      */
561 
562     public int getType()
563     {
564       return type;
565     }
566 
567     /* Topic. */
568 
569     /**
570      * Returns the topic of this message.
571      *
572      * @return                  The topic of this message.
573      *
574      */
575 
576     public String getTopic()
577     {
578       return topic;
579     }
580 
581     /**
582      * Sets the topic of this message.
583      *
584      * @param    setTopic          The topic of this message.
585      *
586      */
587 
588     public void setTopic(String setTopic)
589     {
590       topic = setTopic;
591     }
592 
593     /* Signature. */
594 
595     /**
596      * Returns the signature of this message.
597      *
598      * @return                  The {@link MessageSignature} of this message.
599      *                      Null if one is not available.
600      *
601      */
602 
603     public MessageSignature getSignature()
604     {
605       return signature;
606     }
607 
608     /**
609      * Sets the signature of this message.
610      *
611      * @param    setSignature        The signature of this message.
612      *
613      */
614 
615     public void setSignature(MessageSignature setSignature)
616     {
617       signature = setSignature;
618     }
619   }
620 }
621