Completed
Branch master (dc3042)
by Zaahid
03:52
created

getTypeFrom(int)   F

Complexity

Conditions 28

Size

Total Lines 46
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 812

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 46
c 1
b 0
f 0
dl 0
loc 46
ccs 0
cts 46
cp 0
crap 812
rs 0

How to fix   Complexity   

Complexity

Complex classes like com.strider.datadefender.database.metadata.SqlTypeToClass.getTypeFrom(int) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*
2
 * Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
3
 * by the @authors tag. See the copyright.txt in the distribution for a
4
 * full listing of individual contributors.
5
 *
6
 * This is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
 * Lesser General Public License for more details.
15
 */
16
package com.strider.datadefender.database.metadata;
17
18
import java.math.BigDecimal;
19
import java.sql.Array;
20
import java.sql.Blob;
21
import java.sql.Clob;
22
import java.sql.Date;
23
import java.sql.Time;
24
import java.sql.Timestamp;
25
import java.sql.Types;
26
27
/**
28
 * Maps java.sql.Types to Class objects.
29
 *
30
 * @author Zaahid Bateson <[email protected]>
31
 */
32
public class SqlTypeToClass {
33
    public Class getTypeFrom(int type) {
34
        switch (type) {
35
            case Types.CHAR:
36
            case Types.VARCHAR:
37
            case Types.LONGVARCHAR:
38
            case Types.NCHAR:
39
            case Types.NVARCHAR:
40
            case Types.LONGNVARCHAR:
41
                return String.class;
42
            case Types.BINARY:
43
            case Types.VARBINARY:
44
            case Types.LONGVARBINARY:
45
                return Byte[].class;
46
            case Types.NUMERIC:
47
            case Types.DECIMAL:
48
                return BigDecimal.class;
49
            case Types.BIT:
50
                return Boolean.class;
51
            case Types.TINYINT:
52
            case Types.SMALLINT:
53
                return Short.class;
54
            case Types.INTEGER:
55
                return Integer.class;
56
            case Types.BIGINT:
57
                return Long.class;
58
            case Types.REAL:
59
                return Float.class;
60
            case Types.FLOAT:
61
            case Types.DOUBLE:
62
                return Double.class;
63
            case Types.DATE:
64
                return Date.class;
65
            case Types.TIME:
66
                return Time.class;
67
            case Types.TIMESTAMP:
68
                return Timestamp.class;
69
            case Types.BLOB:
70
                return Blob.class;
71
            case Types.CLOB:
72
                return Clob.class;
73
            case Types.ARRAY:
74
                return Array.class;
75
            case Types.NULL:
76
                return null;
77
            default:
78
                throw new IllegalArgumentException("Unsupported or unknown type");
79
        }
80
    }
81
}
82