All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.context.DefaultMessageContext Maven / Gradle / Ivy

There is a newer version: 4.7.12
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.context;

import io.micronaut.core.annotation.Internal;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;

/**
 * Default message context implementation.
 *
 * @author graemerocher
 * @since 1.2
 */
@Internal
class DefaultMessageContext implements MessageSource.MessageContext {

    private final @Nullable Locale locale;
    private final @Nullable Map variables;

    /**
     * Default constructor.
     * @param locale The locale
     * @param variables The message variables
     */
    DefaultMessageContext(@Nullable Locale locale, @Nullable Map variables) {
        this.locale = locale;
        this.variables = variables;
    }

    @NonNull
    @Override
    public Map getVariables() {
        if (variables != null) {
            return Collections.unmodifiableMap(variables);
        }
        return Collections.emptyMap();
    }

    @NonNull
    @Override
    public Locale getLocale() {
        return getLocale(Locale.getDefault());
    }

    /**
     * The locale to use to resolve messages.
     * @param defaultLocale The locale to use if no locale is present
     * @return The locale
     */
    @NonNull
    public Locale getLocale(@Nullable Locale defaultLocale) {
        if (locale != null) {
            return locale;
        } else {
            return defaultLocale != null ? defaultLocale : Locale.getDefault();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy