|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.graphql; |
| 18 | + |
| 19 | + |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.function.Consumer; |
| 22 | + |
| 23 | +import org.springframework.lang.Nullable; |
| 24 | +import org.springframework.util.Assert; |
| 25 | +import org.springframework.util.ObjectUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Simple container for GraphQL field values that indicates if the field |
| 29 | + * value is present, provided but set to {@literal "null"} or omitted altogether. |
| 30 | + * |
| 31 | + * <p>In the case of GraphQL mutations, clients send an Input Object with several fields; |
| 32 | + * the server must understand the difference between a field being absent |
| 33 | + * (the existing value should be left as-is) and a field set to {@literal "null"} |
| 34 | + * (the existing value must be set to {@literal "null"}). {@code FieldValue<T>} |
| 35 | + * helps to make this distinction. |
| 36 | + * |
| 37 | + * <p>Supported in one of the following places: |
| 38 | + * <ul> |
| 39 | + * <li>On a controller method parameter, either instead of |
| 40 | + * {@link org.springframework.graphql.data.method.annotation.Argument @Argument} |
| 41 | + * in which case the argument name is determined from the method parameter name, |
| 42 | + * or together with {@code @Argument} to specify the argument name. |
| 43 | + * <li>As a field within the object structure of an {@code @Argument} method |
| 44 | + * parameter, either initialized via a constructor argument or a setter, |
| 45 | + * including as a field of an object nested at any level below the top level |
| 46 | + * object. |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * @param <T> the type of value contained |
| 50 | + * @author Rossen Stoyanchev |
| 51 | + * @author Brian Clozel |
| 52 | + * @since 1.4.0 |
| 53 | + * @see <a href="http://spec.graphql.org/October2021/#sec-Input-Objects">Input Object</a> |
| 54 | + * @see <a href="http://spec.graphql.org/October2021/#sec-Non-Null.Nullable-vs-Optional">Nullable vs Optional</a> |
| 55 | + */ |
| 56 | +public final class FieldValue<T> { |
| 57 | + |
| 58 | + private static final FieldValue<?> EMPTY = new FieldValue<>(null, false); |
| 59 | + |
| 60 | + private static final FieldValue<?> OMITTED = new FieldValue<>(null, true); |
| 61 | + |
| 62 | + |
| 63 | + @Nullable |
| 64 | + private final T value; |
| 65 | + |
| 66 | + private final boolean omitted; |
| 67 | + |
| 68 | + |
| 69 | + private FieldValue(@Nullable T value, boolean omitted) { |
| 70 | + this.value = value; |
| 71 | + this.omitted = omitted; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * Return {@code true} if a non-null value is present, and {@code false} otherwise. |
| 77 | + */ |
| 78 | + public boolean isPresent() { |
| 79 | + return (this.value != null); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Return {@code true} if the input value was present in the input but the value was {@code null}, |
| 84 | + * and {@code false} otherwise. |
| 85 | + */ |
| 86 | + public boolean isEmpty() { |
| 87 | + return !this.omitted && this.value == null; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Return {@code true} if the input value was omitted altogether from the |
| 92 | + * input, and {@code false} if it was provided, but possibly set to the |
| 93 | + * {@literal "null"} literal. |
| 94 | + */ |
| 95 | + public boolean isOmitted() { |
| 96 | + return this.omitted; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return the contained value, or {@code null}. |
| 101 | + */ |
| 102 | + @Nullable |
| 103 | + public T value() { |
| 104 | + return this.value; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Return the contained value as a nullable {@link Optional}. |
| 109 | + */ |
| 110 | + public Optional<T> asOptional() { |
| 111 | + return Optional.ofNullable(this.value); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * If a value is present, performs the given action with the value, otherwise does nothing. |
| 116 | + * @param action the action to be performed, if a value is present |
| 117 | + */ |
| 118 | + public void ifPresent(Consumer<? super T> action) { |
| 119 | + Assert.notNull(action, "Action is required"); |
| 120 | + if (this.value != null) { |
| 121 | + action.accept(this.value); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean equals(Object other) { |
| 127 | + // This covers EMPTY and OMITTED constant |
| 128 | + if (this == other) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + if (!(other instanceof FieldValue<?> otherValue)) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + return ObjectUtils.nullSafeEquals(this.value, otherValue.value); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public int hashCode() { |
| 139 | + int result = ObjectUtils.nullSafeHashCode(this.value); |
| 140 | + result = 31 * result + Boolean.hashCode(this.omitted); |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String toString() { |
| 146 | + if (this.isOmitted()) { |
| 147 | + return "FieldValue{omitted}"; |
| 148 | + } |
| 149 | + return "FieldValue{value=" + this.value + "'}'"; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Static factory method for an argument value that was provided, even if |
| 154 | + * it was set to {@literal "null}. |
| 155 | + * @param <T> the type of value |
| 156 | + * @param value the value to hold in the instance |
| 157 | + */ |
| 158 | + @SuppressWarnings("unchecked") |
| 159 | + public static <T> FieldValue<T> ofNullable(@Nullable T value) { |
| 160 | + return (value != null) ? new FieldValue<>(value, false) : (FieldValue<T>) EMPTY; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Static factory method for an argument value that was omitted. |
| 165 | + * @param <T> the type of value |
| 166 | + */ |
| 167 | + @SuppressWarnings("unchecked") |
| 168 | + public static <T> FieldValue<T> omitted() { |
| 169 | + return (FieldValue<T>) OMITTED; |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments