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