001    /*
002     *  Copyright 2003-2004 The Apache Software Foundation
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     *      http://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    package org.apache.commons.convert1.string;
017    
018    
019    import org.apache.commons.convert1.ConversionException;
020    import org.apache.commons.convert1.Converter;
021    
022    
023    /**
024     * <p>Standard {@link Converter} implementation that converts an incoming
025     * String into a <code>java.lang.Character</code> object, optionally using a
026     * default value or throwing a {@link ConversionException} if a conversion
027     * error occurs.</p>
028     *
029     * @author Craig R. McClanahan
030     * @version $Id: CharacterConverter.java 155441 2005-02-26 13:19:22Z dirkv $
031     * @since 0.1
032     */
033    
034    public final class CharacterConverter implements Converter {
035    
036    
037        // ----------------------------------------------------------- Constructors
038    
039    
040        /**
041         * Create a {@link Converter} that will throw a {@link ConversionException}
042         * if a conversion error occurs.
043         */
044        public CharacterConverter() {
045    
046            this.defaultValue = null;
047            this.useDefault = false;
048    
049        }
050    
051    
052        /**
053         * Create a {@link Converter} that will return the specified default value
054         * if a conversion error occurs.
055         *
056         * @param defaultValue The default value to be returned
057         */
058        public CharacterConverter(Object defaultValue) {
059    
060            this.defaultValue = defaultValue;
061            this.useDefault = true;
062    
063        }
064    
065    
066        // ----------------------------------------------------- Instance Variables
067    
068    
069        /**
070         * The default value specified to our Constructor, if any.
071         */
072        private Object defaultValue = null;
073    
074    
075        /**
076         * Should we return the default value on conversion errors?
077         */
078        private boolean useDefault = true;
079    
080    
081        // --------------------------------------------------------- Public Methods
082    
083    
084        /**
085         * Convert the specified input object into an output object of the
086         * specified type.
087         *
088         * @param type Data type to which this value should be converted
089         * @param value The input value to be converted
090         *
091         * @exception ConversionException if conversion cannot be performed
092         *  successfully
093         */
094        public Object convert(Class type, Object value) {
095    
096            if (value == null) {
097                if (useDefault) {
098                    return (defaultValue);
099                } else {
100                    throw new ConversionException("No value specified");
101                }
102            }
103    
104            if (value instanceof Character) {
105                return (value);
106            }
107    
108            try {
109                return (new Character(value.toString().charAt(0)));
110            } catch (Exception e) {
111                if (useDefault) {
112                    return (defaultValue);
113                } else {
114                    throw new ConversionException(e);
115                }
116            }
117    
118        }
119    
120    
121    }