001/*
002 * Copyright 2022-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.soklet;
018
019import org.jspecify.annotations.NonNull;
020
021import javax.annotation.concurrent.Immutable;
022import java.math.BigDecimal;
023import java.util.Optional;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * Immutable wrapper for JSON-RPC request identifiers.
029 *
030 * @author <a href="https://www.revetkn.com">Mark Allen</a>
031 */
032@Immutable
033public record McpJsonRpcRequestId(
034                @NonNull McpValue value
035) {
036        public McpJsonRpcRequestId {
037                requireNonNull(value);
038
039                if (!(value instanceof McpString) && !(value instanceof McpNumber))
040                        throw new IllegalArgumentException("JSON-RPC request IDs must be strings or numbers.");
041        }
042
043        /**
044         * Creates a string-backed JSON-RPC request ID.
045         *
046         * @param value the string request ID
047         * @return the request ID wrapper
048         */
049        @NonNull
050        public static McpJsonRpcRequestId fromString(@NonNull String value) {
051                requireNonNull(value);
052                return new McpJsonRpcRequestId(new McpString(value));
053        }
054
055        /**
056         * Creates a number-backed JSON-RPC request ID.
057         *
058         * @param value the numeric request ID
059         * @return the request ID wrapper
060         */
061        @NonNull
062        public static McpJsonRpcRequestId fromNumber(@NonNull BigDecimal value) {
063                requireNonNull(value);
064                return new McpJsonRpcRequestId(new McpNumber(value));
065        }
066
067        /**
068         * Reads the request ID as a string when it is string-backed.
069         *
070         * @return the string request ID, if this wrapper holds one
071         */
072        @NonNull
073        public Optional<String> asString() {
074                return value() instanceof McpString mcpString ? Optional.of(mcpString.value()) : Optional.empty();
075        }
076
077        /**
078         * Reads the request ID as a number when it is number-backed.
079         *
080         * @return the numeric request ID, if this wrapper holds one
081         */
082        @NonNull
083        public Optional<BigDecimal> asNumber() {
084                return value() instanceof McpNumber mcpNumber ? Optional.of(mcpNumber.value()) : Optional.empty();
085        }
086}