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 */
020package org.sonar.graph;
021
022import org.apache.commons.lang.math.NumberUtils;
023
024public class FeedbackEdge implements Comparable<FeedbackEdge> {
025
026  private Edge edge;
027  private double relativeWeight;
028  private int occurences;
029  private final int hashcode;
030
031  public FeedbackEdge(Edge edge, int occurences) {
032    this.edge = edge;
033    this.hashcode = edge.hashCode();
034    this.occurences = occurences;
035    this.relativeWeight = (double) edge.getWeight() / occurences;
036  }
037
038  protected Edge getEdge() {
039    return edge;
040  }
041
042  protected int getWeight() {
043    return edge.getWeight();
044  }
045
046  protected double getRelativeWeight() {
047    return relativeWeight;
048  }
049
050  protected int getOccurences() {
051    return occurences;
052  }
053
054  public int compareTo(FeedbackEdge feedbackEdge) {
055    if (this.getRelativeWeight() < feedbackEdge.getRelativeWeight()) {
056      return -1;
057    }
058    if (NumberUtils.compare(this.getRelativeWeight(), feedbackEdge.getRelativeWeight())==0) {
059      return this.getEdge().getFrom().toString().compareTo(feedbackEdge.getEdge().getFrom().toString());
060    }
061    return 1;
062  }
063
064  @Override
065  public boolean equals(Object obj) {
066    if (!(obj instanceof FeedbackEdge) || this.hashCode() != obj.hashCode()) {
067      return false;
068    }
069    FeedbackEdge otherEdge = (FeedbackEdge) obj;
070    return edge.equals(otherEdge.edge);
071  }
072
073  @Override
074  public int hashCode() {
075    return hashcode;
076  }
077}