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;
020import org.jspecify.annotations.Nullable;
021
022import javax.annotation.concurrent.ThreadSafe;
023import java.util.List;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * Immutable deterministic extraction view of configured framework-owned MCP
029 * presentation text.
030 *
031 * @author <a href="https://www.revetkn.com">Mark Allen</a>
032 */
033@ThreadSafe
034public final class McpLocalizationCatalog {
035        @NonNull
036        private final List<@NonNull McpLocalizableText> texts;
037
038        /**
039         * Extracts the canonical localizable catalog from the final endpoint
040         * registry. Annotated, generated, and programmatic registrations therefore
041         * share one extraction authority. Extraction rejects an external-key
042         * collision between unequal coordinates instead of silently merging fields.
043         *
044         * @param endpointRegistry final immutable endpoint registry
045         * @return deterministic localization catalog
046         * @throws NullPointerException if {@code endpointRegistry} is null
047         * @throws IllegalStateException if unequal coordinates produce the same
048         * external key
049         */
050        @NonNull
051        public static McpLocalizationCatalog fromEndpointRegistry(
052                        @NonNull McpEndpointRegistry endpointRegistry) {
053                return new McpLocalizationCatalog(
054                                DefaultMcpLocalizationCatalogExtractor.extract(
055                                                requireNonNull(endpointRegistry)));
056        }
057
058        private McpLocalizationCatalog(
059                        @NonNull List<@NonNull McpLocalizableText> texts) {
060                this.texts = List.copyOf(texts);
061        }
062
063        /**
064         * Returns every configured eligible source field exactly once in stable
065         * external-key order.
066         *
067         * @return immutable extracted text list
068         */
069        @NonNull
070        public List<@NonNull McpLocalizableText> getTexts() {
071                return this.texts;
072        }
073
074        /** @return whether the extracted text lists are structurally equal */
075        @Override
076        public boolean equals(@Nullable Object other) {
077                return this == other
078                                || other instanceof McpLocalizationCatalog catalog
079                                && this.texts.equals(catalog.texts);
080        }
081
082        /** @return structural localization-catalog hash code */
083        @Override
084        public int hashCode() {
085                return this.texts.hashCode();
086        }
087}