001/*
002 * Copyright 2015-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2015-2024 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-2024 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.NotNull;
042import com.unboundid.util.Nullable;
043import com.unboundid.util.ThreadSafety;
044import com.unboundid.util.ThreadSafetyLevel;
045
046
047
048/**
049 * This class provides an implementation of a JSON value that represents a
050 * Java Boolean.  The string representation of the JSON Boolean true value is
051 * {@code true}, and the string representation of the JSON Boolean false value
052 * is {@code false}.  These values are not surrounded by quotation marks, and
053 * they must be entirely lowercase.
054 */
055@NotMutable()
056@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
057public final class JSONBoolean
058       extends JSONValue
059{
060  /**
061   * A pre-allocated object that represents a value of {@code false}.
062   */
063  @NotNull public static final JSONBoolean FALSE = new JSONBoolean(false);
064
065
066
067  /**
068   * A pre-allocated object that represents a value of {@code true}.
069   */
070  @NotNull public static final JSONBoolean TRUE = new JSONBoolean(true);
071
072
073
074  /**
075   * The serial version UID for this serializable class.
076   */
077  private static final long serialVersionUID = -5090296701442873481L;
078
079
080
081  // The boolean value for this object.
082  private final boolean booleanValue;
083
084  // The string representation for this object.
085  @NotNull private final String stringRepresentation;
086
087
088
089  /**
090   * Creates a new JSON value capable of representing a Boolean value of either
091   * {@code true} or {@code false}.
092   *
093   * @param  booleanValue  The Boolean value for this JSON value.
094   */
095  public JSONBoolean(final boolean booleanValue)
096  {
097    this.booleanValue = booleanValue;
098    stringRepresentation = (booleanValue ? "true" : "false");
099  }
100
101
102
103  /**
104   * Retrieves the Java boolean value for this JSON value.
105   *
106   * @return  The Java boolean value for this JSON value.
107   */
108  public boolean booleanValue()
109  {
110    return booleanValue;
111  }
112
113
114
115  /**
116   * {@inheritDoc}
117   */
118  @Override()
119  public int hashCode()
120  {
121    return (booleanValue ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode());
122  }
123
124
125
126  /**
127   * {@inheritDoc}
128   */
129  @Override()
130  public boolean equals(@Nullable final Object o)
131  {
132    if (o == this)
133    {
134      return true;
135    }
136
137    if (o instanceof JSONBoolean)
138    {
139      final JSONBoolean b = (JSONBoolean) o;
140      return (b.booleanValue == booleanValue);
141    }
142
143    return false;
144  }
145
146
147
148  /**
149   * {@inheritDoc}
150   */
151  @Override()
152  public boolean equals(@NotNull final JSONValue v,
153                        final boolean ignoreFieldNameCase,
154                        final boolean ignoreValueCase,
155                        final boolean ignoreArrayOrder)
156  {
157    return ((v instanceof JSONBoolean) &&
158         (booleanValue == ((JSONBoolean) v).booleanValue));
159  }
160
161
162
163  /**
164   * Retrieves a string representation of this Boolean value as it should appear
165   * in a JSON object.  If the Boolean value is {@code true}, then the string
166   * representation will be "{@code true}" (without the surrounding quotes).  If
167   * the Boolean value is {@code false}, then the string representation will be
168   * "{@code false}" (again, without the quotes).
169   *
170   * @return  A string representation of this Boolean value as it should appear
171   *          in a JSON object.
172   */
173  @Override()
174  @NotNull()
175  public String toString()
176  {
177    return stringRepresentation;
178  }
179
180
181
182  /**
183   * Appends a string representation of this Boolean value as it should appear
184   * in a JSON object to the provided buffer.  If the Boolean value is
185   * {@code true}, then the string representation will be "{@code true}"
186   * (without the surrounding quotes).  If the Boolean value is {@code false},
187   * then the string representation will be "{@code false}" (again, without the
188   * quotes).
189   *
190   * @param  buffer  The buffer to which the information should be appended.
191   */
192  @Override()
193  public void toString(@NotNull final StringBuilder buffer)
194  {
195    buffer.append(stringRepresentation);
196  }
197
198
199
200  /**
201   * Retrieves a single-line string representation of this Boolean value as it
202   * should appear in a JSON object.  If the Boolean value is {@code true}, then
203   * the string representation will be "{@code true}" (without the surrounding
204   * quotes).  If the Boolean value is {@code false}, then the string
205   * representation will be "{@code false}" (again, without the quotes).
206   *
207   * @return  A single-line string representation of this Boolean value as it
208   *          should appear in a JSON object.
209   */
210  @Override()
211  @NotNull()
212  public String toSingleLineString()
213  {
214    return stringRepresentation;
215  }
216
217
218
219  /**
220   * Appends a single-line string representation of this Boolean value as it
221   * should appear in a JSON object to the provided buffer.  If the Boolean
222   * value is {@code true}, then the string representation will be
223   * "{@code true}" (without the surrounding quotes).  If the Boolean value is
224   * {@code false}, then the string representation will be "{@code false}"
225   * (again, without the quotes).
226   *
227   * @param  buffer  The buffer to which the information should be appended.
228   */
229  @Override()
230  public void toSingleLineString(@NotNull final StringBuilder buffer)
231  {
232    buffer.append(stringRepresentation);
233  }
234
235
236
237  /**
238   * Retrieves a normalized string representation of this Boolean value as it
239   * should appear in a JSON object.  If the Boolean value is {@code true}, then
240   * the string representation will be "{@code true}" (without the surrounding
241   * quotes).  If the Boolean value is {@code false}, then the string
242   * representation will be "{@code false}" (again, without the quotes).
243   *
244   * @return  A normalized string representation of this Boolean value as it
245   *          should appear in a JSON object.
246   */
247  @Override()
248  @NotNull()
249  public String toNormalizedString()
250  {
251    return stringRepresentation;
252  }
253
254
255
256  /**
257   * Appends a normalized string representation of this Boolean value as it
258   * should appear in a JSON object to the provided buffer.  If the Boolean
259   * value is {@code true}, then the string representation will be
260   * "{@code true}" (without the surrounding quotes).  If the Boolean value is
261   * {@code false}, then the string representation will be "{@code false}"
262   * (again, without the quotes).
263   *
264   * @param  buffer  The buffer to which the information should be appended.
265   */
266  @Override()
267  public void toNormalizedString(@NotNull final StringBuilder buffer)
268  {
269    buffer.append(stringRepresentation);
270  }
271
272
273
274  /**
275   * Retrieves a normalized string representation of this Boolean value as it
276   * should appear in a JSON object.  If the Boolean value is {@code true}, then
277   * the string representation will be "{@code true}" (without the surrounding
278   * quotes).  If the Boolean value is {@code false}, then the string
279   * representation will be "{@code false}" (again, without the quotes).
280   *
281   * @param  ignoreFieldNameCase  Indicates whether field names should be
282   *                              treated in a case-sensitive (if {@code false})
283   *                              or case-insensitive (if {@code true}) manner.
284   * @param  ignoreValueCase      Indicates whether string field values should
285   *                              be treated in a case-sensitive (if
286   *                              {@code false}) or case-insensitive (if
287   *                              {@code true}) manner.
288   * @param  ignoreArrayOrder     Indicates whether the order of elements in an
289   *                              array should be considered significant (if
290   *                              {@code false}) or insignificant (if
291   *                              {@code true}).
292   *
293   * @return  A normalized string representation of this Boolean value as it
294   *          should appear in a JSON object.
295   */
296  @Override()
297  @NotNull()
298  public String toNormalizedString(final boolean ignoreFieldNameCase,
299                                   final boolean ignoreValueCase,
300                                   final boolean ignoreArrayOrder)
301  {
302    return stringRepresentation;
303  }
304
305
306
307  /**
308   * Appends a normalized string representation of this Boolean value as it
309   * should appear in a JSON object to the provided buffer.  If the Boolean
310   * value is {@code true}, then the string representation will be
311   * "{@code true}" (without the surrounding quotes).  If the Boolean value is
312   * {@code false}, then the string representation will be "{@code false}"
313   * (again, without the quotes).
314   *
315   * @param  buffer               The buffer to which the information should be
316   *                              appended.
317   * @param  ignoreFieldNameCase  Indicates whether field names should be
318   *                              treated in a case-sensitive (if {@code false})
319   *                              or case-insensitive (if {@code true}) manner.
320   * @param  ignoreValueCase      Indicates whether string field values should
321   *                              be treated in a case-sensitive (if
322   *                              {@code false}) or case-insensitive (if
323   *                              {@code true}) manner.
324   * @param  ignoreArrayOrder     Indicates whether the order of elements in an
325   *                              array should be considered significant (if
326   *                              {@code false}) or insignificant (if
327   *                              {@code true}).
328   */
329  @Override()
330  public void toNormalizedString(@NotNull final StringBuilder buffer,
331                                 final boolean ignoreFieldNameCase,
332                                 final boolean ignoreValueCase,
333                                 final boolean ignoreArrayOrder)
334  {
335    buffer.append(stringRepresentation);
336  }
337
338
339
340  /**
341   * {@inheritDoc}
342   */
343  @Override()
344  @NotNull()
345  public JSONBoolean toNormalizedValue(final boolean ignoreFieldNameCase,
346                                       final boolean ignoreValueCase,
347                                       final boolean ignoreArrayOrder)
348  {
349    // Boolean values are always in normalized form.
350    return this;
351  }
352
353
354
355  /**
356   * {@inheritDoc}
357   */
358  @Override()
359  public void appendToJSONBuffer(@NotNull final JSONBuffer buffer)
360  {
361    buffer.appendBoolean(booleanValue);
362  }
363
364
365
366  /**
367   * {@inheritDoc}
368   */
369  @Override()
370  public void appendToJSONBuffer(@NotNull final String fieldName,
371                                 @NotNull final JSONBuffer buffer)
372  {
373    buffer.appendBoolean(fieldName, booleanValue);
374  }
375}