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.WeightedEdge;
25  
26  /**
27   * 
28   */
29  public class BaseLabeledWeightedEdge
30      extends BaseLabeledEdge
31      implements WeightedEdge<BaseLabeledVertex>
32  {
33  
34      private final Double weight;
35  
36      public BaseLabeledWeightedEdge( String label, BaseLabeledVertex head, BaseLabeledVertex tail, Double weight )
37      {
38          super( label, head, tail );
39          this.weight = weight;
40      }
41  
42      /**
43       * {@inheritDoc}
44       */
45      public int compareTo( WeightedEdge<BaseLabeledVertex> other )
46      {
47          return weight.compareTo( other.getWeight() );
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      public Double getWeight()
54      {
55          return weight;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public int hashCode()
63      {
64          final int prime = 31;
65          int result = super.hashCode();
66          result = prime * result + ( ( weight == null ) ? 0 : weight.hashCode() );
67          return result;
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public boolean equals( Object obj )
75      {
76          if ( this == obj )
77          {
78              return true;
79          }
80  
81          if ( !super.equals( obj ) )
82          {
83              return false;
84          }
85  
86          if ( getClass() != obj.getClass() )
87          {
88              return false;
89          }
90  
91          BaseLabeledWeightedEdge other = (BaseLabeledWeightedEdge) obj;
92          if ( weight == null )
93          {
94              if ( other.getWeight() != null )
95              {
96                  return false;
97              }
98          }
99          else if ( !weight.equals( other.getWeight() ) )
100         {
101             return false;
102         }
103 
104         return true;
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public String toString()
112     {
113         return format( "%s( %s =%s=> %s )", getLabel(), getHead(), weight, getTail() );
114     }
115 
116 }