001package org.unix4j.convert;
002
003import org.unix4j.convert.EnumConverters.StringToEnumConverter;
004import org.unix4j.util.Range;
005
006import java.io.File;
007import java.io.InputStream;
008import java.io.OutputStream;
009import java.net.URL;
010import java.util.Date;
011import java.util.HashMap;
012import java.util.Map;
013import java.util.regex.Pattern;
014
015/**
016 * Default implementation for {@code ConverterRegistry} storing the converters
017 * per value type in a {@link Map}. Converters for enums are created on the fly.
018 */
019public class DefaultConverterRegistry implements ConverterRegistry {
020
021        private final Map<Class<?>, ValueConverter<?>> converterByType = new HashMap<Class<?>, ValueConverter<?>>();
022        
023        public DefaultConverterRegistry() {
024                initConverters();
025        }
026        
027        protected void initConverters() {
028                registerValueConverter(Character.class, CharacterConverters.DEFAULT);
029                registerValueConverter(char.class, CharacterConverters.DEFAULT);
030                registerValueConverter(Integer.class, IntegerConverters.DEFAULT);
031                registerValueConverter(int.class, IntegerConverters.DEFAULT);
032                registerValueConverter(Long.class, LongConverters.DEFAULT);
033                registerValueConverter(long.class, LongConverters.DEFAULT);
034                registerValueConverter(String.class, StringConverters.DEFAULT);
035                registerValueConverter(String[].class, StringArrayConverters.FLATTEN);
036                registerValueConverter(File[].class, FileArrayConverters.FLATTEN);
037                registerValueConverter(Object[].class, ArrayConverters.FLATTEN);
038                registerValueConverter(Date.class, DateConverters.DEFAULT);
039                registerValueConverter(File.class, FileConverters.DEFAULT);
040                registerValueConverter(InputStream.class, InputStreamConverters.DEFAULT);
041                registerValueConverter(OutputStream.class, OutputStreamConverters.DEFAULT);
042                registerValueConverter(URL.class, URLConverters.DEFAULT);
043                registerValueConverter(Pattern.class, PatternConverters.DEFAULT);
044                registerValueConverter(Range.class, RangeConverters.DEFAULT);
045        }
046
047        @Override
048        public <V> ValueConverter<V> getValueConverterFor(Class<V> type) {
049                @SuppressWarnings("unchecked")
050                final ValueConverter<V> converter = (ValueConverter<V>) converterByType.get(type);
051                if (converter != null) {
052                        return converter;
053                }
054                return getEnumConverterFor(type);
055        }
056        
057        private <V> ValueConverter<V> getEnumConverterFor(Class<V> type) {
058                if (type.isEnum()) {
059                        @SuppressWarnings({ "rawtypes", "unchecked" })
060                        final ValueConverter<V> enumConverter = new StringToEnumConverter(type);
061                        return enumConverter;
062                }
063                return null;
064        }
065        
066        public <V> void registerValueConverter(Class<V> type, ValueConverter<V> converter) {
067                converterByType.put(type, converter);
068        }
069
070}