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   */
24  public class ComponentRequirement {
25      private String role;
26  
27      private String roleHint = "";
28  
29      private String fieldName;
30  
31      private String fieldMappingType;
32  
33      private boolean optional;
34  
35      /**
36       * Returns the field name that this component requirement will inject.
37       * @return the field name that this component requirement will inject
38       */
39      public String getFieldName() {
40          return fieldName;
41      }
42  
43      /**
44       * Sets the name of the field that will be populated by the required
45       * component.
46       * @param fieldName the name of the field to be populated
47       */
48      public void setFieldName(String fieldName) {
49          this.fieldName = fieldName;
50      }
51  
52      /**
53       * Returns the role of the required component.
54       * @return the role of the required component
55       */
56      public String getRole() {
57          return role;
58      }
59  
60      /**
61       * Sets the role of the require component.
62       * @param role the required component's role
63       */
64      public void setRole(String role) {
65          this.role = role;
66      }
67  
68      /**
69       * Returns the role-hint of the required component.
70       * @return the role-hint of the required component
71       */
72      public String getRoleHint() {
73          return roleHint;
74      }
75  
76      /**
77       * Sets the role-hint of the require component.
78       * Passing null or an empty string will match any available implementation.
79       * @param roleHint the required component's role-hint
80       */
81      public void setRoleHint(String roleHint) {
82          this.roleHint = (roleHint != null) ? roleHint : "";
83      }
84  
85      /**
86       * Returns the type of the field this component requirement will inject.
87       * @return the type of the field this component requirement will inject
88       */
89      public String getFieldMappingType() {
90          return fieldMappingType;
91      }
92  
93      /**
94       * Sets the type of the field that will be populated by the required
95       * component.
96       * @param fieldType the type of the field to be populated
97       */
98      public void setFieldMappingType(String fieldType) {
99          this.fieldMappingType = fieldType;
100     }
101 
102     /**
103      * Whether this component requirement is optional and needs not be satisfied
104      *
105      * @return {@code true} if the requested component may be missing, {@code false} if the component is mandatory.
106      * @since 1.3.0
107      */
108     public boolean isOptional() {
109         return optional;
110     }
111 
112     /**
113      * Controls whether a failure to satisfy this requirement can be tolerated by host component or whether construction
114      * of the host component should also fail.
115      *
116      * @param optional {@code true} if the requested component may be missing, {@code false} if the component is
117      *            mandatory.
118      * @since 1.3.0
119      */
120     public void setOptional(boolean optional) {
121         this.optional = optional;
122     }
123 
124     public String toString() {
125         return "ComponentRequirement{" + "role='"
126                 + getRole() + "'" + ", " + "roleHint='"
127                 + getRoleHint() + "', " + "fieldName='"
128                 + getFieldName() + "'" + "}";
129     }
130 
131     /**
132      * Returns a human-friendly key, suitable for display.
133      * @return a human-friendly key
134      */
135     public String getHumanReadableKey() {
136         StringBuilder key = new StringBuilder();
137 
138         key.append("role: '").append(getRole()).append("'");
139 
140         if (getRoleHint() != null) {
141             key.append(", role-hint: '").append(getRoleHint()).append("'. ");
142         }
143 
144         if (getFieldName() != null) {
145             key.append(", field name: '").append(getFieldName()).append("' ");
146         }
147 
148         return key.toString();
149     }
150 
151     public boolean equals(Object other) {
152         if (other instanceof ComponentRequirement) {
153             String myId = role + ":" + roleHint;
154 
155             ComponentRequirement req = (ComponentRequirement) other;
156             String otherId = req.role + ":" + req.roleHint;
157 
158             return myId.equals(otherId);
159         }
160 
161         return false;
162     }
163 
164     public int hashCode() {
165         return (role + ":" + roleHint).hashCode();
166     }
167 }