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