|
| 1 | +/* |
| 2 | + * Copyright 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.data.mongodb.core.aggregation; |
| 17 | + |
| 18 | +import org.springframework.lang.Nullable; |
| 19 | +import org.springframework.util.Assert; |
| 20 | +import org.springframework.util.ObjectUtils; |
| 21 | + |
| 22 | +/** |
| 23 | + * A special field that points to a variable {@code $$} expression. |
| 24 | + * |
| 25 | + * @author Christoph Strobl |
| 26 | + * @since 4.1 |
| 27 | + */ |
| 28 | +public interface AggregationVariable extends Field { |
| 29 | + |
| 30 | + String PREFIX = "$$"; |
| 31 | + |
| 32 | + /** |
| 33 | + * @return {@literal true} if the fields {@link #getName() name} does not match the defined {@link #getTarget() |
| 34 | + * target}. |
| 35 | + */ |
| 36 | + default boolean isAliased() { |
| 37 | + return !ObjectUtils.nullSafeEquals(getName(), getTarget()); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + default String getName() { |
| 42 | + return getTarget(); |
| 43 | + } |
| 44 | + |
| 45 | + default boolean isInternal() { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Create a new {@link AggregationVariable} for the given name. |
| 51 | + * <p> |
| 52 | + * Variables start with {@code $$}. If not, the given value gets prefixed with {@code $$}. |
| 53 | + * |
| 54 | + * @param value must not be {@literal null}. |
| 55 | + * @return new instance of {@link AggregationVariable}. |
| 56 | + * @throws IllegalArgumentException if given value is {@literal null}. |
| 57 | + */ |
| 58 | + static AggregationVariable variable(String value) { |
| 59 | + |
| 60 | + Assert.notNull(value, "Value must not be null"); |
| 61 | + return new AggregationVariable() { |
| 62 | + |
| 63 | + private final String val = AggregationVariable.prefixVariable(value); |
| 64 | + |
| 65 | + @Override |
| 66 | + public String getTarget() { |
| 67 | + return val; |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Create a new {@link #isInternal() local} {@link AggregationVariable} for the given name. |
| 74 | + * <p> |
| 75 | + * Variables start with {@code $$}. If not, the given value gets prefixed with {@code $$}. |
| 76 | + * |
| 77 | + * @param value must not be {@literal null}. |
| 78 | + * @return new instance of {@link AggregationVariable}. |
| 79 | + * @throws IllegalArgumentException if given value is {@literal null}. |
| 80 | + */ |
| 81 | + static AggregationVariable localVariable(String value) { |
| 82 | + |
| 83 | + Assert.notNull(value, "Value must not be null"); |
| 84 | + return new AggregationVariable() { |
| 85 | + |
| 86 | + private final String val = AggregationVariable.prefixVariable(value); |
| 87 | + |
| 88 | + @Override |
| 89 | + public String getTarget() { |
| 90 | + return val; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean isInternal() { |
| 95 | + return true; |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Check if the given field name reference may be variable. |
| 102 | + * |
| 103 | + * @param fieldRef can be {@literal null}. |
| 104 | + * @return true if given value matches the variable identification pattern. |
| 105 | + */ |
| 106 | + static boolean isVariable(@Nullable String fieldRef) { |
| 107 | + return fieldRef != null && fieldRef.stripLeading().matches("^\\$\\$\\w.*"); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Check if the given field may be variable. |
| 112 | + * |
| 113 | + * @param field can be {@literal null}. |
| 114 | + * @return true if given {@link Field field} is an {@link AggregationVariable} or if its value is a |
| 115 | + * {@link #isVariable(String) variable}. |
| 116 | + */ |
| 117 | + static boolean isVariable(Field field) { |
| 118 | + |
| 119 | + if (field instanceof AggregationVariable) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + return isVariable(field.getTarget()); |
| 123 | + } |
| 124 | + |
| 125 | + private static String prefixVariable(String variable) { |
| 126 | + |
| 127 | + var trimmed = variable.stripLeading(); |
| 128 | + return trimmed.startsWith(PREFIX) ? trimmed : (PREFIX + trimmed); |
| 129 | + } |
| 130 | +} |
0 commit comments