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 */
020 package org.sonar.api.design;
021
022 import org.apache.commons.lang.builder.EqualsBuilder;
023 import org.apache.commons.lang.builder.HashCodeBuilder;
024 import org.apache.commons.lang.builder.ToStringBuilder;
025 import org.sonar.api.resources.Resource;
026 import org.sonar.graph.Edge;
027
028 public 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 public Resource getFrom() {
049 return from;
050 }
051
052 public Resource getTo() {
053 return to;
054 }
055
056 public String getUsage() {
057 return usage;
058 }
059
060 public Dependency setUsage(String usage) {
061 this.usage = usage;
062 return this;
063 }
064
065 public int getWeight() {
066 return weight;
067 }
068
069 public Dependency setWeight(int weight) {
070 this.weight = weight;
071 return this;
072 }
073
074 public Dependency getParent() {
075 return parent;
076 }
077
078 public Dependency setParent(Dependency parent) {
079 this.parent = parent;
080 return this;
081 }
082
083 public Long getId() {
084 return id;
085 }
086
087 /**
088 * Internal use only.
089 */
090 public Dependency setId(Long id) {
091 this.id = id;
092 return this;
093 }
094
095 @Override
096 public boolean equals(Object obj) {
097 if (!(obj instanceof Dependency)) {
098 return false;
099 }
100 if (this == obj) {
101 return true;
102 }
103 Dependency other = (Dependency) obj;
104 return new EqualsBuilder()
105 .append(from, other.from)
106 .append(to, other.to)
107 .isEquals();
108 }
109
110 @Override
111 public int hashCode() {
112 return new HashCodeBuilder(17, 37)
113 .append(from)
114 .append(to)
115 .toHashCode();
116 }
117
118 @Override
119 public String toString() {
120 return new ToStringBuilder(this)
121 .append("from", from)
122 .append("to", to)
123 .append("weight", weight)
124 .append("usage", usage)
125 .toString();
126 }
127 }