View Javadoc
1   /*
2    * #%L
3    * LetBuilder.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  
21  package com.allanbank.mongodb.builder.expression;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import com.allanbank.mongodb.bson.DocumentAssignable;
27  import com.allanbank.mongodb.bson.Element;
28  import com.allanbank.mongodb.bson.element.DocumentElement;
29  
30  /**
31   * LetBuilder provides a builder for a {@code $let} expression.
32   * 
33   * @api.yes This class is part of the driver's API. Public and protected members
34   *          will be deprecated for at least 1 non-bugfix release (version
35   *          numbers are <major>.<minor>.<bugfix>) before being
36   *          removed or modified.
37   * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved
38   */
39  public class LetBuilder {
40      /** The fields in the {@code $let} expression. */
41      private final List<Element> myFields;
42  
43      /**
44       * Creates a new MapStage2.
45       * 
46       * @param firstField
47       *            The name of the {@code input} field.
48       */
49      /* package */LetBuilder(final Element firstField) {
50          myFields = new ArrayList<Element>();
51          myFields.add(firstField);
52      }
53  
54      /**
55       * Creates the final {@code $let} expression to evaluate.
56       * 
57       * @param letExpression
58       *            The expression to be evaluated with the variables within the
59       *            {@code $let} expression.
60       * @return The {@link UnaryExpression} for the {@code $let}.
61       */
62      public UnaryExpression in(final Expression letExpression) {
63          return Expressions.let(myFields, letExpression);
64      }
65  
66      /**
67       * Adds a variable to the {@code $let} expression.
68       * 
69       * @param name
70       *            The name of the variable to set.
71       * @param document
72       *            The document to set the variable to.
73       * @return This builder for method call chaining.
74       */
75      public LetBuilder let(final String name, final DocumentAssignable document) {
76          myFields.add(new DocumentElement(name, document.asDocument()));
77          return this;
78      }
79  
80      /**
81       * Adds a variable to the {@code $let} expression.
82       * 
83       * @param name
84       *            The name of the variable to set.
85       * @param expression
86       *            The expression to compute the value for the variable.
87       * @return This builder for method call chaining.
88       */
89      public LetBuilder let(final String name, final Expression expression) {
90          myFields.add(expression.toElement(name));
91          return this;
92      }
93  }