1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package de.fu_berlin.ties.extract;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28 import java.util.Set;
29
30 import org.apache.commons.configuration.Configuration;
31 import org.apache.commons.lang.builder.EqualsBuilder;
32 import org.apache.commons.lang.builder.HashCodeBuilder;
33 import org.apache.commons.lang.builder.ToStringBuilder;
34
35 import de.fu_berlin.ties.TiesConfiguration;
36 import de.fu_berlin.ties.util.Util;
37
38 /***
39 * A target structure manages the classes of entities to recognize.
40 * Instances of this class are immutable and thus thread-safe.
41 *
42 * @author Christian Siefkes
43 * @version $Revision: 1.4 $, $Date: 2004/09/06 17:23:10 $, $Author: siefkes $
44 */
45 public class TargetStructure {
46
47 /***
48 * Configuration key: names of the classes to recognize (temporarily).
49 */
50 protected static final String CONFIG_TARGET_CLASSES = "target.classes";
51
52 /***
53 * Maps from class names (String) to {@linkplain TargetClass}es.
54 * This map is immutable and cannot modified.
55 */
56 private final Map<String, TargetClass> classes;
57
58 /***
59 * Creates a new instance using the
60 * {@linkplain TiesConfiguration#CONF standard configuration}.
61 */
62 public TargetStructure() {
63 this(TiesConfiguration.CONF);
64 }
65
66 /***
67 * Creates a new instance using the provided configuration. Currently,
68 * the names of target classes are read from the
69 * {@link #CONFIG_TARGET_CLASSES} parameter.
70 *
71 * @param config used to configure this instance
72 */
73 public TargetStructure(final Configuration config) {
74 super();
75 final String[] classNames =
76 config.getStringArray(CONFIG_TARGET_CLASSES);
77 if ((classNames == null) || (classNames.length == 0)) {
78 Util.LOG.warn("No target classes defined ("
79 + CONFIG_TARGET_CLASSES + " parameter)");
80 }
81
82
83 final LinkedHashMap<String, TargetClass> classMap =
84 new LinkedHashMap<String, TargetClass>();
85
86
87 for (int i = 0; i < classNames.length; i++) {
88 classMap.put(classNames[i], new TargetClass(classNames[i]));
89 }
90
91
92 classes = Collections.unmodifiableMap(classMap);
93 }
94
95 /***
96 * Returns the {@link TargetClass} object for a given class name, if
97 * defined in this target structure.
98 *
99 * @param name the name of the class to look up
100 * @return the {@link TargetClass} for the given name; or <code>null</code>
101 * if the class is not defined in this target structure
102 */
103 public TargetClass classForName(final String name) {
104 return classes.get(name);
105 }
106
107 /***
108 * Returns the set of names of the target classes at the top of the
109 * inheritance hierarchy. This set is immutable and cannot modified.
110 *
111 * @return the set of class names
112 */
113 public Set getBaseClassNames() {
114
115 return classes.keySet();
116 }
117
118 /***
119 * Returns the {@link TargetClass}es at the top of the inheritance
120 * hierarchy. This collection is immutable and cannot modified.
121 *
122 * @return the defined target classes
123 */
124 public Collection getBaseTargetClasses() {
125
126 return classes.values();
127 }
128
129 /***
130 * Indicates whether some other object is "equal to" this one, fulfulling
131 * the {@link Object#equals(java.lang.Object)} contract.
132 *
133 * @param obj the reference object with which to compare
134 * @return <code>true</code> iff the specified object is a
135 * {@link TargetStructure} equal to this instance
136 */
137 public boolean equals(final Object obj) {
138 if (obj == this) {
139 return true;
140 } else if ((obj != null) && (getClass().equals(obj.getClass()))) {
141
142
143 final TargetStructure other = (TargetStructure) obj;
144 return new EqualsBuilder()
145 .append(classes, other.classes)
146 .isEquals();
147 } else {
148 return false;
149 }
150 }
151
152 /***
153 * Returns the set of names of the defined target classes.
154 * This set is immutable and cannot modified.
155 *
156 * @return the set of class names
157 */
158 public Set<String> getClassNames() {
159 return classes.keySet();
160 }
161
162 /***
163 * Returns the defined {@link TargetClass}es.
164 * This collection is immutable and cannot modified.
165 *
166 * @return the defined target classes
167 */
168 public Collection<TargetClass> getTargetClasses() {
169 return classes.values();
170 }
171
172 /***
173 * Returns a hash code value for this object, fulfulling the
174 * {@link Object#hashCode()} contract.
175 *
176 * @return a hash code value for this object
177 */
178 public int hashCode() {
179
180
181 return new HashCodeBuilder(19, 23)
182 .append(classes)
183 .toHashCode();
184 }
185
186 /***
187 * Returns a string representation of this object.
188 * @return a textual representation
189 */
190 public String toString() {
191 return new ToStringBuilder(this)
192
193 .append("target classes", getTargetClasses())
194 .toString();
195 }
196
197 }