001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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.graph;
021
022 import java.util.List;
023
024 public class Cycle {
025
026 private Edge[] edges;
027 private int hashcode = 0;
028
029 public Cycle(List<Edge> edges) {
030 this.edges = edges.toArray(new Edge[edges.size()]);
031 for(Edge edge : edges) {
032 hashcode += edge.hashCode();
033 }
034 }
035
036 public int size() {
037 return edges.length;
038 }
039
040 public boolean contains(Edge e) {
041 for (Edge edge : edges) {
042 if (edge.equals(e)) {
043 return true;
044 }
045 }
046 return false;
047 }
048
049 public Edge[] getEdges() {
050 return edges;
051 }
052
053 @Override
054 public String toString() {
055 StringBuilder builder = new StringBuilder("Cycle with " + size() + " edges : ");
056 for (Edge edge : edges) {
057 builder.append(edge.getFrom()).append(" -> ");
058 }
059 return builder.toString();
060 }
061
062 @Override
063 public int hashCode() {
064 return hashcode;
065 }
066
067 @Override
068 public boolean equals(Object object) {
069 if (object instanceof Cycle) {
070 Cycle otherCycle = (Cycle) object;
071 if (otherCycle.hashcode == hashcode && otherCycle.edges.length == edges.length) {
072 mainLoop: for (Edge otherEdge : otherCycle.edges) {
073 for (Edge edge : edges) {
074 if (otherEdge.equals(edge)) {
075 continue mainLoop;
076 }
077 }
078 return false;
079 }
080 return true;
081 }
082 }
083 return false;
084 }
085 }