diff options
author | Aurelien Poscia <aurelien.poscia@sonarsource.com> | 2023-12-08 13:35:05 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-22 20:03:02 +0000 |
commit | f1dfcc61c643d3c13be6b9743722ed36784569ab (patch) | |
tree | e57b4af1de208cf8cdde66759409eb9e263742d7 /server/sonar-webserver-common/src | |
parent | b0ebb7e25b4ced824865f0d0307629f93a422371 (diff) | |
download | sonarqube-f1dfcc61c643d3c13be6b9743722ed36784569ab.tar.gz sonarqube-f1dfcc61c643d3c13be6b9743722ed36784569ab.zip |
SONAR-21121 Add PATCH /dop-translation/gitlab-configurations/
Diffstat (limited to 'server/sonar-webserver-common/src')
2 files changed, 121 insertions, 0 deletions
diff --git a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/NonNullUpdatedValue.java b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/NonNullUpdatedValue.java new file mode 100644 index 00000000000..025426a6036 --- /dev/null +++ b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/NonNullUpdatedValue.java @@ -0,0 +1,41 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.server.common; + +import javax.annotation.Nullable; + +import static org.sonar.api.utils.Preconditions.checkArgument; + +public class NonNullUpdatedValue<T> extends UpdatedValue<T> { + + private NonNullUpdatedValue(@Nullable T value, boolean isDefined) { + super(value, isDefined); + } + + public static <T> NonNullUpdatedValue<T> withValueOrThrow(@Nullable T value) { + checkArgument(value != null, "Value must not be null"); + return new NonNullUpdatedValue<>(value, true); + } + + public static <T> NonNullUpdatedValue<T> undefined() { + return new NonNullUpdatedValue<>(null, false); + } + +} diff --git a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/UpdatedValue.java b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/UpdatedValue.java new file mode 100644 index 00000000000..7941773c378 --- /dev/null +++ b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/UpdatedValue.java @@ -0,0 +1,80 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.server.common; + +import java.util.Objects; +import java.util.function.Consumer; +import java.util.function.Function; +import javax.annotation.Nullable; + +public class UpdatedValue<T> { + final T value; + final boolean isDefined; + + UpdatedValue(@Nullable T value, boolean isDefined) { + this.value = value; + this.isDefined = isDefined; + } + + public static <T> UpdatedValue<T> withValue(@Nullable T value) { + return new UpdatedValue<>(value, true); + } + + public static <T> UpdatedValue<T> undefined() { + return new UpdatedValue<>(null, false); + } + + public <U> UpdatedValue<U> map(Function<T, U> mappingFunction) { + if (isDefined) { + return withValue(mappingFunction.apply(value)); + } + return undefined(); + } + + public void applyIfDefined(Consumer<T> consumer) { + if (isDefined) { + consumer.accept(value); + } + } + + public boolean contains(T testValue) { + if (isDefined && value != null) { + return value.equals(testValue); + } + return false; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + UpdatedValue<?> that = (UpdatedValue<?>) o; + return isDefined == that.isDefined && Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(value, isDefined); + } +} |