2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.api.batch.sensor.dependency.internal;
22 import com.google.common.base.Preconditions;
23 import org.apache.commons.lang.builder.EqualsBuilder;
24 import org.apache.commons.lang.builder.HashCodeBuilder;
25 import org.sonar.api.batch.fs.InputFile;
26 import org.sonar.api.batch.fs.internal.DefaultInputFile;
27 import org.sonar.api.batch.sensor.SensorStorage;
28 import org.sonar.api.batch.sensor.dependency.Dependency;
29 import org.sonar.api.batch.sensor.dependency.NewDependency;
30 import org.sonar.api.batch.sensor.internal.DefaultStorable;
32 import javax.annotation.Nullable;
34 public class DefaultDependency extends DefaultStorable implements Dependency, NewDependency {
36 private String fromKey;
38 private int weight = 1;
40 public DefaultDependency() {
44 public DefaultDependency(@Nullable SensorStorage storage) {
49 public DefaultDependency from(InputFile from) {
50 Preconditions.checkNotNull(from, "InputFile should be non null");
51 this.fromKey = ((DefaultInputFile) from).key();
56 public DefaultDependency to(InputFile to) {
57 Preconditions.checkNotNull(to, "InputFile should be non null");
58 this.toKey = ((DefaultInputFile) to).key();
63 public DefaultDependency weight(int weight) {
64 Preconditions.checkArgument(weight > 1, "weight should be greater than 1");
70 public void doSave() {
71 Preconditions.checkState(!this.fromKey.equals(this.toKey), "From and To can't be the same inputFile");
72 Preconditions.checkNotNull(this.fromKey, "From inputFile can't be null");
73 Preconditions.checkNotNull(this.toKey, "To inputFile can't be null");
74 storage.store((Dependency) this);
78 public String fromKey() {
82 public DefaultDependency setFromKey(String fromKey) {
83 this.fromKey = fromKey;
88 public String toKey() {
92 public DefaultDependency setToKey(String toKey) {
102 public DefaultDependency setWeight(int weight) {
103 this.weight = weight;
107 // For testing purpose
110 public boolean equals(Object obj) {
117 if (obj.getClass() != getClass()) {
120 DefaultDependency rhs = (DefaultDependency) obj;
121 return new EqualsBuilder()
122 .append(fromKey, rhs.fromKey)
123 .append(toKey, rhs.toKey)
124 .append(weight, rhs.weight)
129 public int hashCode() {
130 return new HashCodeBuilder(27, 45).