--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.validation.valueextraction.UnwrapByDefault;
+import org.sonar.server.common.NonNullUpdatedValue;
+import org.sonar.server.common.UpdatedValue;
@UnwrapByDefault
public class UpdateField<T> {
return undefined();
}
+ public UpdatedValue<T> toUpdatedValue() {
+ return isDefined ? UpdatedValue.withValue(value) : UpdatedValue.undefined();
+ }
+ public NonNullUpdatedValue<T> toNonNullUpdatedValue() {
+ return isDefined ? NonNullUpdatedValue.withValueOrThrow(value) : NonNullUpdatedValue.undefined();
+ }
+
@CheckForNull
public T orElse(@Nullable T other) {
return isDefined ? value : other;