001/*
002 * Copyright 2002-2017 the original author or authors.
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 *      https://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 org.springframework.context.i18n;
018
019import java.util.Locale;
020import java.util.TimeZone;
021
022import org.springframework.lang.Nullable;
023
024/**
025 * Simple implementation of the {@link TimeZoneAwareLocaleContext} interface,
026 * always returning a specified {@code Locale} and {@code TimeZone}.
027 *
028 * <p>Note: Prefer the use of {@link SimpleLocaleContext} when only setting
029 * a Locale but no TimeZone.
030 *
031 * @author Juergen Hoeller
032 * @author Nicholas Williams
033 * @since 4.0
034 * @see LocaleContextHolder#setLocaleContext
035 * @see LocaleContextHolder#getTimeZone()
036 */
037public class SimpleTimeZoneAwareLocaleContext extends SimpleLocaleContext implements TimeZoneAwareLocaleContext {
038
039        @Nullable
040        private final TimeZone timeZone;
041
042
043        /**
044         * Create a new SimpleTimeZoneAwareLocaleContext that exposes the specified
045         * Locale and TimeZone. Every {@link #getLocale()} call will return the given
046         * Locale, and every {@link #getTimeZone()} call will return the given TimeZone.
047         * @param locale the Locale to expose
048         * @param timeZone the TimeZone to expose
049         */
050        public SimpleTimeZoneAwareLocaleContext(@Nullable Locale locale, @Nullable TimeZone timeZone) {
051                super(locale);
052                this.timeZone = timeZone;
053        }
054
055
056        @Override
057        @Nullable
058        public TimeZone getTimeZone() {
059                return this.timeZone;
060        }
061
062        @Override
063        public String toString() {
064                return super.toString() + " " + (this.timeZone != null ? this.timeZone.toString() : "-");
065        }
066
067}