001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.convert.converter;
018
019import java.util.Locale;
020
021import org.apache.wicket.util.convert.IConverter;
022
023
024/**
025 * Converts from Object to Character.
026 * 
027 * @author Eelco Hillenius
028 * @author Jonathan Locke
029 */
030public class CharacterConverter extends AbstractConverter<Character>
031{
032        private static final long serialVersionUID = 1L;
033
034        /**
035         * The singleton instance for a character converter
036         */
037        public static final IConverter<Character> INSTANCE = new CharacterConverter();
038
039        /**
040         * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String,Locale)
041         */
042        @Override
043        public Character convertToObject(final String value, final Locale locale)
044        {
045                int length = value.length();
046                if (length == 0)
047                {
048                        return null;
049                }
050                else if (length == 1)
051                {
052                        return value.charAt(0);
053                }
054                throw newConversionException("Cannot convert '" + value + "' to Character", value, locale);
055        }
056
057        /**
058         * @see org.apache.wicket.util.convert.converter.AbstractConverter#getTargetType()
059         */
060        @Override
061        protected Class<Character> getTargetType()
062        {
063                return Character.class;
064        }
065}