Java tutorial
/* * Copyright 2013 Christian Robert * * 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 de.perdian.commons.lang.conversion.impl.converters; import java.text.Format; import java.util.Locale; import org.springframework.context.i18n.LocaleContext; import org.springframework.core.convert.converter.Converter; public abstract class AbstractFormatConverter<A, B, F extends Format> implements Converter<A, B> { private F format = null; private LocaleContext localeContext = null; @Override public B convert(A source) { Locale resolvedLocale = this.getLocaleContext() == null ? null : this.getLocaleContext().getLocale(); Locale useLocale = resolvedLocale == null ? Locale.getDefault() : resolvedLocale; F internalFormat = this.getFormat(); F useFormat = internalFormat == null ? this.resolveDefaultFormat(useLocale) : internalFormat; return this.convertUsingFormat(useFormat, source); } protected abstract B convertUsingFormat(F format, A source); protected abstract F resolveDefaultFormat(Locale locale); // ------------------------------------------------------------------------- // --- Property access methods --------------------------------------------- // ------------------------------------------------------------------------- public F getFormat() { return this.format; } public void setFormat(F format) { this.format = format; } public LocaleContext getLocaleContext() { return this.localeContext; } public void setLocaleContext(LocaleContext localeContext) { this.localeContext = localeContext; } }