Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

OpenWiseSolutions / openhub-framework / 1200

12 Feb 2021 - 14:25 coverage decreased (-0.6%) to 70.098%
1200

Pull #129

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Merge c3dd657ef into 382999a38
Pull Request #129: [OHFJIRA-85]: Upgrade to Spring Boot 2.4.2

23 of 67 new or added lines in 12 files covered. (34.33%)

30 existing lines in 4 files now uncovered.

4311 of 6150 relevant lines covered (70.1%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/common/src/main/java/org/openhubframework/openhub/common/converter/TypeConverterEnum.java
UNCOV
1
/*
!
2
 * Copyright 2002-2016 the original author or authors.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package org.openhubframework.openhub.common.converter;
18

19
import java.io.File;
20
import java.io.Serializable;
21
import java.text.NumberFormat;
22
import java.text.ParseException;
23
import java.time.LocalDate;
24
import java.time.format.DateTimeFormatter;
25
import java.util.regex.Pattern;
26

27
import org.springframework.context.i18n.LocaleContextHolder;
28
import org.springframework.util.Assert;
29

30
import org.openhubframework.openhub.common.Tools;
31

32

33
/**
34
 * Enumeration of possible types with conversion functionality.
35
 *
36
 * @author Petr Juza
37
 * @since 2.0
38
 */
39
public enum TypeConverterEnum implements DataTypeConverter {
1×
40

41
    /**
1×
42
     * Text, string (default).
43
     */
44
    STRING(String.class) {
1×
45
        @Override
46
        public String convertToString(Object obj) {
47
            if (obj == null) {
1×
48
                return null;
1×
49
            }
50

51
            Assert.isTrue(String.class.isInstance(obj), Tools.fm("input object's type '{}' doesn't "
1×
52
                    + "corresponds to declared type '{}'", obj.getClass().getSimpleName(), String.class.getSimpleName()));
1×
53

54
            return (String)obj;
1×
55
        }
56

57
        @Override
58
        public Object convertToObject(String str) throws ParseException {
59
            return str;
1×
60
        }
61
    },
62

63
    /**
1×
64
     * Integer, whole number.
65
     */
66
    INT(Integer.class) {
1×
67
        @Override
68
        public String convertToString(Object obj) {
69
            if (obj == null) {
1×
70
                return null;
1×
71
            }
72

73
            Assert.isTrue(Integer.class.isInstance(obj), Tools.fm("input object's type '{}' doesn't "
1×
74
                    + "corresponds to declared type '{}'", obj.getClass().getSimpleName(), Integer.class.getSimpleName()));
1×
75

76
            NumberFormat nf = NumberFormat.getInstance(LocaleContextHolder.getLocale());
1×
77
            return nf.format(obj);
1×
78
        }
79

80
        @Override
81
        public Object convertToObject(String str) throws ParseException {
82
            if (str == null) {
1×
83
                return null;
1×
84
            }
85

86
            NumberFormat nf = NumberFormat.getInstance(LocaleContextHolder.getLocale());
1×
87
            return nf.parse(str).intValue();
1×
88
        }
89
    },
90

91
    /**
1×
92
     * Floating number.
93
     */
94
    FLOAT(Float.class) {
1×
95
        @Override
96
        public String convertToString(Object obj) {
97
            if (obj == null) {
1×
98
                return null;
1×
99
            }
100

101
            Assert.isTrue(Float.class.isInstance(obj), Tools.fm("input object's type '{}' doesn't "
1×
102
                    + "corresponds to declared type '{}'", obj.getClass().getSimpleName(), Float.class.getSimpleName()));
1×
103

104
            NumberFormat nf = NumberFormat.getInstance(LocaleContextHolder.getLocale());
1×
105
            return nf.format(obj);
1×
106
        }
107

108
        @Override
109
        public Object convertToObject(String str) throws ParseException {
110
            if (str == null) {
1×
111
                return null;
1×
112
            }
113

114
            NumberFormat nf = NumberFormat.getInstance(LocaleContextHolder.getLocale());
1×
115
            return nf.parse(str).floatValue();
1×
116
        }
117
    },
118

119
    /**
1×
120
     * Date (without time).
121
     */
122
    DATE(LocalDate.class) {
1×
123
        @Override
124
        public String convertToString(Object obj) {
125
            if (obj == null) {
1×
126
                return null;
1×
127
            }
128

129
            Assert.isTrue(LocalDate.class.isInstance(obj), Tools.fm("input object's type '{}' doesn't "
1×
130
                    + "corresponds to declared type '{}'", obj.getClass().getSimpleName(), LocalDate.class.getSimpleName()));
1×
131

132
            return obj.toString(); // output will be in the ISO-8601 format
1×
133
        }
134

135
        @Override
136
        public Object convertToObject(String str) throws ParseException {
137
            if (str == null) {
1×
138
                return null;
1×
139
            }
140

141
            try {
142
                // str must be in ISO format: 2007-12-03
143
                return LocalDate.parse(str);
1×
144
            } catch (IllegalArgumentException | UnsupportedOperationException e) {
!
145
                // try again with time
146
                return LocalDate.parse(str, DateTimeFormatter.ISO_DATE_TIME);
!
147
            }
148
        }
149
    },
150

151
    /**
1×
152
     * Boolean.
153
     */
154
    BOOLEAN(Boolean.class) {
1×
155
        @Override
156
        public String convertToString(Object obj) {
157
            if (obj == null) {
1×
158
                return null;
1×
159
            }
160

161
            Assert.isTrue(Boolean.class.isInstance(obj), Tools.fm("input object's type '{}' doesn't "
1×
162
                    + "corresponds to declared type '{}'", obj.getClass().getSimpleName(), Boolean.class.getSimpleName()));
1×
163

164
            return obj.toString();
1×
165
        }
166

167
        @Override
168
        public Object convertToObject(String str) throws ParseException {
169
            if (str == null) {
1×
170
                return null;
1×
171
            }
172

173
            return Boolean.valueOf(str);
1×
174
        }
175
    },
176

177
    /**
1×
178
     * File.
179
     */
180
    FILE(File.class) {
1×
181
        @Override
182
        public String convertToString(Object obj) {
183
            if (obj == null) {
1×
184
                return null;
1×
185
            }
186

187
            Assert.isTrue(File.class.isInstance(obj), Tools.fm("input object's type '{}' doesn't "
1×
188
                    + "corresponds to declared type '{}'", obj.getClass().getSimpleName(), File.class.getSimpleName()));
1×
189

190
            return ((File)obj).getPath();
1×
191
        }
192

193
        @Override
194
        public Object convertToObject(String str) throws ParseException {
195
            if (str == null) {
1×
196
                return null;
1×
197
            }
198

199
            return new File(str);
1×
200
        }
201
    },
202

203
    /**
1×
204
     * Regular pattern.
205
     */
206
    PATTERN(Pattern.class) {
1×
207
        @Override
208
        public String convertToString(Object obj) {
209
            if (obj == null) {
1×
210
                return null;
1×
211
            }
212

213
            Assert.isTrue(Pattern.class.isInstance(obj), Tools.fm("input object's type '{}' doesn't "
1×
214
                    + "corresponds to declared type '{}'", obj.getClass().getSimpleName(), Pattern.class.getSimpleName()));
1×
215

216
            return ((Pattern)obj).pattern();
1×
217
        }
218

219
        @Override
220
        public Object convertToObject(String str) throws ParseException {
221
            if (str == null) {
1×
222
                return null;
1×
223
            }
224

225
            return Pattern.compile(str);
1×
226
        }
227
    };
228

229
    private Class<?> typeClass;
230

231
    TypeConverterEnum(Class<?> typeClass) {
1×
232
        this.typeClass = typeClass;
1×
233
    }
1×
234

235
    /**
236
     * Gets class of data type.
237
     *
238
     * @return class of data type
239
     */
240
    public Class<?> getTypeClass() {
241
        return typeClass;
1×
242
    }
243

244
    /**
245
     * Gets {@link TypeConverterEnum} by its class.
246
     *
247
     * @param clazz Requested class
248
     * @return type of property
249
     */
250
    public static TypeConverterEnum typeOf(Class<? extends Serializable> clazz) {
251
        for (TypeConverterEnum dataType : values()) {
!
252
            if (dataType.getTypeClass().equals(clazz)) {
!
253
                return dataType;
!
254
            }
255
        }
256

257
        throw new IllegalStateException("There is no DataTypeEnum with class" + clazz);
!
258
    }
259
}
260

Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2021 Coveralls, Inc