View Javadoc

1   /*
2    * Copyright (c) 2003-2008, by Henrik Arro and Contributors
3    *
4    * This file is part of JSeq, a tool to automatically create
5    * sequence diagrams by tracing program execution.
6    *
7    * See <http://jseq.sourceforge.net> for more information.
8    *
9    * JSeq is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of
12   * the License, or (at your option) any later version.
13   *
14   * JSeq is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public License
20   * along with JSeq. If not, see <http://www.gnu.org/licenses/>.
21   */
22  
23  package th.co.edge.jseq;
24  
25  /**
26   * A <code>MockObject</code> is an immutable class representing one life-line
27   * in a sequence diagram.
28   */
29  public class MockObject implements Comparable<MockObject> {
30      private final String name;
31      private final int column;
32  
33      /**
34       * Creates a new <code>MockObject</code> with a given name and column in
35       * the diagram.
36       *
37       * @param name
38       *            the name that will be displayed at the top of the life-line
39       *
40       * @param column
41       *            the column number of this life-line
42       */
43      public MockObject(String name, int column) {
44          this.name = name;
45          this.column = column;
46      }
47  
48      /**
49       * Returns the name that will be displayed at the top of the life-line.
50       *
51       * @return the name of this <code>MockObject</code>
52       */
53      public String getName() {
54          return name;
55      }
56  
57      /**
58       * Returns the column number of this <code>MockObject</code>, which
59       * determines the order of the life-lines in the sequence diagram.
60       *
61       * @return the column number of this <code>MockObject</code>
62       */
63      public int getColumn() {
64          return column;
65      }
66  
67      /**
68       * Returns a string representation of this <code>MockObject</code>,
69       * mainly useful for logging.
70       *
71       * @return a string representation of this <code>MockObject</code>
72       */
73      @Override
74      public String toString() {
75          return "{" + getName() + "," + getColumn() + "}";
76      }
77  
78      /**
79       * Compares this <code>MockObject</code> to another object and returns
80       * <code>true</code> if and only if the other object is a
81       * <code>MockObject</code> with the same name and column number.
82       *
83       * @param o
84       *            the object to compare this <code>MockObject</code> to
85       *
86       * @return <code>true</code> if <code>o</code> is a
87       *         <code>MockObject</code> with the same name and column number,
88       *         <code>false</code> otherwise
89       *
90       */
91      @Override
92      public boolean equals(Object o) {
93          boolean equal = false;
94          if (o instanceof MockObject) {
95              MockObject otherMockObject = (MockObject) o;
96              equal =
97                      name.equals(otherMockObject.name) &&
98                              column == otherMockObject.column;
99          }
100         return equal;
101     }
102 
103     /**
104      * Returns a hash code for this <code>MockObject</code>.
105      *
106      * @return a hash code for this <code>MockObject</code>
107      */
108     @Override
109     public int hashCode() {
110         int result = 17;
111         result = 31 * result + name.hashCode();
112         result = 31 * result + column;
113         return result;
114     }
115 
116     /**
117      * Compares this <code>MockObject</code> to another
118      * <code>MockObject</code>, returning a negative integer, zero or a
119      * positive integer if this object is less than, equal to, or greater than
120      * the other object, respectively.
121      *
122      * <p>
123      * Comparison is done as follows:
124      * <ul>
125      * <li>First the column numbers are compared</li>
126      * <li>If the column numbers are equal, the names are compared
127      * lexicographically</li>
128      * </ul>
129      *
130      * @param otherObject
131      *            the <code>MockObject</code> to compare this object to
132      *
133      * @return <code>0</code> if this object is equal to
134      *         <code>otherObject</code>; a negative integer if this object is
135      *         smaller than <code>otherObject</code>; and a positive integer
136      *         if this object is greater than <code>otherObject</code>
137      */
138     @Override
139     public int compareTo(MockObject otherObject) {
140         int result = getColumn() - otherObject.getColumn();
141         if (result == 0) {
142             result = name.compareTo(otherObject.name);
143         }
144         return result;
145     }
146 }