KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > util > info > DependencyDescriptor


1 /*
2  * Copyright (C) The Loom Group. All rights reserved.
3  *
4  * This software is published under the terms of the Loom
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.loom.components.util.info;
9
10 /**
11  * A descriptor that describes dependency information for a particular
12  * Component. This class contains information about; <ul> <li>key: the name
13  * component uses to look up dependency</li> <li>type: the class/interface that
14  * the dependency must provide</li> </ul>
15  *
16  * <p>Also associated with each dependency is a set of arbitrary Attributes that
17  * can be used to store extra information about dependency.</p>
18  *
19  * <p>Possible uses for the Attributes are to declare container specific
20  * constraints of component. For example a dependency on a Corba ORB may also
21  * require that the Corba ORB contain the TimeServer and PersistenceStateService
22  * at initialization. Or it may require that the componenet be multi-thread safe
23  * or that it is persistent etc. These are all container specific demands.</p>
24  *
25  * @author Peter Donald
26  * @version $Revision: 1.2 $ $Date: 2004/05/01 12:48:34 $
27  */

28 public final class DependencyDescriptor
29 {
30     /** Constant with empty set of dependencys. */
31     public static final DependencyDescriptor[] EMPTY_SET = new DependencyDescriptor[ 0 ];
32
33     /** The postfix indicating an array type. */
34     public static final String JavaDoc ARRAY_POSTFIX = "[]";
35
36     /** The postfix indicating a "Map" type. */
37     public static final String JavaDoc MAP_POSTFIX = "{}";
38
39     /** The name the component uses to lookup dependency. */
40     private final String JavaDoc m_key;
41
42     /** The service class/interface that the dependency must provide. */
43     private final String JavaDoc m_type;
44
45     /** True if dependency is optional, false otherwise. */
46     private final boolean m_optional;
47
48     /**
49      * Constructor that has all parts sans parent.
50      */

51     public DependencyDescriptor( final String JavaDoc key,
52                                  final String JavaDoc type,
53                                  final boolean optional )
54     {
55         if( null == key )
56         {
57             throw new NullPointerException JavaDoc( "key" );
58         }
59         if( null == type )
60         {
61             throw new NullPointerException JavaDoc( "type" );
62         }
63
64         m_key = key;
65         m_type = type;
66         m_optional = optional;
67     }
68
69     /**
70      * Return the key the component uses to lookup dependency.
71      *
72      * @return the key the component uses to lookup dependency.
73      */

74     public String JavaDoc getKey()
75     {
76         return m_key;
77     }
78
79     /**
80      * Return the service class/interface name that describes the dependency
81      * must fulfilled by a provider.
82      *
83      * @return a reference to service descriptor that describes the fulfillment
84      * obligations that must be met by a service provider.
85      */

86     public String JavaDoc getType()
87     {
88         return m_type;
89     }
90
91     /**
92      * Return true if dependency is optional, false otherwise.
93      *
94      * @return true if dependency is optional, false otherwise.
95      */

96     public boolean isOptional()
97     {
98         return m_optional;
99     }
100
101     /**
102      * Return true if dependency type is an array.
103      *
104      * @return true if dependency type is an array.
105      */

106     public boolean isArray()
107     {
108         return getType().endsWith( ARRAY_POSTFIX );
109     }
110
111     /**
112      * Return true if dependency type is a map.
113      *
114      * @return true if dependency type is a map.
115      */

116     public boolean isMap()
117     {
118         return getType().endsWith( MAP_POSTFIX );
119     }
120
121     /**
122      * Return the type of component type if the service is an array or Map
123      * Service. Otherwise just return the name of service.
124      *
125      * @return the Service component type
126      */

127     public String JavaDoc getComponentType()
128     {
129         final String JavaDoc fullname = getType();
130         if( isArray() )
131         {
132             final int end = fullname.length() - ARRAY_POSTFIX.length();
133             return fullname.substring( 0, end );
134         }
135         else if( isMap() )
136         {
137             final int end = fullname.length() - MAP_POSTFIX.length();
138             return fullname.substring( 0, end );
139         }
140         else
141         {
142             return fullname;
143         }
144     }
145
146     /**
147      * Convert dependency to human readable string representaiton.
148      *
149      * @return strinigified dependency
150      */

151     public String JavaDoc toString()
152     {
153         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
154         sb.append( m_key );
155         sb.append( '/' );
156         sb.append( m_type );
157         if( m_optional )
158         {
159             sb.append( "(Optional)" );
160         }
161         return sb.toString();
162     }
163 }
164
Popular Tags