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