Coverage Report - com.allanbank.mongodb.bson.element.NullElement
 
Classes in this File Line Coverage Branch Coverage Complexity
NullElement
100%
17/17
100%
2/2
1.25
 
 1  
 /*
 2  
  * #%L
 3  
  * NullElement.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.bson.element;
 21  
 
 22  
 import com.allanbank.mongodb.bson.Element;
 23  
 import com.allanbank.mongodb.bson.ElementType;
 24  
 import com.allanbank.mongodb.bson.Visitor;
 25  
 import com.allanbank.mongodb.bson.io.StringEncoder;
 26  
 
 27  
 /**
 28  
  * A wrapper for a BSON null.
 29  
  * 
 30  
  * @api.yes This class is part of the driver's API. Public and protected members
 31  
  *          will be deprecated for at least 1 non-bugfix release (version
 32  
  *          numbers are <major>.<minor>.<bugfix>) before being
 33  
  *          removed or modified.
 34  
  * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved
 35  
  */
 36  34
 public class NullElement extends AbstractElement {
 37  
 
 38  
     /** The BSON type for a binary. */
 39  1
     public static final ElementType TYPE = ElementType.NULL;
 40  
 
 41  
     /** Serialization version for the class. */
 42  
     private static final long serialVersionUID = -4974513577366947524L;
 43  
 
 44  
     /**
 45  
      * Computes and returns the number of bytes that are used to encode the
 46  
      * element.
 47  
      * 
 48  
      * @param name
 49  
      *            The name for the element.
 50  
      * @return The size of the element when encoded in bytes.
 51  
      */
 52  
     private static long computeSize(final String name) {
 53  122
         long result = 2; // type (1) + name null byte (1).
 54  122
         result += StringEncoder.utf8Size(name);
 55  
 
 56  122
         return result;
 57  
     }
 58  
 
 59  
     /**
 60  
      * Constructs a new {@link NullElement}.
 61  
      * 
 62  
      * @param name
 63  
      *            The name for the BSON null.
 64  
      * @throws IllegalArgumentException
 65  
      *             If the {@code name} is <code>null</code>.
 66  
      */
 67  
     public NullElement(final String name) {
 68  122
         this(name, computeSize(name));
 69  121
     }
 70  
 
 71  
     /**
 72  
      * Constructs a new {@link NullElement}.
 73  
      * 
 74  
      * @param name
 75  
      *            The name for the BSON null.
 76  
      * @param size
 77  
      *            The size of the element when encoded in bytes. If not known
 78  
      *            then use the {@link NullElement#NullElement(String)}
 79  
      *            constructor instead.
 80  
      * @throws IllegalArgumentException
 81  
      *             If the {@code name} is <code>null</code>.
 82  
      */
 83  
     public NullElement(final String name, final long size) {
 84  130
         super(name, size);
 85  129
     }
 86  
 
 87  
     /**
 88  
      * Accepts the visitor and calls the {@link Visitor#visitNull} method.
 89  
      * 
 90  
      * @see Element#accept(Visitor)
 91  
      */
 92  
     @Override
 93  
     public void accept(final Visitor visitor) {
 94  21
         visitor.visitNull(getName());
 95  21
     }
 96  
 
 97  
     /**
 98  
      * {@inheritDoc}
 99  
      */
 100  
     @Override
 101  
     public ElementType getType() {
 102  12
         return TYPE;
 103  
     }
 104  
 
 105  
     /**
 106  
      * {@inheritDoc}
 107  
      * <p>
 108  
      * Returns <code>null</code>.
 109  
      * </p>
 110  
      */
 111  
     @Override
 112  
     public Object getValueAsObject() {
 113  1
         return null;
 114  
     }
 115  
 
 116  
     /**
 117  
      * {@inheritDoc}
 118  
      * <p>
 119  
      * Returns the result of {@link String#valueOf(Object) String.valueOf(null)}
 120  
      * .
 121  
      * </p>
 122  
      */
 123  
     @Override
 124  
     public String getValueAsString() {
 125  1
         return String.valueOf((Object) null);
 126  
     }
 127  
 
 128  
     /**
 129  
      * {@inheritDoc}
 130  
      * <p>
 131  
      * Returns a new {@link NullElement}.
 132  
      * </p>
 133  
      */
 134  
     @Override
 135  
     public NullElement withName(final String name) {
 136  36
         if (getName().equals(name)) {
 137  34
             return this;
 138  
         }
 139  2
         return new NullElement(name);
 140  
     }
 141  
 }