View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.util.search;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Basic implementation of the {@link IEdge} interface.
28   * 
29   * @author Felipe Leme (dbunit@felipeal.net)
30   * @author Last changed by: $Author$
31   * @version $Revision$ $Date$
32   * @since 2.2.0 (Aug 25, 2005)
33   */
34  public class Edge implements IEdge {
35  
36      /**
37       * Logger for this class
38       */
39      private static final Logger logger = LoggerFactory.getLogger(Edge.class);
40  
41      private final Comparable nodeFrom;
42      private final Comparable nodeTo;
43  
44      /**
45       * @param nodeFrom
46       * @param nodeTo
47       */
48      public Edge(Comparable nodeFrom, Comparable nodeTo) {
49          if (nodeFrom == null) {
50              throw new IllegalArgumentException("node from cannot be null");
51          }
52          if (nodeTo == null) {
53              throw new IllegalArgumentException("node to cannot be null");
54          }
55          this.nodeFrom = nodeFrom;
56          this.nodeTo = nodeTo;
57      }
58  
59      public Object getFrom() {
60          return this.nodeFrom;
61      }
62  
63      public Object getTo() {
64          return this.nodeTo;
65      }
66  
67      public String toString() {
68          return this.nodeFrom + "->" + this.nodeTo;
69      }
70  
71      /**
72       * Compares this edge to the given one using 
73       * the <code>{@link #getFrom()}</code> nodes first. 
74       * If those are equal the <code>{@link #getTo()}}</code>
75       * is used for comparison.
76       * @see java.lang.Comparable#compareTo(java.lang.Object)
77       */
78      public int compareTo(Object o) {
79          logger.debug("compareTo(o={}) - start", o);
80  
81          Edge otherEdge = (Edge) o;
82          int result = this.nodeFrom.compareTo(otherEdge.getFrom());
83          if ( result == 0 ) {
84              result = this.nodeTo.compareTo(otherEdge.getTo());
85          }
86          return result;
87      }
88  
89      public int hashCode() {
90          final int prime = 31;
91          int result = 1;
92          result = prime * result
93                  + ((nodeFrom == null) ? 0 : nodeFrom.hashCode());
94          result = prime * result + ((nodeTo == null) ? 0 : nodeTo.hashCode());
95          return result;
96      }
97  
98      public boolean equals(Object obj) {
99          if (this == obj)
100             return true;
101         if (obj == null)
102             return false;
103         if (getClass() != obj.getClass())
104             return false;
105         Edge other = (Edge) obj;
106         if (nodeFrom == null) {
107             if (other.nodeFrom != null)
108                 return false;
109         } else if (!nodeFrom.equals(other.nodeFrom))
110             return false;
111         if (nodeTo == null) {
112             if (other.nodeTo != null)
113                 return false;
114         } else if (!nodeTo.equals(other.nodeTo))
115             return false;
116         return true;
117     }
118 
119 }