|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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 | +package org.springframework.graphql.data; |
| 17 | + |
| 18 | + |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import org.springframework.lang.Nullable; |
| 22 | +import org.springframework.util.ObjectUtils; |
| 23 | + |
| 24 | +/** |
| 25 | + * Simple container for the value from binding a GraphQL argument to a higher |
| 26 | + * level Object, along with a flag to indicate whether the input argument was |
| 27 | + * omitted altogether, as opposed to provided but set to the {@literal "null"} |
| 28 | + * literal. |
| 29 | + * |
| 30 | + * <p>Supported in one of the following places: |
| 31 | + * <ul> |
| 32 | + * <li>On a controller method parameter, either instead of |
| 33 | + * {@link org.springframework.graphql.data.method.annotation.Argument @Argument} |
| 34 | + * in which case the argument name is determined from the method parameter name, |
| 35 | + * or together with {@code @Argument} to specify the argument name. |
| 36 | + * <li>As a field within the object structure of an {@code @Argument} method |
| 37 | + * parameter, either initialized via a constructor argument or a setter, |
| 38 | + * including as a field of an object nested at any level below the top level |
| 39 | + * object. |
| 40 | + * </ul> |
| 41 | + * |
| 42 | + * @author Rossen Stoyanchev |
| 43 | + * @param <T> the type of value contained |
| 44 | + * @since 1.1 |
| 45 | + * @see <a href="http://spec.graphql.org/October2021/#sec-Non-Null.Nullable-vs-Optional">Nullable vs Optional</a> |
| 46 | + */ |
| 47 | +public final class ArgumentValue<T> { |
| 48 | + |
| 49 | + private static final ArgumentValue<?> OMITTED = new ArgumentValue<>(null, false); |
| 50 | + |
| 51 | + |
| 52 | + @Nullable |
| 53 | + private final T value; |
| 54 | + |
| 55 | + private final boolean omitted; |
| 56 | + |
| 57 | + |
| 58 | + private ArgumentValue(@Nullable T value, boolean omitted) { |
| 59 | + this.value = value; |
| 60 | + this.omitted = omitted; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Return {@code true} if a non-null value is present, and {@code false} otherwise. |
| 66 | + */ |
| 67 | + public boolean isPresent() { |
| 68 | + return (this.value != null); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Return {@code true} if the input value was omitted altogether from the |
| 73 | + * input, and {@code false} if it was provided, but possibly set to the |
| 74 | + * {@literal "null"} literal. |
| 75 | + */ |
| 76 | + public boolean isOmitted() { |
| 77 | + return this.omitted; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Return the contained value, or {@code null}. |
| 82 | + */ |
| 83 | + @Nullable |
| 84 | + public T value() { |
| 85 | + return this.value; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Return the contained value as a nullable {@link Optional}. |
| 90 | + */ |
| 91 | + public Optional<T> asOptional() { |
| 92 | + return Optional.ofNullable(this.value); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean equals(Object other) { |
| 97 | + // This covers OMITTED constant |
| 98 | + if (this == other) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + if (!(other instanceof ArgumentValue<?> otherValue)) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + return ObjectUtils.nullSafeEquals(this.value, otherValue.value); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public int hashCode() { |
| 109 | + int result = ObjectUtils.nullSafeHashCode(this.value); |
| 110 | + result = 31 * result + Boolean.hashCode(this.omitted); |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + /** |
| 116 | + * Static factory method for an argument value that was provided, even if |
| 117 | + * it was set to {@literal "null}. |
| 118 | + * @param value the value to hold in the instance |
| 119 | + */ |
| 120 | + public static <T> ArgumentValue<T> ofNullable(@Nullable T value) { |
| 121 | + return new ArgumentValue<>(value, false); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Static factory method for an argument value that was omitted. |
| 126 | + */ |
| 127 | + @SuppressWarnings("unchecked") |
| 128 | + public static <T> ArgumentValue<T> omitted() { |
| 129 | + return (ArgumentValue<T>) OMITTED; |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments