es.webbeta.serializer.FieldFormatter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 30
c 1
b 0
f 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A format(String) 0 18 4
A FieldFormatter(FieldFormatterType) 0 2 1
A FieldFormatter(String) 0 2 1
1
package es.webbeta.serializer;
2
3
import com.google.common.base.Ascii;
4
import com.google.common.base.CaseFormat;
5
import es.webbeta.serializer.type.FieldFormatterType;
6
7
public class FieldFormatter implements es.webbeta.serializer.base.FieldFormatter {
0 ignored issues
show
Comprehensibility introduced by
Class or interface names should not shadow other classes or interfaces. In general, shadowing is a bad practice as it makes code harder to understand. Consider renaming this class.
Loading history...
8
9
    private FieldFormatterType formatterType;
10
11
    public FieldFormatter(FieldFormatterType type) {
12
        formatterType = type;
13
    }
14
15
    public FieldFormatter(String type) {
16
        this(FieldFormatterType.fromString(type));
17
    }
18
19
    public String format(String name) {
20
        if (name.contains(" "))
21
            throw new IllegalArgumentException("A field cannot have empty spaces.");
22
23
        if (formatterType == FieldFormatterType.INHERITED)
24
            return name;
25
        else {
26
            boolean hasDashOrUnderscore = name.contains("-") || name.contains("_");
27
28
            if (hasDashOrUnderscore) {
29
                name = name.replace("-", "_");
30
                name = name.toLowerCase();
31
32
                return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.valueOf(formatterType.getStringType().toUpperCase()), name);
33
            } else {
34
                name = String.valueOf(Ascii.toLowerCase(name.charAt(0))) + name.substring(1);
35
36
                return CaseFormat.LOWER_CAMEL.to(CaseFormat.valueOf(formatterType.getStringType().toUpperCase()), name);
37
            }
38
        }
39
    }
40
41
}
42