View Javadoc
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  /**
20   * {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion to and from <strong>java.lang.Character</strong> objects.
21   * <p>
22   * Can be configured to either return a <em>default value</em> or throw a {@code ConversionException} if a conversion error occurs.
23   *
24   * @since 1.3
25   */
26  public final class CharacterConverter extends AbstractConverter<Character> {
27  
28      /**
29       * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
30       */
31      public CharacterConverter() {
32      }
33  
34      /**
35       * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that returns a default value if an error occurs.
36       *
37       * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
38       */
39      public CharacterConverter(final char defaultValue) {
40          super(defaultValue);
41      }
42  
43      /**
44       * Constructs a <strong>java.lang.Character</strong> <em>Converter</em> that returns a default value if an error occurs.
45       *
46       * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
47       */
48      public CharacterConverter(final Character defaultValue) {
49          super(defaultValue);
50      }
51  
52      /**
53       * <p>
54       * Converts a java.lang.Class or object into a String.
55       * </p>
56       *
57       * @param value The input value to be converted
58       * @return the converted String value.
59       * @since 1.8.0
60       */
61      @Override
62      protected String convertToString(final Object value) {
63          final String strValue = value.toString();
64          return strValue.isEmpty() ? "" : strValue.substring(0, 1);
65      }
66  
67      /**
68       * <p>
69       * Converts the input object into a java.lang.Character.
70       * </p>
71       *
72       * @param <T>   Target type of the conversion.
73       * @param type  Data type to which this value should be converted.
74       * @param value The input value to be converted.
75       * @return The converted value.
76       * @throws Exception if conversion cannot be performed successfully
77       * @since 1.8.0
78       */
79      @Override
80      protected <T> T convertToType(final Class<T> type, final Object value) throws Exception {
81          if (Character.class.equals(type) || Character.TYPE.equals(type)) {
82              return type.cast(Character.valueOf(value.toString().charAt(0)));
83          }
84  
85          throw conversionException(type, value);
86      }
87  
88      /**
89       * Gets the default type this {@code Converter} handles.
90       *
91       * @return The default type this {@code Converter} handles.
92       * @since 1.8.0
93       */
94      @Override
95      protected Class<Character> getDefaultType() {
96          return Character.class;
97      }
98  
99  }