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<String> nodeFrom;
42      private final Comparable<String> nodeTo;
43  
44      /**
45       * @param nodeFrom
46       * @param nodeTo
47       */
48      public Edge(final Comparable<String> nodeFrom, final Comparable<String> 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      @Override
60      public Object getFrom() {
61          return this.nodeFrom;
62      }
63  
64      @Override
65      public Object getTo() {
66          return this.nodeTo;
67      }
68  
69      @Override
70      public String toString() {
71          return this.nodeFrom + "->" + this.nodeTo;
72      }
73  
74      /**
75       * Compares this edge to the given one using 
76       * the <code>{@link #getFrom()}</code> nodes first. 
77       * If those are equal the <code>{@link #getTo()}}</code>
78       * is used for comparison.
79       * @see java.lang.Comparable#compareTo(java.lang.Object)
80       */
81      @Override
82      public int compareTo(final Object o) {
83          logger.debug("compareTo(o={}) - start", o);
84  
85          final Edge otherEdge = (Edge) o;
86          int result = this.nodeFrom.compareTo((String) otherEdge.getFrom());
87          if ( result == 0 ) {
88              result = this.nodeTo.compareTo((String) otherEdge.getTo());
89          }
90          return result;
91      }
92  
93      @Override
94      public int hashCode() {
95          final int prime = 31;
96          int result = 1;
97          result = prime * result
98                  + ((nodeFrom == null) ? 0 : nodeFrom.hashCode());
99          result = prime * result + ((nodeTo == null) ? 0 : nodeTo.hashCode());
100         return result;
101     }
102 
103     @Override
104     public boolean equals(final Object obj) {
105         if (this == obj)
106             return true;
107         if (obj == null)
108             return false;
109         if (getClass() != obj.getClass())
110             return false;
111         final Edge other = (Edge) obj;
112         if (nodeFrom == null) {
113             if (other.nodeFrom != null)
114                 return false;
115         } else if (!nodeFrom.equals(other.nodeFrom))
116             return false;
117         if (nodeTo == null) {
118             if (other.nodeTo != null)
119                 return false;
120         } else if (!nodeTo.equals(other.nodeTo))
121             return false;
122         return true;
123     }
124 
125 }