|
| 1 | +/* |
| 2 | + * Copyright 2015-2021 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 java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.bson.Document; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | +/** |
| 26 | + * An enum of supported {@link AggregationExpression}s in aggregation pipeline stages. |
| 27 | + * |
| 28 | + * @author Thomas Darimont |
| 29 | + * @author Oliver Gierke |
| 30 | + * @author Christoph Strobl |
| 31 | + * @author Mark Paluch |
| 32 | + * @since 1.7 |
| 33 | + * @deprecated since 1.10. Please use {@link ArithmeticOperators} and {@link ComparisonOperators} instead. |
| 34 | + */ |
| 35 | +@Deprecated |
| 36 | +public enum AggregationFunctionExpressions { |
| 37 | + |
| 38 | + SIZE, CMP, EQ, GT, GTE, LT, LTE, NE, SUBTRACT, ADD, MULTIPLY; |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns an {@link AggregationExpression} build from the current {@link Enum} name and the given parameters. |
| 42 | + * |
| 43 | + * @param parameters must not be {@literal null} |
| 44 | + * @return new instance of {@link AggregationExpression}. |
| 45 | + */ |
| 46 | + public AggregationExpression of(Object... parameters) { |
| 47 | + |
| 48 | + Assert.notNull(parameters, "Parameters must not be null!"); |
| 49 | + return new FunctionExpression(name().toLowerCase(), parameters); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * An {@link AggregationExpression} representing a function call. |
| 54 | + * |
| 55 | + * @author Thomas Darimont |
| 56 | + * @author Oliver Gierke |
| 57 | + * @since 1.7 |
| 58 | + */ |
| 59 | + static class FunctionExpression implements AggregationExpression { |
| 60 | + |
| 61 | + private final String name; |
| 62 | + private final List<Object> values; |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates a new {@link FunctionExpression} for the given name and values. |
| 66 | + * |
| 67 | + * @param name must not be {@literal null} or empty. |
| 68 | + * @param values must not be {@literal null}. |
| 69 | + */ |
| 70 | + public FunctionExpression(String name, Object[] values) { |
| 71 | + |
| 72 | + Assert.hasText(name, "Name must not be null!"); |
| 73 | + Assert.notNull(values, "Values must not be null!"); |
| 74 | + |
| 75 | + this.name = name; |
| 76 | + this.values = Arrays.asList(values); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Document toDocument(AggregationOperationContext context) { |
| 81 | + |
| 82 | + List<Object> args = new ArrayList<Object>(values.size()); |
| 83 | + |
| 84 | + for (Object value : values) { |
| 85 | + args.add(unpack(value, context)); |
| 86 | + } |
| 87 | + |
| 88 | + return new Document("$" + name, args); |
| 89 | + } |
| 90 | + |
| 91 | + private static Object unpack(Object value, AggregationOperationContext context) { |
| 92 | + |
| 93 | + if (value instanceof AggregationExpression) { |
| 94 | + return ((AggregationExpression) value).toDocument(context); |
| 95 | + } |
| 96 | + |
| 97 | + if (value instanceof Field) { |
| 98 | + return context.getReference((Field) value).toString(); |
| 99 | + } |
| 100 | + |
| 101 | + return value; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments