001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.revetsec.json;
018
019import com.revetsec.internal.json.JsonWriter;
020import org.jspecify.annotations.NonNull;
021import org.jspecify.annotations.Nullable;
022
023import javax.annotation.concurrent.Immutable;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * A JSON {@code true} or {@code false}. There are exactly two instances.
029 *
030 * @author <a href="https://www.revetkn.com">Mark Allen</a>
031 * @since 1.0.0
032 */
033@Immutable
034public final class JsonBoolean implements JsonValue {
035        @NonNull
036        private static final JsonBoolean TRUE = new JsonBoolean(true);
037        @NonNull
038        private static final JsonBoolean FALSE = new JsonBoolean(false);
039
040        private final boolean value;
041
042        /**
043         * Returns the JSON {@code true}.
044         *
045         * @return the shared instance for {@code true}
046         * @since 1.0.0
047         */
048        @NonNull
049        public static JsonBoolean trueInstance() {
050                return TRUE;
051        }
052
053        /**
054         * Returns the JSON {@code false}.
055         *
056         * @return the shared instance for {@code false}
057         * @since 1.0.0
058         */
059        @NonNull
060        public static JsonBoolean falseInstance() {
061                return FALSE;
062        }
063
064        /**
065         * Returns the JSON boolean for {@code value}.
066         *
067         * @param value the boolean
068         * @return {@link #trueInstance()} or {@link #falseInstance()}
069         * @throws NullPointerException if {@code value} is {@code null}
070         * @since 1.0.0
071         */
072        @NonNull
073        public static JsonBoolean fromValue(@NonNull Boolean value) {
074                requireNonNull(value);
075                return value.booleanValue() ? TRUE : FALSE;
076        }
077
078        private JsonBoolean(boolean value) {
079                this.value = value;
080        }
081
082        /**
083         * Returns this boolean's value.
084         *
085         * @return {@code true} or {@code false}
086         * @since 1.0.0
087         */
088        @NonNull
089        public Boolean getValue() {
090                return this.value;
091        }
092
093        @Override
094        @NonNull
095        public String toJson() {
096                return JsonWriter.toJson(this);
097        }
098
099        @Override
100        public boolean equals(@Nullable Object other) {
101                // Exactly two instances exist, one per value, so identity is value equality.
102                return this == other;
103        }
104
105        @Override
106        public int hashCode() {
107                return Boolean.hashCode(this.value);
108        }
109
110        @Override
111        @NonNull
112        public String toString() {
113                return "JsonBoolean{value=<redacted>}";
114        }
115}