Coverage Report - com.allanbank.mongodb.builder.AggregationGroupId
 
Classes in this File Line Coverage Branch Coverage Complexity
AggregationGroupId
100%
12/12
100%
2/2
1.4
AggregationGroupId$Builder
100%
11/11
100%
4/4
1.4
 
 1  
 /*
 2  
  * #%L
 3  
  * AggregationGroupId.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;
 22  
 
 23  
 import com.allanbank.mongodb.bson.DocumentAssignable;
 24  
 import com.allanbank.mongodb.bson.Element;
 25  
 import com.allanbank.mongodb.bson.builder.impl.AbstractBuilder;
 26  
 import com.allanbank.mongodb.bson.builder.impl.DocumentBuilderImpl;
 27  
 import com.allanbank.mongodb.bson.element.DocumentElement;
 28  
 import com.allanbank.mongodb.bson.element.StringElement;
 29  
 
 30  
 /**
 31  
  * AggregationGroupId holds the information for the <tt>_id</tt> field in an
 32  
  * aggregate command's
 33  
  * {@link Aggregate.Builder#group(AggregationGroupId, AggregationGroupField...)
 34  
  * $group} pipeline operator.
 35  
  * 
 36  
  * @see <a href=
 37  
  *      "http://docs.mongodb.org/manual/reference/aggregation/#_S_group">MongoDB
 38  
  *      Aggregate Command $group Operator</a>
 39  
  * @api.yes This class is part of the driver's API. Public and protected members
 40  
  *          will be deprecated for at least 1 non-bugfix release (version
 41  
  *          numbers are &lt;major&gt;.&lt;minor&gt;.&lt;bugfix&gt;) before being
 42  
  *          removed or modified.
 43  
  * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved
 44  
  */
 45  
 public class AggregationGroupId {
 46  
 
 47  
     /**
 48  
      * Constructs a {@link AggregationGroupId} with a constant value.
 49  
      * 
 50  
      * @param value
 51  
      *            The value of the _id.
 52  
      * @return The AggregationGroupId with a constant value.
 53  
      * @see <a href=
 54  
      *      "http://docs.mongodb.org/manual/reference/aggregation/#_S_group">MongoDB
 55  
      *      Aggregate Command $group Operator</a>
 56  
      */
 57  
     public static AggregationGroupId constantId(final String value) {
 58  1
         return new AggregationGroupId(value);
 59  
     }
 60  
 
 61  
     /**
 62  
      * Creates a builder to construct a complex _id value for the group.
 63  
      * 
 64  
      * @return The builder for the aggregation $group's id.
 65  
      * @see <a href=
 66  
      *      "http://docs.mongodb.org/manual/reference/aggregation/#_S_group">MongoDB
 67  
      *      Aggregate Command $group Operator</a>
 68  
      */
 69  
     public static AggregationGroupId.Builder id() {
 70  5
         return new AggregationGroupId.Builder();
 71  
     }
 72  
 
 73  
     /**
 74  
      * Constructs a {@link AggregationGroupId} with a value from a single field
 75  
      * in the source documents.
 76  
      * 
 77  
      * @param fieldRef
 78  
      *            The field name in the source documents to use in constructing
 79  
      *            the _id of the group'd documents. If the <tt>fieldRef</tt>
 80  
      *            does not start with a '$' then one will be added.
 81  
      * @return The AggregationGroupId with a single field value reference (which
 82  
      *         may resolve to a complex sub-document).
 83  
      * @see <a href=
 84  
      *      "http://docs.mongodb.org/manual/reference/aggregation/#_S_group">MongoDB
 85  
      *      Aggregate Command $group Operator</a>
 86  
      */
 87  
     public static AggregationGroupId id(final String fieldRef) {
 88  4
         if (!fieldRef.startsWith("$")) {
 89  3
             return new AggregationGroupId("$" + fieldRef);
 90  
         }
 91  
 
 92  1
         return new AggregationGroupId(fieldRef);
 93  
     }
 94  
 
 95  
     /** The id element. */
 96  
     private final Element myIdElement;
 97  
 
 98  
     /**
 99  
      * Creates a new AggregationGroupId.
 100  
      * 
 101  
      * @param builder
 102  
      *            The builder containing the details of the id.
 103  
      */
 104  5
     public AggregationGroupId(final DocumentAssignable builder) {
 105  5
         myIdElement = new DocumentElement("_id", builder.asDocument());
 106  5
     }
 107  
 
 108  
     /**
 109  
      * Creates a new AggregationGroupId.
 110  
      * 
 111  
      * @param value
 112  
      *            The value for the simple group id.
 113  
      */
 114  5
     public AggregationGroupId(final String value) {
 115  5
         myIdElement = new StringElement("_id", value);
 116  5
     }
 117  
 
 118  
     /**
 119  
      * Returns the element for the group operator's id.
 120  
      * 
 121  
      * @return The element for the group operator's id.
 122  
      */
 123  
     public Element toElement() {
 124  10
         return myIdElement;
 125  
     }
 126  
 
 127  
     /**
 128  
      * Builder provides the ability to construct a complex
 129  
      * {@link AggregationGroupId}.
 130  
      * <p>
 131  
      * This {@link Builder} also implements the
 132  
      * {@link com.allanbank.mongodb.bson.builder.DocumentBuilder} interface to
 133  
      * allow the construction of arbitrarily complex id documents.
 134  
      * </p>
 135  
      * 
 136  
      * @api.yes This class is part of the driver's API. Public and protected
 137  
      *          members will be deprecated for at least 1 non-bugfix release
 138  
      *          (version numbers are &lt;major&gt;.&lt;minor&gt;.&lt;bugfix&gt;)
 139  
      *          before being removed or modified.
 140  
      * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved
 141  
      */
 142  
     public static class Builder extends DocumentBuilderImpl {
 143  
 
 144  
         /**
 145  
          * Creates a new Builder.
 146  
          */
 147  
         public Builder() {
 148  5
             super((AbstractBuilder) null);
 149  5
         }
 150  
 
 151  
         /**
 152  
          * Adds a field reference to the id document. The output field name is
 153  
          * the same as the input field.
 154  
          * <p>
 155  
          * This method is equivalent to {@link #addField(String, String)
 156  
          * addField(&lt;fieldRef&gt;, &lt;fieldRef&gt;)} with appropriate
 157  
          * handling for the '$' prefix.
 158  
          * </p>
 159  
          * 
 160  
          * @param fieldRef
 161  
          *            The dotted field path for the field to use. If the
 162  
          *            <tt>fieldRef</tt> does not start with a '$' then one will
 163  
          *            be added.
 164  
          * @return This builder for chaining method calls.
 165  
          */
 166  
         public Builder addField(final String fieldRef) {
 167  6
             if (!fieldRef.startsWith("$")) {
 168  5
                 add(new StringElement(fieldRef, "$" + fieldRef));
 169  
             }
 170  
             else {
 171  1
                 add(new StringElement(fieldRef.substring(1), fieldRef));
 172  
             }
 173  6
             return this;
 174  
         }
 175  
 
 176  
         /**
 177  
          * Adds a field reference to the id document.
 178  
          * 
 179  
          * @param name
 180  
          *            The name of the field in the id document.
 181  
          * @param fieldRef
 182  
          *            The dotted field path for the field to use. If the
 183  
          *            <tt>fieldRef</tt> does not start with a '$' then one will
 184  
          *            be added.
 185  
          * @return This builder for chaining method calls.
 186  
          */
 187  
         public Builder addField(final String name, final String fieldRef) {
 188  4
             if (!fieldRef.startsWith("$")) {
 189  3
                 add(new StringElement(name, "$" + fieldRef));
 190  
             }
 191  
             else {
 192  1
                 add(new StringElement(name, fieldRef));
 193  
             }
 194  4
             return this;
 195  
         }
 196  
 
 197  
         /**
 198  
          * Constructs a new {@link AggregationGroupId} object from the state of
 199  
          * the builder.
 200  
          * 
 201  
          * @return The new {@link AggregationGroupId} object.
 202  
          */
 203  
         public AggregationGroupId buildId() {
 204  4
             return new AggregationGroupId(this);
 205  
         }
 206  
     }
 207  
 }