001/* 002 * Copyright 2015-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2015-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2015-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util.json; 037 038 039 040import com.unboundid.util.NotMutable; 041import com.unboundid.util.ThreadSafety; 042import com.unboundid.util.ThreadSafetyLevel; 043 044 045 046/** 047 * This class provides an implementation of a JSON value that represents a 048 * Java Boolean. The string representation of the JSON Boolean true value is 049 * {@code true}, and the string representation of the JSON Boolean false value 050 * is {@code false}. These values are not surrounded by quotation marks, and 051 * they must be entirely lowercase. 052 */ 053@NotMutable() 054@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 055public final class JSONBoolean 056 extends JSONValue 057{ 058 /** 059 * A pre-allocated object that represents a value of {@code false}. 060 */ 061 public static final JSONBoolean FALSE = new JSONBoolean(false); 062 063 064 065 /** 066 * A pre-allocated object that represents a value of {@code true}. 067 */ 068 public static final JSONBoolean TRUE = new JSONBoolean(true); 069 070 071 072 /** 073 * The serial version UID for this serializable class. 074 */ 075 private static final long serialVersionUID = -5090296701442873481L; 076 077 078 079 // The boolean value for this object. 080 private final boolean booleanValue; 081 082 // The string representation for this object. 083 private final String stringRepresentation; 084 085 086 087 /** 088 * Creates a new JSON value capable of representing a Boolean value of either 089 * {@code true} or {@code false}. 090 * 091 * @param booleanValue The Boolean value for this JSON value. 092 */ 093 public JSONBoolean(final boolean booleanValue) 094 { 095 this.booleanValue = booleanValue; 096 stringRepresentation = (booleanValue ? "true" : "false"); 097 } 098 099 100 101 /** 102 * Retrieves the Java boolean value for this JSON value. 103 * 104 * @return The Java boolean value for this JSON value. 105 */ 106 public boolean booleanValue() 107 { 108 return booleanValue; 109 } 110 111 112 113 /** 114 * {@inheritDoc} 115 */ 116 @Override() 117 public int hashCode() 118 { 119 return (booleanValue ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode()); 120 } 121 122 123 124 /** 125 * {@inheritDoc} 126 */ 127 @Override() 128 public boolean equals(final Object o) 129 { 130 if (o == this) 131 { 132 return true; 133 } 134 135 if (o instanceof JSONBoolean) 136 { 137 final JSONBoolean b = (JSONBoolean) o; 138 return (b.booleanValue == booleanValue); 139 } 140 141 return false; 142 } 143 144 145 146 /** 147 * {@inheritDoc} 148 */ 149 @Override() 150 public boolean equals(final JSONValue v, final boolean ignoreFieldNameCase, 151 final boolean ignoreValueCase, 152 final boolean ignoreArrayOrder) 153 { 154 return ((v instanceof JSONBoolean) && 155 (booleanValue == ((JSONBoolean) v).booleanValue)); 156 } 157 158 159 160 /** 161 * Retrieves a string representation of this Boolean value as it should appear 162 * in a JSON object. If the Boolean value is {@code true}, then the string 163 * representation will be "{@code true}" (without the surrounding quotes). If 164 * the Boolean value is {@code false}, then the string representation will be 165 * "{@code false}" (again, without the quotes). 166 * 167 * @return A string representation of this Boolean value as it should appear 168 * in a JSON object. 169 */ 170 @Override() 171 public String toString() 172 { 173 return stringRepresentation; 174 } 175 176 177 178 /** 179 * Appends a string representation of this Boolean value as it should appear 180 * in a JSON object to the provided buffer. If the Boolean value is 181 * {@code true}, then the string representation will be "{@code true}" 182 * (without the surrounding quotes). If the Boolean value is {@code false}, 183 * then the string representation will be "{@code false}" (again, without the 184 * quotes). 185 * 186 * @param buffer The buffer to which the information should be appended. 187 */ 188 @Override() 189 public void toString(final StringBuilder buffer) 190 { 191 buffer.append(stringRepresentation); 192 } 193 194 195 196 /** 197 * Retrieves a single-line string representation of this Boolean value as it 198 * should appear in a JSON object. If the Boolean value is {@code true}, then 199 * the string representation will be "{@code true}" (without the surrounding 200 * quotes). If the Boolean value is {@code false}, then the string 201 * representation will be "{@code false}" (again, without the quotes). 202 * 203 * @return A single-line string representation of this Boolean value as it 204 * should appear in a JSON object. 205 */ 206 @Override() 207 public String toSingleLineString() 208 { 209 return stringRepresentation; 210 } 211 212 213 214 /** 215 * Appends a single-line string representation of this Boolean value as it 216 * should appear in a JSON object to the provided buffer. If the Boolean 217 * value is {@code true}, then the string representation will be 218 * "{@code true}" (without the surrounding quotes). If the Boolean value is 219 * {@code false}, then the string representation will be "{@code false}" 220 * (again, without the quotes). 221 * 222 * @param buffer The buffer to which the information should be appended. 223 */ 224 @Override() 225 public void toSingleLineString(final StringBuilder buffer) 226 { 227 buffer.append(stringRepresentation); 228 } 229 230 231 232 /** 233 * Retrieves a normalized string representation of this Boolean value as it 234 * should appear in a JSON object. If the Boolean value is {@code true}, then 235 * the string representation will be "{@code true}" (without the surrounding 236 * quotes). If the Boolean value is {@code false}, then the string 237 * representation will be "{@code false}" (again, without the quotes). 238 * 239 * @return A normalized string representation of this Boolean value as it 240 * should appear in a JSON object. 241 */ 242 @Override() 243 public String toNormalizedString() 244 { 245 return stringRepresentation; 246 } 247 248 249 250 /** 251 * Appends a normalized string representation of this Boolean value as it 252 * should appear in a JSON object to the provided buffer. If the Boolean 253 * value is {@code true}, then the string representation will be 254 * "{@code true}" (without the surrounding quotes). If the Boolean value is 255 * {@code false}, then the string representation will be "{@code false}" 256 * (again, without the quotes). 257 * 258 * @param buffer The buffer to which the information should be appended. 259 */ 260 @Override() 261 public void toNormalizedString(final StringBuilder buffer) 262 { 263 buffer.append(stringRepresentation); 264 } 265 266 267 268 /** 269 * Retrieves a normalized string representation of this Boolean value as it 270 * should appear in a JSON object. If the Boolean value is {@code true}, then 271 * the string representation will be "{@code true}" (without the surrounding 272 * quotes). If the Boolean value is {@code false}, then the string 273 * representation will be "{@code false}" (again, without the quotes). 274 * 275 * @param ignoreFieldNameCase Indicates whether field names should be 276 * treated in a case-sensitive (if {@code false}) 277 * or case-insensitive (if {@code true}) manner. 278 * @param ignoreValueCase Indicates whether string field values should 279 * be treated in a case-sensitive (if 280 * {@code false}) or case-insensitive (if 281 * {@code true}) manner. 282 * @param ignoreArrayOrder Indicates whether the order of elements in an 283 * array should be considered significant (if 284 * {@code false}) or insignificant (if 285 * {@code true}). 286 * 287 * @return A normalized string representation of this Boolean value as it 288 * should appear in a JSON object. 289 */ 290 @Override() 291 public String toNormalizedString(final boolean ignoreFieldNameCase, 292 final boolean ignoreValueCase, 293 final boolean ignoreArrayOrder) 294 { 295 return stringRepresentation; 296 } 297 298 299 300 /** 301 * Appends a normalized string representation of this Boolean value as it 302 * should appear in a JSON object to the provided buffer. If the Boolean 303 * value is {@code true}, then the string representation will be 304 * "{@code true}" (without the surrounding quotes). If the Boolean value is 305 * {@code false}, then the string representation will be "{@code false}" 306 * (again, without the quotes). 307 * 308 * @param buffer The buffer to which the information should be 309 * appended. 310 * @param ignoreFieldNameCase Indicates whether field names should be 311 * treated in a case-sensitive (if {@code false}) 312 * or case-insensitive (if {@code true}) manner. 313 * @param ignoreValueCase Indicates whether string field values should 314 * be treated in a case-sensitive (if 315 * {@code false}) or case-insensitive (if 316 * {@code true}) manner. 317 * @param ignoreArrayOrder Indicates whether the order of elements in an 318 * array should be considered significant (if 319 * {@code false}) or insignificant (if 320 * {@code true}). 321 */ 322 @Override() 323 public void toNormalizedString(final StringBuilder buffer, 324 final boolean ignoreFieldNameCase, 325 final boolean ignoreValueCase, 326 final boolean ignoreArrayOrder) 327 { 328 buffer.append(stringRepresentation); 329 } 330 331 332 333 /** 334 * {@inheritDoc} 335 */ 336 @Override() 337 public void appendToJSONBuffer(final JSONBuffer buffer) 338 { 339 buffer.appendBoolean(booleanValue); 340 } 341 342 343 344 /** 345 * {@inheritDoc} 346 */ 347 @Override() 348 public void appendToJSONBuffer(final String fieldName, 349 final JSONBuffer buffer) 350 { 351 buffer.appendBoolean(fieldName, booleanValue); 352 } 353}