1 package com.github.mygreen.supercsv.cellprocessor.format;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.LinkedHashSet;
6 import java.util.Map;
7 import java.util.Objects;
8 import java.util.Set;
9
10
11
12
13
14
15
16
17 public class BooleanFormatter extends AbstractTextFormatter<Boolean> {
18
19 private static final String[] DEFAULT_READ_TRUE_VALUES = new String[] {"true", "1", "yes", "on", "y", "t"};
20 private static final String[] DEFAULT_READ_FALSE_VALUES = new String[] {"false", "0", "no", "off", "f", "n"};
21
22 private static final String DEFAULT_WRITE_TRUE_VALUE = "true";
23 private static final String DEFAULT_WRITE_FALSE_VALUE = "false";
24
25 private final Set<String> readTrueValues;
26
27 private final Set<String> readFalseValues;
28
29 private final String writeTrueValue;
30
31 private final String writeFalseValue;
32
33 private boolean ignoreCase;
34
35 private boolean failToFalse;
36
37 public BooleanFormatter() {
38 this(DEFAULT_READ_TRUE_VALUES, DEFAULT_READ_FALSE_VALUES,
39 DEFAULT_WRITE_TRUE_VALUE, DEFAULT_WRITE_FALSE_VALUE,
40 false, false);
41 }
42
43 public BooleanFormatter(final String[] readTrueValues, final String[] readFalseValues,
44 final String writeTrueValue, final String writeFalseValue,
45 final boolean ignoreCase, boolean failToFalse) {
46
47 Objects.requireNonNull(readTrueValues);
48 Objects.requireNonNull(readFalseValues);
49
50 this.readTrueValues = toSet(readTrueValues);
51 this.readFalseValues = toSet(readFalseValues);
52 this.writeTrueValue = writeTrueValue;
53 this.writeFalseValue = writeFalseValue;
54 this.ignoreCase = ignoreCase;
55 this.failToFalse = failToFalse;
56 }
57
58 private static Set<String> toSet(final String[] values) {
59
60 Set<String> set = new LinkedHashSet<>();
61 Collections.addAll(set, values);
62 return Collections.unmodifiableSet(set);
63
64 }
65
66 @Override
67 public Boolean parse(final String text) {
68
69 if(contains(readTrueValues, text, ignoreCase) ) {
70 return Boolean.TRUE;
71
72 } else if(contains(readFalseValues, text, ignoreCase) ) {
73 return Boolean.FALSE;
74
75 } else {
76 if(failToFalse) {
77 return Boolean.FALSE;
78 } else {
79 throw new TextParseException(text, Boolean.class,
80 String.format("'%s' could not be parsed as a Boolean", text));
81 }
82 }
83 }
84
85 private static boolean contains(final Set<String> set, final String value, final boolean ignoreCase) {
86
87 if(ignoreCase) {
88 for(String element : set) {
89 if(element.equalsIgnoreCase(value)) {
90 return true;
91 }
92 }
93
94 return false;
95 } else {
96 return set.contains(value);
97 }
98
99 }
100
101 @Override
102 public String print(final Boolean object) {
103
104 return object ? writeTrueValue : writeFalseValue;
105
106 }
107
108 @Override
109 public Map<String, Object> getMessageVariables() {
110
111 final Map<String, Object> vars = new HashMap<>();
112 vars.put("trueValues", readTrueValues);
113 vars.put("falseValues", readFalseValues);
114
115 vars.put("ignoreCase", ignoreCase);
116 vars.put("failToFalse", failToFalse);
117 return vars;
118
119 }
120
121 }