1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ancientprogramming.fixedformat4j.format.impl;
17
18 import com.ancientprogramming.fixedformat4j.exception.FixedFormatException;
19 import com.ancientprogramming.fixedformat4j.format.AbstractFixedFormatter;
20 import com.ancientprogramming.fixedformat4j.format.FormatInstructions;
21 import org.apache.commons.lang.StringUtils;
22
23
24
25
26
27
28
29 public class BooleanFormatter extends AbstractFixedFormatter<Boolean> {
30
31 public Boolean asObject(String string, FormatInstructions instructions) throws FixedFormatException {
32 Boolean result = false;
33 if (!StringUtils.isEmpty(string)) {
34 if (instructions.getFixedFormatBooleanData().getTrueValue().equals(string)) {
35 result = true;
36 } else if (instructions.getFixedFormatBooleanData().getFalseValue().equals(string)) {
37 result = false;
38 } else {
39 throw new FixedFormatException("Could not convert string[" + string + "] to boolean value according to booleanData[" + instructions.getFixedFormatBooleanData() + "]");
40 }
41 }
42 return result;
43 }
44
45 public String asString(Boolean obj, FormatInstructions instructions) {
46 String result = instructions.getFixedFormatBooleanData().getFalseValue();
47 if (obj != null) {
48 result = obj ? instructions.getFixedFormatBooleanData().getTrueValue() : instructions.getFixedFormatBooleanData().getFalseValue();
49 }
50 return result;
51 }
52
53 }