001/*
002 * Copyright 2017-2022 Product Mog LLC, 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.lokalized;
018
019import org.jspecify.annotations.NonNull;
020
021import static java.util.Objects.requireNonNull;
022
023/**
024 * Decides how Lokalized should respond to a non-fatal validation problem detected while loading a localized strings file.
025 * <p>
026 * A handler may be supplied to {@link LocalizedStringLoader}'s load methods. Overloads without a handler silently
027 * {@link #ignore() ignore} warnings. Provide {@link #throwException()} to make warnings fatal, or a custom handler to
028 * collect them, forward them to your logging framework, increment a metric, and so on.
029 *
030 * @author <a href="https://revetkn.com">Mark Allen</a>
031 * @since 3.0.0
032 */
033@FunctionalInterface
034public interface LocalizedStringWarningHandler {
035        /**
036         * Handles a localized strings validation warning.
037         * <p>
038         * Throwing from this method aborts loading and propagates to the caller of the load method.
039         *
040         * @param warning the warning to handle, not null
041         */
042        void handle(@NonNull LocalizedStringWarning warning);
043
044        /**
045         * Returns a handler that silently ignores every warning and allows loading to continue.
046         * <p>
047         * This is the default when a loading or parsing overload does not accept a handler.
048         *
049         * @return the handler, not null
050         */
051        @NonNull
052        static LocalizedStringWarningHandler ignore() {
053                return (warning) -> {
054                        requireNonNull(warning);
055                };
056        }
057
058        /**
059         * Returns a handler that turns every warning into a fatal {@link LocalizedStringLoadingException}, aborting the load.
060         * <p>
061         * This is useful for build-time or test-time strictness where an incomplete localized strings file should fail fast.
062         *
063         * @return the handler, not null
064         */
065        @NonNull
066        static LocalizedStringWarningHandler throwException() {
067                return (warning) -> {
068                        requireNonNull(warning);
069                        throw new LocalizedStringLoadingException(warning.getMessage());
070                };
071        }
072}