001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.squid.graph;
021    
022    import java.util.HashSet;
023    import java.util.Set;
024    
025    public class Edge {
026    
027      private final Node      from;
028      private final Node      to;
029      private final Set<Edge> rootEdges = new HashSet<Edge>();
030      private final Set<Node> rootFromNodes = new HashSet<Node>();
031      private final Set<Node> rootToNodes = new HashSet<Node>();
032      private final EdgeUsage usage;
033    
034      public Edge(Node from, Node to, EdgeUsage link) {
035        this(from, to, link, null);
036      }
037    
038      public Edge(Node from, Node to, EdgeUsage usage, Edge rootEdge) {
039        this.from = from;
040        this.to = to;
041        this.usage = usage;
042        addRootEdge(rootEdge);
043      }
044    
045      public Node getFrom() {
046        return from;
047      }
048    
049      public Node getTo() {
050        return to;
051      }
052    
053      public boolean hasAnEdgeFromRootNode(Node rootFromNode) {
054        return rootFromNodes.contains(rootFromNode);
055      }
056    
057      public boolean hasAnEdgeToRootNode(Node rootToNode) {
058        return rootToNodes.contains(rootToNode);
059      }
060      public EdgeUsage getUsage() {
061        return usage;
062      }
063    
064      public Set<Edge> getRootEdges() {
065        return rootEdges;
066      }
067    
068      public int getNumberOfRootFromNodes() {
069        return rootFromNodes.size();
070      }
071    
072      public final void addRootEdge(Edge rootRelationShip) {
073        if (rootRelationShip != null) {
074          rootEdges.add(rootRelationShip);
075          rootFromNodes.add(rootRelationShip.from);
076          rootToNodes.add(rootRelationShip.to);
077        }
078      }
079    
080      public int getWeight() {
081        return rootEdges.size();
082      }
083    
084      @Override
085      public boolean equals(Object obj) {
086        if (!(obj instanceof Edge)) {
087          return false;
088        }
089        Edge edge = (Edge) obj;
090        return from.equals(edge.from) && to.equals(edge.to);
091      }
092    
093      @Override
094      public int hashCode() {
095        return from.hashCode() + to.hashCode() + usage.hashCode();
096      }
097    }