diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-13 11:54:50 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-14 14:26:45 +0200 |
commit | 6882924ef2d57618e9391c20b1a7a54546a7d9dd (patch) | |
tree | de6469b2b4aa3f9599995186317b037c715024c9 /sonar-plugin-api | |
parent | 95551739d6659af8c03250a708f075937e607456 (diff) | |
download | sonarqube-6882924ef2d57618e9391c20b1a7a54546a7d9dd.tar.gz sonarqube-6882924ef2d57618e9391c20b1a7a54546a7d9dd.zip |
SONAR-5389 Refactor dependency API
Diffstat (limited to 'sonar-plugin-api')
6 files changed, 235 insertions, 4 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index 76cab2f4790..831ea878114 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -22,6 +22,7 @@ package org.sonar.api.batch.sensor; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.rule.ActiveRules; +import org.sonar.api.batch.sensor.dependency.Dependency; import org.sonar.api.batch.sensor.duplication.DuplicationBuilder; import org.sonar.api.batch.sensor.duplication.DuplicationGroup; import org.sonar.api.batch.sensor.duplication.DuplicationTokenBuilder; @@ -111,7 +112,7 @@ public interface SensorContext { // ------------ TESTS ------------ /** - * Create a new test case for the given test file. + * Create a new test case. * Don't forget to call {@link TestCase#save()} once all parameters are provided. * @since 5.0 */ @@ -130,10 +131,10 @@ public interface SensorContext { // ------------ DEPENDENCIES ------------ /** - * Declare a dependency between 2 files. - * @param weight Weight of the dependency + * Create a new dependency. + * Don't forget to call {@link Dependency#save()} once all parameters are provided. * @since 5.0 */ - void saveDependency(InputFile from, InputFile to, int weight); + Dependency newDependency(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorStorage.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorStorage.java index 8298250523b..7faec27f4c6 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorStorage.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorStorage.java @@ -19,6 +19,7 @@ */ package org.sonar.api.batch.sensor; +import org.sonar.api.batch.sensor.dependency.Dependency; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.batch.sensor.measure.Measure; import org.sonar.api.batch.sensor.test.TestCase; @@ -35,4 +36,6 @@ public interface SensorStorage { void store(TestCase testCase); + void store(Dependency dependency); + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/Dependency.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/Dependency.java new file mode 100644 index 00000000000..87bd3029fbd --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/Dependency.java @@ -0,0 +1,54 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.batch.sensor.dependency; + +import com.google.common.annotations.Beta; +import org.sonar.api.batch.fs.InputFile; + +/** + * @since 5.0 + */ +@Beta +public interface Dependency { + + InputFile from(); + + Dependency from(InputFile from); + + InputFile to(); + + Dependency to(InputFile to); + + /** + * Default weight value is 1. + */ + int weight(); + + /** + * Set the weight of the dependency. + */ + Dependency weight(int weight); + + /** + * Save the dependency. + */ + void save(); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/internal/DefaultDependency.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/internal/DefaultDependency.java new file mode 100644 index 00000000000..34a91f80756 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/internal/DefaultDependency.java @@ -0,0 +1,131 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.batch.sensor.dependency.internal; + +import com.google.common.base.Preconditions; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang.builder.ToStringBuilder; +import org.apache.commons.lang.builder.ToStringStyle; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.sensor.SensorStorage; +import org.sonar.api.batch.sensor.dependency.Dependency; + +import javax.annotation.Nullable; + +public class DefaultDependency implements Dependency { + + private final SensorStorage storage; + private InputFile from; + private InputFile to; + private int weight = 1; + private boolean saved = false; + + public DefaultDependency() { + this.storage = null; + } + + public DefaultDependency(@Nullable SensorStorage storage) { + this.storage = storage; + } + + @Override + public Dependency from(InputFile from) { + Preconditions.checkNotNull(from, "InputFile should be non null"); + this.from = from; + return this; + } + + @Override + public Dependency to(InputFile to) { + Preconditions.checkNotNull(to, "InputFile should be non null"); + this.to = to; + return this; + } + + @Override + public Dependency weight(int weight) { + Preconditions.checkArgument(weight > 1, "weight should be greater than 1"); + this.weight = weight; + return this; + } + + @Override + public void save() { + Preconditions.checkNotNull(this.storage, "No persister on this object"); + Preconditions.checkState(!saved, "This dependency was already saved"); + Preconditions.checkState(!this.from.equals(this.to), "From and To can't be the same inputFile"); + Preconditions.checkNotNull(this.from, "From inputFile can't be null"); + Preconditions.checkNotNull(this.to, "To inputFile can't be null"); + storage.store((Dependency) this); + this.saved = true; + } + + @Override + public InputFile from() { + return this.from; + } + + @Override + public InputFile to() { + return this.to; + } + + @Override + public int weight() { + return this.weight; + } + + // For testing purpose + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + if (obj.getClass() != getClass()) { + return false; + } + DefaultDependency rhs = (DefaultDependency) obj; + return new EqualsBuilder() + .append(from, rhs.from) + .append(to, rhs.to) + .append(weight, rhs.weight) + .isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(27, 45). + append(from). + append(to). + append(weight). + toHashCode(); + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/internal/package-info.java new file mode 100644 index 00000000000..d45dd459268 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/internal/package-info.java @@ -0,0 +1,21 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@javax.annotation.ParametersAreNonnullByDefault +package org.sonar.api.batch.sensor.dependency.internal; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/package-info.java new file mode 100644 index 00000000000..e42fabac294 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/dependency/package-info.java @@ -0,0 +1,21 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@javax.annotation.ParametersAreNonnullByDefault +package org.sonar.api.batch.sensor.dependency; |