1 /* 2 * #%L 3 * BooleanElement.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 boolean. 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 public class BooleanElement extends AbstractElement { 37 38 /** The BSON type for a Object Id. */ 39 public static final ElementType TYPE = ElementType.BOOLEAN; 40 41 /** Serialization version for the class. */ 42 private static final long serialVersionUID = -3534279865960686134L; 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 long result = 3; // type (1) + name null byte (1) + value (1). 54 result += StringEncoder.utf8Size(name); 55 56 return result; 57 } 58 59 /** The boolean value */ 60 private final boolean myValue; 61 62 /** 63 * Constructs a new {@link BooleanElement}. 64 * 65 * @param name 66 * The name for the BSON boolean. 67 * @param value 68 * The BSON boolean value. 69 * @throws IllegalArgumentException 70 * If the {@code name} is <code>null</code>. 71 */ 72 public BooleanElement(final String name, final boolean value) { 73 this(name, value, computeSize(name)); 74 } 75 76 /** 77 * Constructs a new {@link BooleanElement}. 78 * 79 * @param name 80 * The name for the BSON boolean. 81 * @param value 82 * The BSON boolean value. 83 * @param size 84 * The size of the element when encoded in bytes. If not known 85 * then use the 86 * {@link BooleanElement#BooleanElement(String, boolean)} 87 * constructor instead. 88 * @throws IllegalArgumentException 89 * If the {@code name} is <code>null</code>. 90 */ 91 public BooleanElement(final String name, final boolean value, 92 final long size) { 93 super(name, size); 94 myValue = value; 95 } 96 97 /** 98 * Accepts the visitor and calls the {@link Visitor#visitBoolean} method. 99 * 100 * @see Element#accept(Visitor) 101 */ 102 @Override 103 public void accept(final Visitor visitor) { 104 visitor.visitBoolean(getName(), getValue()); 105 } 106 107 /** 108 * {@inheritDoc} 109 * <p> 110 * Overridden to compare the values if the base class comparison is equals. 111 * False is less than true. 112 * </p> 113 */ 114 @Override 115 public int compareTo(final Element otherElement) { 116 int result = super.compareTo(otherElement); 117 118 if (result == 0) { 119 final BooleanElement other = (BooleanElement) otherElement; 120 121 final int value = myValue ? 1 : 0; 122 final int otherValue = other.myValue ? 1 : 0; 123 124 result = value - otherValue; 125 } 126 127 return result; 128 } 129 130 /** 131 * Determines if the passed object is of this same type as this object and 132 * if so that its fields are equal. 133 * 134 * @param object 135 * The object to compare to. 136 * 137 * @see java.lang.Object#equals(java.lang.Object) 138 */ 139 @Override 140 public boolean equals(final Object object) { 141 boolean result = false; 142 if (this == object) { 143 result = true; 144 } 145 else if ((object != null) && (getClass() == object.getClass())) { 146 final BooleanElement other = (BooleanElement) object; 147 148 result = super.equals(object) && (myValue == other.myValue); 149 } 150 return result; 151 } 152 153 /** 154 * {@inheritDoc} 155 */ 156 @Override 157 public ElementType getType() { 158 return TYPE; 159 } 160 161 /** 162 * Returns the BSON boolean value. 163 * 164 * @return The BSON boolean value. 165 */ 166 public boolean getValue() { 167 return myValue; 168 } 169 170 /** 171 * {@inheritDoc} 172 * <p> 173 * Returns a {@link Boolean}. 174 * </p> 175 */ 176 @Override 177 public Boolean getValueAsObject() { 178 return Boolean.valueOf(getValue()); 179 } 180 181 /** 182 * {@inheritDoc} 183 * <p> 184 * Returns "true" or "false". 185 * </p> 186 */ 187 @Override 188 public String getValueAsString() { 189 return Boolean.toString(myValue); 190 } 191 192 /** 193 * Computes a reasonable hash code. 194 * 195 * @return The hash code value. 196 */ 197 @Override 198 public int hashCode() { 199 return super.hashCode() + (myValue ? 31 : 11); 200 } 201 202 /** 203 * {@inheritDoc} 204 * <p> 205 * Returns a new {@link BooleanElement}. 206 * </p> 207 */ 208 @Override 209 public BooleanElement withName(final String name) { 210 if (getName().equals(name)) { 211 return this; 212 } 213 return new BooleanElement(name, myValue); 214 } 215 }