1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.properties.converter

File AbstractConverter.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

6
12
4
1
107
34
7
0.58
3
4
1.75

Classes

Class Line # Actions
AbstractConverter 36 12 0% 7 2
0.9090909490.9%
 

Contributing tests

This file is covered by 589 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.properties.converter;
21   
22    import java.lang.reflect.Type;
23   
24    import org.xwiki.component.util.ReflectionUtils;
25   
26    /**
27    * Helper base class for a {@link Converter} component.
28    * <p>
29    * Commonly a new component is supposed to implements {@link AbstractConverter#convertToString(Object)} and
30    * {@link AbstractConverter#convertToType(Class, Object)}.
31    *
32    * @param <T> the type in which the provided value has to be converted
33    * @version $Id: 1ab948f1f76935985bc9d433afff884dfda7650b $
34    * @since 2.0M2
35    */
 
36    public abstract class AbstractConverter<T> implements Converter<T>
37    {
 
38  1985215 toggle @Override
39    public <G> G convert(Type targetType, Object sourceValue)
40    {
41  1986862 Class<?> sourceType = sourceValue == null ? null : sourceValue.getClass();
42   
43  1987272 G result;
44  1987099 if (targetType.equals(String.class)) {
45    // Convert --> String
46  35352 result = (G) ((Class) targetType).cast(convertToString((T) sourceValue));
47  1951748 } else if (targetType.equals(sourceType)) {
48    // No conversion necessary
49  1 result = (G) sourceValue;
50    } else {
51    // Convert --> Type
52  1951574 result = (G) convertToType(targetType, sourceValue);
53    }
54   
55  1986156 return result;
56    }
57   
58    /**
59    * Convert the input object into a String.
60    * <p>
61    * <b>N.B.</b>This implementation simply uses the value's <code>toString()</code> method and should be overridden if
62    * a more sophisticated mechanism for <i>conversion to a String</i> is required.
63    *
64    * @param value The input value to be converted.
65    * @return the converted String value.
66    */
 
67  10455 toggle protected String convertToString(T value)
68    {
69  10456 return value.toString();
70    }
71   
72    /**
73    * Convert the input object into an output object of the specified type.
74    * <p>
75    * Typical implementations will provide a minimum of {@code String --> type} conversion.
76    *
77    * @param <G> the type in which the provided value has o be converted
78    * @param targetType Data type to which this value should be converted.
79    * @param value The input value to be converted.
80    * @return The converted value.
81    * @since 3.0M1
82    */
 
83  1 toggle protected <G extends T> G convertToType(Type targetType, Object value)
84    {
85  1 Class<G> clazz = ReflectionUtils.getTypeClass(targetType);
86   
87    // Call #convertToType(Class<T> type, Object value) for retro-compatibility
88  1 return convertToType(clazz, value);
89    }
90   
91    /**
92    * Convert the input object into an output object of the specified type.
93    * <p>
94    * Typical implementations will provide a minimum of {@code String --> type} conversion.
95    *
96    * @param <G> the type in which the provided value has o be converted
97    * @param type Data type to which this value should be converted.
98    * @param value The input value to be converted.
99    * @return The converted value.
100    * @deprecated since 3.0M1 overwrite {@link #convertToType(Type, Object)} instead
101    */
 
102  0 toggle @Deprecated
103    protected <G extends T> G convertToType(Class<G> type, Object value)
104    {
105  0 return convertToType((Type) type, value);
106    }
107    }