From ec5c44dd34ba4e77d43a0f6b67c9326a38c243dc Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 23 Feb 2015 21:51:43 +0100 Subject: Fix quality flaws --- .../org/sonar/core/util/NonNullInputFunction.java | 44 ++++++++++++++++++ .../java/org/sonar/core/util/package-info.java | 24 ++++++++++ .../sonar/core/util/NonNullInputFunctionTest.java | 52 ++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 sonar-core/src/main/java/org/sonar/core/util/NonNullInputFunction.java create mode 100644 sonar-core/src/main/java/org/sonar/core/util/package-info.java create mode 100644 sonar-core/src/test/java/org/sonar/core/util/NonNullInputFunctionTest.java (limited to 'sonar-core') 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 implements Function { + + @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 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 { + @Override + protected Integer doApply(String input) { + return input.length(); + } + } +} -- cgit v1.2.3