001package org.maltparser.core.syntaxgraph.edge;
002
003import org.maltparser.core.exception.MaltChainedException;
004import org.maltparser.core.syntaxgraph.Weightable;
005import org.maltparser.core.syntaxgraph.node.Node;
006/**
007*
008*
009* @author Johan Hall
010*/
011public class WeightedEdge extends GraphEdge implements Weightable {
012        private Double weight = Double.NaN;
013        
014        public WeightedEdge() { }
015        
016        public WeightedEdge(Node source, Node target, int type) throws MaltChainedException {
017                super(source, target, type);
018        }
019
020        public WeightedEdge(Node source, Node target, int type, Double weight) throws MaltChainedException {
021                super(source, target, type);
022                setWeight(weight);
023        }
024        
025        public void clear() throws MaltChainedException {
026                super.clear();
027                weight = Double.NaN;
028        }
029        
030        public double getWeight() {
031                return weight.doubleValue();
032        }
033
034        public void setWeight(double weight) {
035                this.weight = weight;
036        }
037        
038        public int compareTo(WeightedEdge that) {
039            if (this == that) return 0;
040            int comparison = this.weight.compareTo(that.getWeight());
041            if ( comparison != 0 ) return comparison;
042            
043            return super.compareTo(that);
044        }
045        
046        public boolean equals(Object obj) {
047                WeightedEdge e = (WeightedEdge)obj;
048                return weight.equals(e.getWeight()) && super.equals(obj); 
049        }
050        
051        public int hashCode() {
052                int hash = 7;
053                hash = 31 * hash + (null == weight ? 0 : weight.hashCode());
054                return 31 * hash + super.hashCode();
055        }
056        
057        public String toString() {
058                final StringBuilder sb = new StringBuilder();
059                sb.append(getWeight());
060                sb.append(' ');
061                sb.append(getSource().getIndex());
062                sb.append("->");
063                sb.append(getTarget().getIndex());
064                sb.append(' ');
065                sb.append(super.toString());
066                return sb.toString();
067        }
068}