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