View Javadoc
1   package org.codehaus.plexus.component.repository;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  /**
20   * This represents a component this is required by another component.
21   *
22   * @author <a href="mmaczka@interia.pl">Michal Maczka</a>
23   * @version $Id$
24   */
25  public class ComponentRequirement
26  {
27      private String role;
28  
29      private String roleHint = "";
30  
31      private String fieldName;
32  
33      private String fieldMappingType;
34  
35      private boolean optional;
36  
37      /**
38       * Returns the field name that this component requirement will inject.
39       * @return the field name that this component requirement will inject
40       */
41      public String getFieldName()
42      {
43          return fieldName;
44      }
45  
46      /**
47       * Sets the name of the field that will be populated by the required
48       * component.
49       * @param fieldName the name of the field to be populated
50       */
51      public void setFieldName( String fieldName )
52      {
53          this.fieldName = fieldName;
54      }
55  
56      /**
57       * Returns the role of the required component.
58       * @return the role of the required component
59       */
60      public String getRole()
61      {
62          return role;
63      }
64  
65      /**
66       * Sets the role of the require component.
67       * @param role the required component's role
68       */
69      public void setRole( String role )
70      {
71          this.role = role;
72      }
73  
74      /**
75       * Returns the role-hint of the required component.
76       * @return the role-hint of the required component
77       */
78      public String getRoleHint()
79      {
80          return roleHint;
81      }
82  
83      /**
84       * Sets the role-hint of the require component.
85       * Passing null or an empty string will match any available implementation.
86       * @param roleHint the required component's role-hint
87       */
88      public void setRoleHint( String roleHint )
89      {
90          this.roleHint = ( roleHint != null ) ? roleHint : "";
91      }
92  
93      /**
94       * Returns the type of the field this component requirement will inject.
95       * @return the type of the field this component requirement will inject
96       */
97      public String getFieldMappingType()
98      {
99          return fieldMappingType;
100     }
101 
102     /**
103      * Sets the type of the field that will be populated by the required
104      * component.
105      * @param fieldType the type of the field to be populated
106      */
107     public void setFieldMappingType( String fieldType )
108     {
109         this.fieldMappingType = fieldType;
110     }
111 
112     /**
113      * Whether this component requirement is optional and needs not be satisfied
114      *
115      * @return {@code true} if the requested component may be missing, {@code false} if the component is mandatory.
116      * @since 1.3.0
117      */
118     public boolean isOptional()
119     {
120         return optional;
121     }
122 
123     /**
124      * Controls whether a failure to satisfy this requirement can be tolerated by host component or whether construction
125      * of the host component should also fail.
126      *
127      * @param optional {@code true} if the requested component may be missing, {@code false} if the component is
128      *            mandatory.
129      * @since 1.3.0
130      */
131     public void setOptional( boolean optional )
132     {
133         this.optional = optional;
134     }
135 
136     public String toString()
137     {
138         return "ComponentRequirement{" +
139             "role='" + getRole() + "'" + ", " +
140             "roleHint='" + getRoleHint() + "', " +
141             "fieldName='" + getFieldName() + "'" +
142             "}";
143     }
144 
145     /**
146      * Returns a human-friendly key, suitable for display.
147      * @return a human-friendly key
148      */
149     public String getHumanReadableKey()
150     {
151         StringBuilder key = new StringBuilder();
152 
153         key.append( "role: '").append( getRole() ).append( "'" );
154 
155         if ( getRoleHint() != null )
156         {
157             key.append( ", role-hint: '" ).append( getRoleHint() ).append( "'. " );
158         }
159 
160         if ( getFieldName() != null )
161         {
162             key.append( ", field name: '" ).append( getFieldName() ).append( "' " );
163         }
164 
165         return key.toString();
166     }
167 
168     public boolean equals( Object other )
169     {
170         if ( other instanceof ComponentRequirement )
171         {
172             String myId = role + ":" + roleHint;
173 
174             ComponentRequirement req = (ComponentRequirement) other;
175             String otherId = req.role + ":" + req.roleHint;
176 
177             return myId.equals( otherId );
178         }
179 
180         return false;
181     }
182 
183     public int hashCode()
184     {
185         return ( role + ":" + roleHint ).hashCode();
186     }
187 }