001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Original code by Joe Walnes                                               *
009     *****************************************************************************/
010    
011    
012    package org.picocontainer.script.util;
013    
014    import org.picocontainer.PicoCompositionException;
015    
016    import java.util.HashMap;
017    import java.util.Map;
018    
019    @SuppressWarnings("serial")
020    public class StringConversions {
021    
022        public interface StringConverter<T> {
023            T convert(String in);
024        }
025    
026        public static class InvalidConversionException extends PicoCompositionException {
027            public InvalidConversionException(String message) {
028            super(message);
029        }
030    }
031    
032        private final Map<Class<?>, StringConverter<?>> converters = new HashMap<Class<?>, StringConverter<?>>();
033    
034        public StringConversions() {
035            register(String.class, new StringConverter<String>() {
036                public String convert(String in) {
037                    return in;
038                }
039            });
040    
041            register(Integer.class, new StringConverter<Integer>() {
042                public Integer convert(String in) {
043                    return in == null ? 0 : Integer.valueOf(in);
044                }
045            });
046    
047            register(Long.class, new StringConverter<Long>() {
048                public Long convert(String in) {
049                    return in == null ? (long) 0 : Long.valueOf(in);
050                }
051            });
052    
053            register(Boolean.class, new StringConverter<Boolean>() {
054                public Boolean convert(String in) {
055                    if (in == null || in.length() == 0) {
056                        return Boolean.FALSE;
057                    }
058                    char c = in.toLowerCase().charAt(0);
059                    return c == '1' || c == 'y' || c == 't' ? Boolean.TRUE : Boolean.FALSE;
060                }
061            });
062        }
063    
064        public Object convertTo(Class<?> desiredClass, String inputString) {
065            StringConverter<?> converter = converters.get(desiredClass);
066            if (converter == null) {
067                throw new InvalidConversionException("Cannot convert to type " + desiredClass.getName());
068            }
069            return converter.convert(inputString);
070        }
071    
072        public void register(Class<?> type, StringConverter<?> converter) {
073            converters.put(type, converter);
074        }
075    }