aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-02-23 21:51:43 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-02-23 21:51:43 +0100
commitec5c44dd34ba4e77d43a0f6b67c9326a38c243dc (patch)
treeab0007c9768c6f8f3550a8686e7c0d8742c7d4b5 /sonar-core
parent1b2791924ef45c4b6a09d405deb3b1cfd5076798 (diff)
downloadsonarqube-ec5c44dd34ba4e77d43a0f6b67c9326a38c243dc.tar.gz
sonarqube-ec5c44dd34ba4e77d43a0f6b67c9326a38c243dc.zip
Fix quality flaws
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java44
-rw-r--r--sonar-core/src/main/java/org/sonar/core/util/package-info.java24
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java52
3 files changed, 120 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java b/sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java
new file mode 100644
index 00000000000..bf312d68d25
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java
@@ -0,0 +1,44 @@
+/*
+ * 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.core.util;
+
+import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
+
+import javax.annotation.Nullable;
+
+/**
+ * Guava Function that does not accept null input elements
+ * @since 5.1
+ */
+public abstract class NonNullInputFunction<F,T> implements Function<F, T> {
+
+ @Override
+ public final T apply(@Nullable F input) {
+ Preconditions.checkNotNull(input, "Null inputs are not allowed in this function");
+ return doApply(input);
+ }
+
+ /**
+ * This method is the same as {@link #apply(Object)} except that the input argument
+ * is not marked as nullable
+ */
+ protected abstract T doApply(F input);
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/util/package-info.java b/sonar-core/src/main/java/org/sonar/core/util/package-info.java
new file mode 100644
index 00000000000..5f2ad77c297
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/util/package-info.java
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.core.util;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java b/sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java
new file mode 100644
index 00000000000..f7417d0ce48
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.core.util;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
+
+public class NonNullInputFunctionTest {
+
+ NonNullInputFunction<String, Integer> sut = new TestFunction();
+
+ @Test
+ public void fail_if_null_input() throws Exception {
+ try {
+ sut.apply(null);
+ fail();
+ } catch (NullPointerException e) {
+ assertThat(e).hasMessage("Null inputs are not allowed in this function");
+ }
+ }
+
+ @Test
+ public void apply() throws Exception {
+ assertThat(sut.apply("foo")).isEqualTo(3);
+ }
+
+ private static class TestFunction extends NonNullInputFunction<String,Integer> {
+ @Override
+ protected Integer doApply(String input) {
+ return input.length();
+ }
+ }
+}