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.scim;
018
019import com.google.errorprone.annotations.CheckReturnValue;
020import com.revetsec.json.JsonValue;
021import org.jspecify.annotations.NonNull;
022import org.jspecify.annotations.Nullable;
023import javax.annotation.concurrent.Immutable;
024import java.util.Optional;
025import static java.util.Objects.requireNonNull;
026
027/** A schema-bound operation created only by the PATCH parser. Its value may contain personal data.
028 * Filtered paths expose the outer attribute, a same-element predicate, and an optional suffix.
029 * An unfiltered subattribute remains part of the attribute path. A whole registered extension
030 * schema URI has a separate extension identifier and no attribute path.
031 * @since 1.0.0
032 */
033@Immutable
034@CheckReturnValue
035public final class ScimPatchOperation {
036    private final int operationIndex;
037    private final @NonNull ScimPatchOperationType operationType;
038    private final @Nullable ScimAttributePath attributePath;
039    private final @Nullable String extensionSchemaId;
040    private final @Nullable ScimFilter valueFilter;
041    private final @Nullable String subAttributeName;
042    private final @Nullable JsonValue value;
043    ScimPatchOperation(int index, @NonNull ScimPatchOperationType type,
044            @Nullable ScimAttributePath path, @Nullable ScimFilter filter,
045            @Nullable String suffix, @Nullable JsonValue value) {
046        this.operationIndex=index; this.operationType=requireNonNull(type); this.attributePath=path;
047        this.extensionSchemaId=null;
048        this.valueFilter=filter; this.subAttributeName=suffix; this.value=value;
049    }
050    ScimPatchOperation(int index, @NonNull ScimPatchOperationType type,
051            @NonNull String extensionSchemaId, @Nullable JsonValue value) {
052        this.operationIndex=index; this.operationType=requireNonNull(type); this.attributePath=null;
053        this.extensionSchemaId=requireNonNull(extensionSchemaId);
054        this.valueFilter=null; this.subAttributeName=null; this.value=value;
055    }
056    /** Returns the zero-based original request index, preserved through reduction.
057     * @return the original index
058     * @since 1.0.0 */
059    public @NonNull Integer getOperationIndex() { return operationIndex; }
060    /** Returns the operation kind.
061     * @return the kind
062     * @since 1.0.0 */
063    public @NonNull ScimPatchOperationType getOperationType() { return operationType; }
064    /** Returns the canonical path, absent for a resource-object or whole-extension operation.
065     * @return the path
066     * @since 1.0.0 */
067    public @NonNull Optional<@NonNull ScimAttributePath> getAttributePath() { return Optional.ofNullable(attributePath); }
068    /** Returns the canonical registered extension schema URI for a whole-extension target.
069     * It is absent for ordinary attribute paths and resource-object operations.
070     * @return the extension schema identifier
071     * @since 1.0.0 */
072    public @NonNull Optional<@NonNull String> getExtensionSchemaId() { return Optional.ofNullable(extensionSchemaId); }
073    /** Returns the bound same-element selector, when present.
074     * @return the predicate
075     * @since 1.0.0 */
076    public @NonNull Optional<@NonNull ScimFilter> getValueFilter() { return Optional.ofNullable(valueFilter); }
077    /** Returns the canonical suffix following a filtered path.
078     * @return the suffix
079     * @since 1.0.0 */
080    public @NonNull Optional<@NonNull String> getSubAttributeName() { return Optional.ofNullable(subAttributeName); }
081    /** Returns the normalized value. Explicit JSON null is present; an absent remove value is empty.
082     * @return the value, which may contain personal data
083     * @since 1.0.0 */
084    public @NonNull Optional<@NonNull JsonValue> getValue() { return Optional.ofNullable(value); }
085    /** Returns a fixed redacted description.
086     * @return the description
087     * @since 1.0.0 */
088    @Override public @NonNull String toString() { return "ScimPatchOperation{<redacted>}"; }
089}