001/*
002 * Copyright 2015-2017 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2017 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.util.json;
022
023
024
025import com.unboundid.util.NotMutable;
026import com.unboundid.util.ThreadSafety;
027import com.unboundid.util.ThreadSafetyLevel;
028
029
030
031/**
032 * This class provides an implementation of a JSON value that represents a
033 * Java Boolean.  The string representation of the JSON Boolean true value is
034 * {@code true}, and the string representation of the JSON Boolean false value
035 * is {@code false}.  These values are not surrounded by quotation marks, and
036 * they must be entirely lowercase.
037 */
038@NotMutable()
039@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
040public final class JSONBoolean
041       extends JSONValue
042{
043  /**
044   * A pre-allocated object that represents a value of {@code false}.
045   */
046  public static final JSONBoolean FALSE = new JSONBoolean(false);
047
048
049
050  /**
051   * A pre-allocated object that represents a value of {@code true}.
052   */
053  public static final JSONBoolean TRUE = new JSONBoolean(true);
054
055
056
057  /**
058   * The serial version UID for this serializable class.
059   */
060  private static final long serialVersionUID = -5090296701442873481L;
061
062
063
064  // The boolean value for this object.
065  private final boolean booleanValue;
066
067  // The string representation for this object.
068  private final String stringRepresentation;
069
070
071
072  /**
073   * Creates a new JSON value capable of representing a Boolean value of either
074   * {@code true} or {@code false}.
075   *
076   * @param  booleanValue  The Boolean value for this JSON value.
077   */
078  public JSONBoolean(final boolean booleanValue)
079  {
080    this.booleanValue = booleanValue;
081    stringRepresentation = (booleanValue ? "true" : "false");
082  }
083
084
085
086  /**
087   * Retrieves the Java boolean value for this JSON value.
088   *
089   * @return  The Java boolean value for this JSON value.
090   */
091  public boolean booleanValue()
092  {
093    return booleanValue;
094  }
095
096
097
098  /**
099   * {@inheritDoc}
100   */
101  @Override()
102  public int hashCode()
103  {
104    return (booleanValue ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode());
105  }
106
107
108
109  /**
110   * {@inheritDoc}
111   */
112  @Override()
113  public boolean equals(final Object o)
114  {
115    if (o == this)
116    {
117      return true;
118    }
119
120    if (o instanceof JSONBoolean)
121    {
122      final JSONBoolean b = (JSONBoolean) o;
123      return (b.booleanValue == booleanValue);
124    }
125
126    return false;
127  }
128
129
130
131  /**
132   * {@inheritDoc}
133   */
134  @Override()
135  public boolean equals(final JSONValue v, final boolean ignoreFieldNameCase,
136                        final boolean ignoreValueCase,
137                        final boolean ignoreArrayOrder)
138  {
139    return ((v instanceof JSONBoolean) &&
140         (booleanValue == ((JSONBoolean) v).booleanValue));
141  }
142
143
144
145  /**
146   * Retrieves a string representation of this Boolean value as it should appear
147   * in a JSON object.  If the Boolean value is {@code true}, then the string
148   * representation will be "{@code true}" (without the surrounding quotes).  If
149   * the Boolean value is {@code false}, then the string representation will be
150   * "{@code false}" (again, without the quotes).
151   *
152   * @return  A string representation of this Boolean value as it should appear
153   *          in a JSON object.
154   */
155  @Override()
156  public String toString()
157  {
158    return stringRepresentation;
159  }
160
161
162
163  /**
164   * Appends a string representation of this Boolean value as it should appear
165   * in a JSON object to the provided buffer.  If the Boolean value is
166   * {@code true}, then the string representation will be "{@code true}"
167   * (without the surrounding quotes).  If the Boolean value is {@code false},
168   * then the string representation will be "{@code false}" (again, without the
169   * quotes).
170   *
171   * @param  buffer  The buffer to which the information should be appended.
172   */
173  @Override()
174  public void toString(final StringBuilder buffer)
175  {
176    buffer.append(stringRepresentation);
177  }
178
179
180
181  /**
182   * Retrieves a single-line string representation of this Boolean value as it
183   * should appear in a JSON object.  If the Boolean value is {@code true}, then
184   * the string representation will be "{@code true}" (without the surrounding
185   * quotes).  If the Boolean value is {@code false}, then the string
186   * representation will be "{@code false}" (again, without the quotes).
187   *
188   * @return  A single-line string representation of this Boolean value as it
189   *          should appear in a JSON object.
190   */
191  @Override()
192  public String toSingleLineString()
193  {
194    return stringRepresentation;
195  }
196
197
198
199  /**
200   * Appends a single-line string representation of this Boolean value as it
201   * should appear in a JSON object to the provided buffer.  If the Boolean
202   * value is {@code true}, then the string representation will be
203   * "{@code true}" (without the surrounding quotes).  If the Boolean value is
204   * {@code false}, then the string representation will be "{@code false}"
205   * (again, without the quotes).
206   *
207   * @param  buffer  The buffer to which the information should be appended.
208   */
209  @Override()
210  public void toSingleLineString(final StringBuilder buffer)
211  {
212    buffer.append(stringRepresentation);
213  }
214
215
216
217  /**
218   * Retrieves a normalized string representation of this Boolean value as it
219   * should appear in a JSON object.  If the Boolean value is {@code true}, then
220   * the string representation will be "{@code true}" (without the surrounding
221   * quotes).  If the Boolean value is {@code false}, then the string
222   * representation will be "{@code false}" (again, without the quotes).
223   *
224   * @return  A normalized string representation of this Boolean value as it
225   *          should appear in a JSON object.
226   */
227  @Override()
228  public String toNormalizedString()
229  {
230    return stringRepresentation;
231  }
232
233
234
235  /**
236   * Appends a normalized string representation of this Boolean value as it
237   * should appear in a JSON object to the provided buffer.  If the Boolean
238   * value is {@code true}, then the string representation will be
239   * "{@code true}" (without the surrounding quotes).  If the Boolean value is
240   * {@code false}, then the string representation will be "{@code false}"
241   * (again, without the quotes).
242   *
243   * @param  buffer  The buffer to which the information should be appended.
244   */
245  @Override()
246  public void toNormalizedString(final StringBuilder buffer)
247  {
248    buffer.append(stringRepresentation);
249  }
250
251
252
253  /**
254   * {@inheritDoc}
255   */
256  @Override()
257  public void appendToJSONBuffer(final JSONBuffer buffer)
258  {
259    buffer.appendBoolean(booleanValue);
260  }
261
262
263
264  /**
265   * {@inheritDoc}
266   */
267  @Override()
268  public void appendToJSONBuffer(final String fieldName,
269                                 final JSONBuffer buffer)
270  {
271    buffer.appendBoolean(fieldName, booleanValue);
272  }
273}