View Javadoc

1   // Copyright (C) 2001 - 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.common;
23  
24  
25  /**
26   * Base class which provides equality and ordering semantics for
27   * {@link Test} implementations.
28   *
29   * @author Philip Aston
30   */
31  public abstract class AbstractTestSemantics implements Test {
32  
33    /**
34     * Define ordering.
35     *
36     * @param o <code>Object</code> to compare.
37     * @return <code>-1</code> if <code>o</code> is less, <code>0</code>
38     * if its equal, <code>1</code> if its greater.
39     */
40    @Override public final int compareTo(Test o) {
41      final int ours = getNumber();
42      final int others = ((AbstractTestSemantics)o).getNumber();
43      return ours < others ? -1 : (ours == others ? 0 : 1);
44    }
45  
46    /**
47     * Define hash semantics. The test number is used as the hash code.
48     * I wondered whether it was worth distributing the hash codes more
49     * evenly across the range of an int, but using the value is good
50     * enough for <code>java.lang.Integer</code> so its good enough for
51     * us.
52     *
53     * @return The hash code.
54     */
55    @Override public final int hashCode() {
56      return getNumber();
57    }
58  
59    /**
60     * {@inheritDoc}
61     */
62    @Override public final boolean equals(Object o) {
63  
64      if (o == this) {
65        return true;
66      }
67  
68      // instanceof does not break symmetry since equals() is final
69      // and all Tests inherit from AbstractTestSemantics.
70      if (!(o instanceof Test)) {
71        return false;
72      }
73  
74      return getNumber() == ((Test)o).getNumber();
75    }
76  
77    /**
78     * {@inheritDoc}
79     */
80    @Override public final String toString() {
81  
82      final String description = getDescription();
83  
84      if (description == null) {
85        return "Test " + getNumber();
86      }
87      else {
88        return "Test " + getNumber() + " (" + description + ')';
89      }
90    }
91  }