001/*
002 * Copyright 2002-2014 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.core.convert.support;
018
019import java.util.Set;
020
021import org.springframework.core.convert.converter.Converter;
022import org.springframework.core.convert.converter.ConverterFactory;
023import org.springframework.core.convert.converter.ConverterRegistry;
024import org.springframework.core.convert.converter.GenericConverter;
025
026/**
027 * A factory for common {@link org.springframework.core.convert.ConversionService}
028 * configurations.
029 *
030 * @author Keith Donald
031 * @author Juergen Hoeller
032 * @author Chris Beams
033 * @since 3.0
034 */
035public abstract class ConversionServiceFactory {
036
037        /**
038         * Register the given Converter objects with the given target ConverterRegistry.
039         * @param converters the converter objects: implementing {@link Converter},
040         * {@link ConverterFactory}, or {@link GenericConverter}
041         * @param registry the target registry
042         */
043        public static void registerConverters(Set<?> converters, ConverterRegistry registry) {
044                if (converters != null) {
045                        for (Object converter : converters) {
046                                if (converter instanceof GenericConverter) {
047                                        registry.addConverter((GenericConverter) converter);
048                                }
049                                else if (converter instanceof Converter<?, ?>) {
050                                        registry.addConverter((Converter<?, ?>) converter);
051                                }
052                                else if (converter instanceof ConverterFactory<?, ?>) {
053                                        registry.addConverterFactory((ConverterFactory<?, ?>) converter);
054                                }
055                                else {
056                                        throw new IllegalArgumentException("Each converter object must implement one of the " +
057                                                        "Converter, ConverterFactory, or GenericConverter interfaces");
058                                }
059                        }
060                }
061        }
062
063}