View Javadoc

1   /*
2    * Copyright (C) 2003-2004 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
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.3 $, $Date: 2004/09/06 17:22:41 $, $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         // determine whether we need a separator
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         // set all other fields to null
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             // delegate to local feature
138             return localFeature.getComment();
139         } else {
140             // delegate to superclass
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             return prefix + separator + localFeature.getRepresentation();
165         } else {
166             // this is a comment-only feature
167             return null;
168         }
169     }
170 
171     /***
172      * Returns a string representation of this object.
173      *
174      * @return a textual representation
175      */
176     public String toString() {
177         return new ToStringBuilder(this)
178             .appendSuper(super.toString())
179             .append("prefix", prefix)
180             .append("local feature", localFeature)
181             .toString();
182     }
183 
184 }