| Total Complexity | 6 |
| Total Lines | 30 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | package es.webbeta.serializer; |
||
| 7 | public class FieldFormatter implements es.webbeta.serializer.base.FieldFormatter { |
||
|
|
|||
| 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 | } |
||
| 42 |