View Javadoc
1   /*
2    * #%L
3    * UnaryExpression.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  package com.allanbank.mongodb.builder.expression;
21  
22  import java.io.StringWriter;
23  
24  import com.allanbank.mongodb.bson.Element;
25  import com.allanbank.mongodb.bson.ElementAssignable;
26  import com.allanbank.mongodb.bson.element.DocumentElement;
27  import com.allanbank.mongodb.bson.element.JsonSerializationVisitor;
28  
29  /**
30   * UnaryExpression provides an implementation of an {@link Expression} with 1
31   * operand.
32   * 
33   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
34   *         mutated in incompatible ways between any two releases of the driver.
35   * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved
36   */
37  public class UnaryExpression implements Expression, ElementAssignable {
38  
39      /** The sub expression. */
40      protected final Expression myExpression;
41  
42      /** The operator. */
43      protected final String myOperator;
44  
45      /**
46       * Creates a new NaryExpression.
47       * 
48       * @param operator
49       *            The operator this object represents.
50       * @param expression
51       *            The sub expression.
52       */
53      public UnaryExpression(final String operator, final Expression expression) {
54          myOperator = operator;
55          myExpression = expression;
56      }
57  
58      /**
59       * {@inheritDoc}
60       * <p>
61       * Overridden to return the sub expressions as an element:
62       * </p>
63       * <blockquote>
64       * 
65       * <pre>
66       * <code>
67       * "$op" : expression
68       * </code>
69       * </pre>
70       * 
71       * </blockquote>
72       */
73      @Override
74      public Element asElement() {
75          return myExpression.toElement(myOperator);
76      }
77  
78      /**
79       * {@inheritDoc}
80       * <p>
81       * Overridden to return the sub expressions as a document with a nested
82       * array element:
83       * </p>
84       * <blockquote>
85       * 
86       * <pre>
87       * <code>
88       * { &lt;name&gt; : { "$op" : expression } }
89       * </code>
90       * </pre>
91       * 
92       * </blockquote>
93       */
94      @Override
95      public DocumentElement toElement(final String name) {
96          return new DocumentElement(name, myExpression.toElement(myOperator));
97      }
98  
99      /**
100      * {@inheritDoc}
101      * <p>
102      * Overridden to return the expression in JSON format.
103      * </p>
104      * <blockquote>
105      * 
106      * <pre>
107      * <code>
108      * "$op" : expression
109      * </code>
110      * </pre>
111      * 
112      * </blockquote>
113      */
114     @Override
115     public String toString() {
116         final StringWriter sink = new StringWriter();
117         final JsonSerializationVisitor json = new JsonSerializationVisitor(
118                 sink, true);
119 
120         asElement().accept(json);
121 
122         return sink.toString();
123     }
124 }