1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.properties.internal.converter

File EnumConverter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
23
2
1
95
49
6
0.26
11.5
2
3

Classes

Class Line # Actions
EnumConverter 38 23 0% 6 2
0.9393939493.9%
 

Contributing tests

This file is covered by 618 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.internal.converter;
21   
22    import java.lang.reflect.Type;
23   
24    import javax.inject.Singleton;
25   
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.properties.converter.AbstractConverter;
28    import org.xwiki.properties.converter.ConversionException;
29   
30    /**
31    * Converter that converts a value into an enumeration class value.
32    *
33    * @version $Id: edd984364e759ae42a24db62ec5b9f5e6260ad52 $
34    * @since 5.2M1
35    */
36    @Component
37    @Singleton
 
38    public class EnumConverter extends AbstractConverter<Enum>
39    {
 
40  45983 toggle @Override
41    protected <E extends Enum> E convertToType(Type type, Object value)
42    {
43  45982 if (value != null) {
44  45981 Object[] enumValues = ((Class<?>) type).getEnumConstants();
45   
46  45982 String testValue = value.toString();
47  45983 for (Object enumValue : enumValues) {
48    // Conversion is case insensitive
49  81001 if (enumValue.toString().equalsIgnoreCase(testValue)) {
50  45982 return (E) enumValue;
51    }
52    }
53   
54  1 throw new ConversionException(generateInvalidErrorMessage(enumValues, testValue));
55    } else {
56  1 return null;
57    }
58    }
59   
60    /**
61    * Generate error message to use in the {@link ConversionException}.
62    *
63    * @param enumValues possible values of the enum.
64    * @param testValue the value to convert.
65    * @return the generated error message.
66    */
 
67  1 toggle private String generateInvalidErrorMessage(Object[] enumValues, String testValue)
68    {
69  1 StringBuffer errorMessage = new StringBuffer("Unable to convert value [" + testValue + "].");
70   
71  1 errorMessage.append(" Allowed values are (case insensitive) ");
72   
73  1 StringBuffer valueList = new StringBuffer();
74   
75  1 int index = 1;
76  1 for (Object enumValue : enumValues) {
77  2 if (valueList.length() > 0) {
78  1 if (++index == enumValues.length) {
79  1 valueList.append(" or ");
80    } else {
81  0 valueList.append(", ");
82    }
83    }
84   
85  2 valueList.append('"');
86  2 valueList.append(enumValue);
87  2 valueList.append('"');
88    }
89   
90  1 errorMessage.append(valueList);
91  1 errorMessage.append('.');
92   
93  1 return errorMessage.toString();
94    }
95    }