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.context;
23
24 import java.util.Iterator;
25 import java.util.LinkedList;
26
27 import org.apache.commons.lang.builder.ToStringBuilder;
28
29 import de.fu_berlin.ties.classify.feature.Feature;
30
31 /***
32 * An immutable representation of a feature that can be used for classification.
33 *
34 * @author Christian Siefkes
35 * @version $Revision: 1.7 $, $Date: 2006/10/21 16:04:03 $, $Author: siefkes $
36 */
37 public class GlobalFeature extends Feature {
38
39 /***
40 * Converts a series of {@link LocalFeature}s into global features,
41 * adding the created global features to a linked list.
42 *
43 * @param axisName the name of the global axis
44 * @param localIter an iterator over the local features to globalize
45 * @param globalFeatures the created global features are added to this
46 * linked list
47 * @param addAtEnd whether to add the created features at the end of at the
48 * beginning of the linked list
49 * @throws ClassCastException if the iterator refers to objects that aren't
50 * {@link LocalFeature}
51 */
52 public static void globalize(final String axisName,
53 final Iterator<LocalFeature> localIter,
54 final LinkedList<Feature> globalFeatures,
55 final boolean addAtEnd)
56 throws ClassCastException {
57 GlobalFeature newFeature;
58 LocalFeature localValue;
59
60 while (localIter.hasNext()) {
61 localValue = localIter.next();
62 newFeature = new GlobalFeature(axisName, localValue);
63
64 if (addAtEnd) {
65 globalFeatures.addLast(newFeature);
66 } else {
67 globalFeatures.addFirst(newFeature);
68 }
69 }
70 }
71
72 /***
73 * Separator string between axis and element names, used if neither or
74 * them is empty.
75 */
76 public static final String SEP = ".";
77
78 /***
79 * The local feature wrapped by this global feature.
80 */
81 private final LocalFeature localFeature;
82
83 /***
84 * The axis prefix wrapped by this feature.
85 */
86 private final String prefix;
87
88 /***
89 * The separator actually used between axis and element names -- either
90 * the {@link #SEP} constants, or the empty string if one of them is empty
91 * as well (so there is no need for a separator).
92 */
93 private final String separator;
94
95 /***
96 * Creates a new instance that combines a local feature with an axis prefix.
97 *
98 * @param axisPrefix the axis prefix wrapped by this feature
99 * @param local the local feature wrapped by this global feature
100 */
101 public GlobalFeature(final String axisPrefix,
102 final LocalFeature local) {
103 super();
104 prefix = axisPrefix;
105 localFeature = local;
106
107
108 if ((prefix.length() > 0)
109 && (localFeature.getElementName().length() > 0)) {
110 separator = SEP;
111 } else {
112 separator = "";
113 }
114 }
115
116 /***
117 * Creates a new instance that contains only a comment.
118 *
119 * @param myComment a comment on the features to follow, ignored for
120 * classification
121 */
122 public GlobalFeature(final String myComment) {
123 super(myComment);
124
125 prefix = null;
126 localFeature = null;
127 separator = null;
128 }
129
130 /***
131 * Returns the comment attached to this feature, if any.
132 *
133 * @return the comment, or <code>null</code> is no comment was stored
134 */
135 public String getComment() {
136 if (localFeature != null) {
137
138 return localFeature.getComment();
139 } else {
140
141 return super.getComment();
142 }
143 }
144
145 /***
146 * Returns the local feature wrapped by this global feature.
147 *
148 * @return the local feature; or <code>null</code>if this features doesn't
149 * wrap a local feature (but only a comment)
150 */
151 public LocalFeature getLocalFeature() {
152 return localFeature;
153 }
154
155 /***
156 * Returns the representation of this feature, to be used for
157 * classification.
158 *
159 * @return the feature representation, or <code>null</code> if this feature
160 * contains only a comment
161 */
162 public String getRepresentation() {
163 if (localFeature != null) {
164 final String localRep = localFeature.getRepresentation();
165 final StringBuilder builder = new StringBuilder(
166 prefix.length() + separator.length() + localRep.length());
167 builder.append(prefix).append(separator).append(localRep);
168 return builder.toString();
169 } else {
170
171 return null;
172 }
173 }
174
175 /***
176 * Returns a string representation of this object.
177 *
178 * @return a textual representation
179 */
180 public String toString() {
181 return new ToStringBuilder(this)
182 .appendSuper(super.toString())
183 .append("prefix", prefix)
184 .append("local feature", localFeature)
185 .toString();
186 }
187
188 }