1   package org.apache.commons.graph.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.lang.String.format;
23  
24  import org.apache.commons.graph.LabeledEdge;
25  
26  public class BaseLabeledEdge
27      implements LabeledEdge<BaseLabeledVertex>
28  {
29  
30      private final String label;
31  
32      private final BaseLabeledVertex head;
33  
34      private final BaseLabeledVertex tail;
35  
36      public BaseLabeledEdge( String label, BaseLabeledVertex head, BaseLabeledVertex tail )
37      {
38          this.label = label;
39          this.head = head;
40          this.tail = tail;
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      public String getLabel()
47      {
48          return label;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      public BaseLabeledVertex getHead()
55      {
56          return head;
57      }
58  
59      /**
60       * {@inheritDoc}
61       */
62      public BaseLabeledVertex getTail()
63      {
64          return tail;
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public int hashCode()
72      {
73          final int prime = 31;
74          int result = 1;
75          result = prime * result + ( ( head == null ) ? 0 : head.hashCode() );
76          result = prime * result + ( ( label == null ) ? 0 : label.hashCode() );
77          result = prime * result + ( ( tail == null ) ? 0 : tail.hashCode() );
78          return result;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public boolean equals( Object obj )
86      {
87          if ( this == obj )
88          {
89              return true;
90          }
91  
92          if ( obj == null )
93          {
94              return false;
95          }
96  
97          if ( getClass() != obj.getClass() )
98          {
99              return false;
100         }
101 
102         BaseLabeledEdge other = (BaseLabeledEdge) obj;
103 
104         if ( head == null )
105         {
106             if ( other.getHead() != null )
107             {
108                 return false;
109             }
110         }
111         else if ( !head.equals( other.getHead() ) )
112         {
113             return false;
114         }
115 
116         if ( tail == null )
117         {
118             if ( other.getTail() != null )
119             {
120                 return false;
121             }
122         }
123         else if ( !tail.equals( other.getTail() ) )
124         {
125             return false;
126         }
127 
128         if ( label == null )
129         {
130             if ( other.getLabel() != null )
131             {
132                 return false;
133             }
134         }
135         else if ( !label.equals( other.label ) )
136         {
137             return false;
138         }
139 
140         return true;
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public String toString()
148     {
149         return format( "%s( %s ==> %s )", label, head, tail );
150     }
151 
152 }