View Javadoc

1   /*
2    * Copyright (C) 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.extract;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.lang.builder.EqualsBuilder;
26  import org.apache.commons.lang.builder.HashCodeBuilder;
27  import org.apache.commons.lang.builder.ToStringBuilder;
28  
29  /***
30   * A class (type) of entities to recognize. Instances of this class are
31   * immutable and thus thread-safe.
32   *
33   * @author Christian Siefkes
34   * @version $Revision: 1.2 $, $Date: 2004/02/03 13:16:42 $, $Author: siefkes $
35   */
36  public class TargetClass {
37  
38      /***
39       * The name of this class.
40       */
41      private final String name;
42  
43      /***
44       * Non-public constructor -- should only be used by subclasses and related
45       * classes. Other classes should go thru {@link TargetStructure} to access
46       * target classes.
47       *
48       * @param className the name of this class (outer whitespace will be
49       * {@linkplain String#trim() trimmed})
50       * @throws IllegalArgumentException if <code>className</code> is null or
51       * empty (after trimming)
52       */
53      protected TargetClass(final String className)
54              throws IllegalArgumentException {
55          super();
56          final String trimmedName = StringUtils.trimToEmpty(className);
57  
58          if (StringUtils.isEmpty(trimmedName)) {
59              throw new IllegalArgumentException(
60                      "Target class name must not be null or empty");
61          }
62          name = trimmedName;
63      }
64  
65      /***
66       * Indicates whether some other object is "equal to" this one, fulfulling
67       * the {@link Object#equals(java.lang.Object)} contract.
68       *
69       * @param obj the reference object with which to compare
70       * @return <code>true</code> iff the specified object is a
71       * {@link TargetClass} equal to this instance
72       */
73      public boolean equals(final Object obj) {
74          if (obj == this) {
75              return true;
76          } else if ((obj != null) && (getClass().equals(obj.getClass()))) {
77              // used getClass instead of instanceof because otherwise subclasses
78              // with additional fields would break the contract
79              final TargetClass other = (TargetClass) obj;
80              return new EqualsBuilder()
81                  .append(getName(), other.getName())
82                  .isEquals();
83          } else {
84              return false;
85          }
86      }
87  
88      /***
89       * Returns the name of this class.
90       * @return the value of the attribute
91       */
92      public String getName() {
93          return name;
94      }
95  
96      /***
97       * Returns a hash code value for this object, fulfulling the
98       * {@link Object#hashCode()} contract.
99       *
100      * @return a hash code value for this object
101      */
102     public int hashCode() {
103         // you pick two hard-coded, randomly chosen, non-zero, odd numbers
104         // (preferably primes); ideally different for each class
105         return new HashCodeBuilder(13, 17)
106             .append(getName())
107             .toHashCode();
108     }
109 
110     /***
111      * Returns a string representation of this object.
112      * @return a textual representation
113      */
114     public String toString() {
115         return new ToStringBuilder(this)
116             .append("name", name)
117             .toString();
118     }
119 
120 }