Edge.java

  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. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. /**
  25.  * Basic implementation of the {@link IEdge} interface.
  26.  *
  27.  * @author Felipe Leme (dbunit@felipeal.net)
  28.  * @author Last changed by: $Author$
  29.  * @version $Revision$ $Date$
  30.  * @since 2.2.0 (Aug 25, 2005)
  31.  */
  32. public class Edge implements IEdge {

  33.     /**
  34.      * Logger for this class
  35.      */
  36.     private static final Logger logger = LoggerFactory.getLogger(Edge.class);

  37.     private final Comparable<String> nodeFrom;
  38.     private final Comparable<String> nodeTo;

  39.     /**
  40.      * @param nodeFrom
  41.      * @param nodeTo
  42.      */
  43.     public Edge(final Comparable<String> nodeFrom, final Comparable<String> nodeTo) {
  44.         if (nodeFrom == null) {
  45.             throw new IllegalArgumentException("node from cannot be null");
  46.         }
  47.         if (nodeTo == null) {
  48.             throw new IllegalArgumentException("node to cannot be null");
  49.         }
  50.         this.nodeFrom = nodeFrom;
  51.         this.nodeTo = nodeTo;
  52.     }

  53.     @Override
  54.     public Object getFrom() {
  55.         return this.nodeFrom;
  56.     }

  57.     @Override
  58.     public Object getTo() {
  59.         return this.nodeTo;
  60.     }

  61.     @Override
  62.     public String toString() {
  63.         return this.nodeFrom + "->" + this.nodeTo;
  64.     }

  65.     /**
  66.      * Compares this edge to the given one using
  67.      * the <code>{@link #getFrom()}</code> nodes first.
  68.      * If those are equal the <code>{@link #getTo()}}</code>
  69.      * is used for comparison.
  70.      * @see java.lang.Comparable#compareTo(java.lang.Object)
  71.      */
  72.     @Override
  73.     public int compareTo(final Object o) {
  74.         logger.debug("compareTo(o={}) - start", o);

  75.         final Edge otherEdge = (Edge) o;
  76.         int result = this.nodeFrom.compareTo((String) otherEdge.getFrom());
  77.         if ( result == 0 ) {
  78.             result = this.nodeTo.compareTo((String) otherEdge.getTo());
  79.         }
  80.         return result;
  81.     }

  82.     @Override
  83.     public int hashCode() {
  84.         final int prime = 31;
  85.         int result = 1;
  86.         result = prime * result
  87.                 + ((nodeFrom == null) ? 0 : nodeFrom.hashCode());
  88.         result = prime * result + ((nodeTo == null) ? 0 : nodeTo.hashCode());
  89.         return result;
  90.     }

  91.     @Override
  92.     public boolean equals(final Object obj) {
  93.         if (this == obj)
  94.             return true;
  95.         if (obj == null)
  96.             return false;
  97.         if (getClass() != obj.getClass())
  98.             return false;
  99.         final Edge other = (Edge) obj;
  100.         if (nodeFrom == null) {
  101.             if (other.nodeFrom != null)
  102.                 return false;
  103.         } else if (!nodeFrom.equals(other.nodeFrom))
  104.             return false;
  105.         if (nodeTo == null) {
  106.             if (other.nodeTo != null)
  107.                 return false;
  108.         } else if (!nodeTo.equals(other.nodeTo))
  109.             return false;
  110.         return true;
  111.     }

  112. }