001 /*
002 * Copyright 2015-2016 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2015-2016 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 */
021 package com.unboundid.util.json;
022
023
024
025 import com.unboundid.util.NotMutable;
026 import com.unboundid.util.ThreadSafety;
027 import 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)
040 public 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 * {@inheritDoc}
147 */
148 @Override()
149 public String toString()
150 {
151 return stringRepresentation;
152 }
153
154
155
156 /**
157 * {@inheritDoc}
158 */
159 @Override()
160 public void toString(final StringBuilder buffer)
161 {
162 buffer.append(stringRepresentation);
163 }
164
165
166
167 /**
168 * {@inheritDoc}
169 */
170 @Override()
171 public String toSingleLineString()
172 {
173 return stringRepresentation;
174 }
175
176
177
178 /**
179 * {@inheritDoc}
180 */
181 @Override()
182 public void toSingleLineString(final StringBuilder buffer)
183 {
184 buffer.append(stringRepresentation);
185 }
186
187
188
189 /**
190 * {@inheritDoc}
191 */
192 @Override()
193 public String toNormalizedString()
194 {
195 return stringRepresentation;
196 }
197
198
199
200 /**
201 * {@inheritDoc}
202 */
203 @Override()
204 public void toNormalizedString(final StringBuilder buffer)
205 {
206 buffer.append(stringRepresentation);
207 }
208
209
210
211 /**
212 * {@inheritDoc}
213 */
214 @Override()
215 public void appendToJSONBuffer(final JSONBuffer buffer)
216 {
217 buffer.appendBoolean(booleanValue);
218 }
219
220
221
222 /**
223 * {@inheritDoc}
224 */
225 @Override()
226 public void appendToJSONBuffer(final String fieldName,
227 final JSONBuffer buffer)
228 {
229 buffer.appendBoolean(fieldName, booleanValue);
230 }
231 }