CharacterConverter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.beanutils2.converters;

  18. /**
  19.  * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from <strong>java.lang.Character</strong> objects.
  20.  * <p>
  21.  * Can be configured to either return a <em>default value</em> or throw a {@code ConversionException} if a conversion error occurs.
  22.  *
  23.  * @since 1.3
  24.  */
  25. public final class CharacterConverter extends AbstractConverter<Character> {

  26.     /**
  27.      * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
  28.      */
  29.     public CharacterConverter() {
  30.     }

  31.     /**
  32.      * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that returns a default value if an error occurs.
  33.      *
  34.      * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
  35.      */
  36.     public CharacterConverter(final char defaultValue) {
  37.         super(defaultValue);
  38.     }

  39.     /**
  40.      * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that returns a default value if an error occurs.
  41.      *
  42.      * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
  43.      */
  44.     public CharacterConverter(final Character defaultValue) {
  45.         super(defaultValue);
  46.     }

  47.     /**
  48.      * <p>
  49.      * Converts a java.lang.Class or object into a String.
  50.      * </p>
  51.      *
  52.      * @param value The input value to be converted
  53.      * @return the converted String value.
  54.      * @since 1.8.0
  55.      */
  56.     @Override
  57.     protected String convertToString(final Object value) {
  58.         final String strValue = value.toString();
  59.         return strValue.isEmpty() ? "" : strValue.substring(0, 1);
  60.     }

  61.     /**
  62.      * <p>
  63.      * Converts the input object into a java.lang.Character.
  64.      * </p>
  65.      *
  66.      * @param <T>   Target type of the conversion.
  67.      * @param type  Data type to which this value should be converted.
  68.      * @param value The input value to be converted.
  69.      * @return The converted value.
  70.      * @throws Exception if conversion cannot be performed successfully
  71.      * @since 1.8.0
  72.      */
  73.     @Override
  74.     protected <T> T convertToType(final Class<T> type, final Object value) throws Exception {
  75.         if (Character.class.equals(type) || Character.TYPE.equals(type)) {
  76.             return type.cast(Character.valueOf(value.toString().charAt(0)));
  77.         }

  78.         throw conversionException(type, value);
  79.     }

  80.     /**
  81.      * Gets the default type this {@code Converter} handles.
  82.      *
  83.      * @return The default type this {@code Converter} handles.
  84.      * @since 1.8.0
  85.      */
  86.     @Override
  87.     protected Class<Character> getDefaultType() {
  88.         return Character.class;
  89.     }

  90. }