de.perdian.commons.i18n.polyglot.PolyglotFactory.java Source code

Java tutorial

Introduction

Here is the source code for de.perdian.commons.i18n.polyglot.PolyglotFactory.java

Source

/*
 * 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.i18n.polyglot;

import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;

public class PolyglotFactory {

    /**
     * Creates a new {@link Polyglot} implementation. A default instance of the
     * {@code MessageSource} interface will be created. The lookup for keys will
     * be performed through a resource bundle that must be located at the same
     * directory within the classath as the given class.
     *
     * @param polyglotClass
     *      the class of the interface, that should be implemented according to
     *      the polyglot logics. Note, that this class must be an interface,
     *      not an actual class.
     * @return
     *      the {@code Polyglot} implementation. Once this instance has been
     *      created, it has no further references to the factory from which it
     *      was created.
     */
    public static <T> Polyglot<T> createPolyglot(Class<T> polyglotClass) {
        if (polyglotClass == null) {
            throw new IllegalArgumentException("Parameter 'polyglotClass' must not be null");
        } else {
            ResourceBundleMessageSource defaultMessageSource = new ResourceBundleMessageSource();
            defaultMessageSource.setBasename(polyglotClass.getName());
            return PolyglotFactory.createPolyglot(polyglotClass, defaultMessageSource);
        }
    }

    /**
     * Creates a new {@link Polyglot} implementation. A default instance of
     * {@code PolyglotOptions} will be created.
     *
     * @param polyglotClass
     *      the class of the interface, that should be implemented according to
     *      the polyglot logics. Note, that this class must be an interface, not
     *      an actual class.
     * @param messageSource
     *      the source from which messages to be localized can retrieve their
     *      values
     * @return
     *      the {@code Polyglot} implementation. Once this instance has been
     *      created, it has no further references to the factory from which it
     *      was created.
     */
    public static <T> Polyglot<T> createPolyglot(Class<T> polyglotClass, MessageSource messageSource) {
        if (polyglotClass == null) {
            throw new IllegalArgumentException("Parameter 'polyglotClass' must not be null");
        } else if (messageSource == null) {
            throw new IllegalArgumentException("Parameter 'messageSource' must not be null");
        } else if (!polyglotClass.isInterface()) {
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.append("Polyglot class '").append(polyglotClass.getName());
            errorMessage.append("' is not an interface. Only real interfaces can be ");
            errorMessage.append("used to create a Polyglot instance.");
            throw new IllegalArgumentException(errorMessage.toString());
        } else {
            PolyglotImpl<T> polyglotImpl = new PolyglotImpl<>();
            polyglotImpl.setInstanceClass(polyglotClass);
            polyglotImpl.setMessageSource(messageSource);
            return polyglotImpl;
        }
    }

}