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 */
016package com.revetsec.scim;
017
018import org.jspecify.annotations.NonNull;
019import org.jspecify.annotations.Nullable;
020
021import com.google.errorprone.annotations.CheckReturnValue;
022import javax.annotation.concurrent.Immutable;
023import java.math.BigDecimal;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * A SCIM comparison literal: JSON string, number, boolean or null. Construction
029 * is restricted to bounded parsing. Strings compare exactly; numbers compare
030 * by numeric value. Diagnostic rendering always redacts content.
031 * The four permitted literal kinds are stable within a major version and may be matched exhaustively.
032 *
033 * @since 1.0.0
034 */
035@Immutable
036public sealed interface ScimFilterValue permits ScimFilterValue.StringValue,
037        ScimFilterValue.NumberValue, ScimFilterValue.BooleanValue, ScimFilterValue.NullValue {
038    /** A well-formed UTF-16 string literal.
039     * @since 1.0.0
040     */
041    @Immutable
042    final class StringValue implements ScimFilterValue {
043        private final @NonNull String value;
044        StringValue(@NonNull String value) { this.value = requireNonNull(value); }
045
046        /** Returns the decoded literal, which may contain personal data.
047         * @return value
048         * @since 1.0.0
049         */
050        @CheckReturnValue public @NonNull String getValue() { return this.value; }
051
052        /** Compares literal content exactly.
053         * @param other candidate
054         * @return equality
055         * @since 1.0.0
056         */
057        @Override public boolean equals(@Nullable Object other) {
058            return this == other || (other instanceof StringValue literal && this.value.equals(literal.value));
059        }
060        /** Returns the value hash.
061         * @return hash
062         * @since 1.0.0
063         */
064        @Override public int hashCode() { return this.value.hashCode(); }
065        /** Redacts content.
066         * @return redacted description
067         * @since 1.0.0
068         */
069        @Override public @NonNull String toString() { return "ScimFilterValue.StringValue{value=<redacted>}"; }
070    }
071
072    /** A bounded exact decimal literal.
073     * @since 1.0.0
074     */
075    @Immutable
076    final class NumberValue implements ScimFilterValue {
077        private final @NonNull BigDecimal value;
078        private final @NonNull String expression;
079        private final int hashCode;
080        NumberValue(@NonNull BigDecimal value, @NonNull String expression) {
081            this.value = requireNonNull(value);
082            this.expression = requireNonNull(expression);
083            // Numeric equality has the same correctly rounded double; no exponent expansion.
084            this.hashCode = Double.hashCode(value.doubleValue());
085        }
086
087        /** Returns the exact literal value without expanding exponent notation.
088         * @return value
089         * @since 1.0.0
090         */
091        @CheckReturnValue public @NonNull BigDecimal getValue() { return this.value; }
092
093        @NonNull String expression() { return this.expression; }
094
095        /** Compares numeric value, ignoring scale.
096         * @param other candidate
097         * @return equality
098         * @since 1.0.0
099         */
100        @Override public boolean equals(@Nullable Object other) {
101            return this == other || (other instanceof NumberValue literal
102                    && this.hashCode == literal.hashCode && this.value.compareTo(literal.value) == 0);
103        }
104        /** Returns a hash consistent with numeric equality.
105         * @return hash
106         * @since 1.0.0
107         */
108        @Override public int hashCode() { return this.hashCode; }
109        /** Redacts content.
110         * @return redacted description
111         * @since 1.0.0
112         */
113        @Override public @NonNull String toString() { return "ScimFilterValue.NumberValue{value=<redacted>}"; }
114    }
115
116    /** A JSON boolean literal.
117     * @since 1.0.0
118     */
119    @Immutable
120    final class BooleanValue implements ScimFilterValue {
121        private final boolean value;
122        BooleanValue(boolean value) { this.value = value; }
123
124        /** Returns the boolean literal.
125         * @return value
126         * @since 1.0.0
127         */
128        @CheckReturnValue public @NonNull Boolean getValue() { return this.value; }
129
130        /** Compares boolean value.
131         * @param other candidate
132         * @return equality
133         * @since 1.0.0
134         */
135        @Override public boolean equals(@Nullable Object other) {
136            return this == other || (other instanceof BooleanValue literal && this.value == literal.value);
137        }
138        /** Returns the value hash.
139         * @return hash
140         * @since 1.0.0
141         */
142        @Override public int hashCode() { return Boolean.hashCode(this.value); }
143        /** Redacts content.
144         * @return redacted description
145         * @since 1.0.0
146         */
147        @Override public @NonNull String toString() { return "ScimFilterValue.BooleanValue{value=<redacted>}"; }
148    }
149
150    /** JSON null, with no nullable payload.
151     * @since 1.0.0
152     */
153    @Immutable
154    final class NullValue implements ScimFilterValue {
155        NullValue() {}
156        /** Compares null literals.
157         * @param other candidate
158         * @return equality
159         * @since 1.0.0
160         */
161        @Override public boolean equals(@Nullable Object other) { return other instanceof NullValue; }
162        /** Returns a fixed hash.
163         * @return hash
164         * @since 1.0.0
165         */
166        @Override public int hashCode() { return 0; }
167        /** Redacts content.
168         * @return redacted description
169         * @since 1.0.0
170         */
171        @Override public @NonNull String toString() { return "ScimFilterValue.NullValue{value=<redacted>}"; }
172    }
173}