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.classify.feature;
23  
24  import org.apache.commons.lang.builder.ToStringBuilder;
25  
26  /***
27   * Default implementation of the
28   * {@link de.fu_berlin.ties.classify.feature.Feature} class. Instances of this
29   * class are immutable and thus thread-safe.
30   *
31   * @author Christian Siefkes
32   * @version $Revision: 1.3 $, $Date: 2004/05/31 19:13:49 $, $Author: siefkes $
33   */
34  public class DefaultFeature extends Feature {
35  
36      /***
37       * The strenght of this feature -- higher values might indicate more
38       * important or more frequent features.
39       */
40      private final float strength;
41  
42      /***
43       * The representation of this feature, to be used for classification.
44       */
45      private final String representation;
46  
47      /***
48       * Creates a new instance, without storing a comment.
49       *
50       * @param rep the representation of this feature; or <code>null</code> to
51       * create a comment-only feature
52       */
53      public DefaultFeature(final String rep) {
54          this(rep, null);
55      }
56  
57      /***
58       * Creates a new instance, setting the strength to 1.0.
59       *
60       * @param rep the representation of this feature; or <code>null</code> to
61       * create a comment-only feature
62       * @param myComment a comment on this feature or the features to follow;
63       * might be <code>null</code>
64       */
65      public DefaultFeature(final String rep, final String myComment) {
66          this(rep, myComment, 1.0f);
67      }
68  
69      /***
70       * Creates a new instance.
71       *
72       * @param rep the representation of this feature; or <code>null</code> to
73       * create a comment-only feature
74       * @param myComment a comment on this feature or the features to follow;
75       * might be <code>null</code>
76       * @param myStrength The strenght of this feature -- higher values might
77       * indicate more important or more frequent features
78       */
79      public DefaultFeature(final String rep, final String myComment,
80                            final float myStrength) {
81          super(myComment);
82          representation = rep;
83          strength = myStrength;
84      }
85  
86      /***
87       * Returns the representation of this feature, to be used for
88       * classification.
89       *
90       * @return the feature representation, or <code>null</code> if this feature
91       * contains only a comment
92       */
93      public String getRepresentation() {
94          return representation;
95      }
96  
97      /***
98       * Returns a strength value for this feature.
99       *
100      * <p>This method is non-public because it should only be accessed through
101      * {@link FeatureVector#strength(Feature)} to allow feature vectors to
102      * modify the strenghts of the stored features.
103      *
104      * @return a strength value for this feature
105      */
106     public float getStrength() {
107         return strength;
108     }
109 
110     /***
111      * Returns a string representation of this object.
112      *
113      * @return a textual representation
114      */
115     public String toString() {
116         return new ToStringBuilder(this)
117             .appendSuper(super.toString())
118             .append("representation", representation)
119             .append("strength", strength)
120             .toString();
121     }
122 
123 }