001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.design;
021
022import org.apache.commons.lang.builder.EqualsBuilder;
023import org.apache.commons.lang.builder.HashCodeBuilder;
024import org.apache.commons.lang.builder.ToStringBuilder;
025import org.sonar.api.resources.Resource;
026import org.sonar.graph.Edge;
027
028public class Dependency implements Edge<Resource> {
029
030  private Resource from;
031  private Resource to;
032  private String usage;
033  private int weight;
034  private Dependency parent;
035  private Long id;
036
037  public Dependency(Resource from, Resource to) {
038    if (from == null) {
039      throw new IllegalArgumentException("Dependency source is null");
040    }
041    if (to == null) {
042      throw new IllegalArgumentException("Dependency target is null");
043    }
044    this.from = from;
045    this.to = to;
046  }
047
048  @Override
049  public Resource getFrom() {
050    return from;
051  }
052
053  /**
054   * For internal use only
055   */
056  public void setFrom(Resource from) {
057    this.from = from;
058  }
059
060  @Override
061  public Resource getTo() {
062    return to;
063  }
064
065  /**
066   * For internal use only
067   */
068  public void setTo(Resource to) {
069    this.to = to;
070  }
071
072  public String getUsage() {
073    return usage;
074  }
075
076  public Dependency setUsage(String usage) {
077    this.usage = usage;
078    return this;
079  }
080
081  @Override
082  public int getWeight() {
083    return weight;
084  }
085
086  public Dependency setWeight(int weight) {
087    this.weight = weight;
088    return this;
089  }
090
091  public Dependency getParent() {
092    return parent;
093  }
094
095  public Dependency setParent(Dependency parent) {
096    this.parent = parent;
097    return this;
098  }
099
100  public Long getId() {
101    return id;
102  }
103
104  /**
105   * Internal use only.
106   */
107  public Dependency setId(Long id) {
108    this.id = id;
109    return this;
110  }
111
112  @Override
113  public boolean equals(Object obj) {
114    if (!(obj instanceof Dependency)) {
115      return false;
116    }
117    if (this == obj) {
118      return true;
119    }
120    Dependency other = (Dependency) obj;
121    return new EqualsBuilder()
122      .append(from, other.from)
123      .append(to, other.to)
124      .isEquals();
125  }
126
127  @Override
128  public int hashCode() {
129    return new HashCodeBuilder(17, 37)
130      .append(from)
131      .append(to)
132      .toHashCode();
133  }
134
135  @Override
136  public String toString() {
137    return new ToStringBuilder(this)
138      .append("from", from)
139      .append("to", to)
140      .append("weight", weight)
141      .append("usage", usage)
142      .toString();
143  }
144}