01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.uilib.list.util.converter;
16:
17: import org.araneaframework.uilib.list.util.Converter;
18:
19: /**
20: * Not-null converter for converting <code>Boolean</code> values into
21: * <code>String</code> values and vice-versa.
22: */
23: public class BooleanToStringConverter implements Converter {
24: private static final long serialVersionUID = 1L;
25:
26: public static final Converter YN_UPPER_CONVERTER = new BooleanToStringConverter(
27: "Y", "N");
28: public static final Converter YN_LOWER_CONVERTER = new BooleanToStringConverter(
29: "y", "n");
30:
31: protected String trueValue;
32: protected String falseValue;
33:
34: public BooleanToStringConverter(String trueValue, String falseValue) {
35: if (trueValue == null || falseValue == null) {
36: throw new ConversionException(
37: "Target values can not be null");
38: }
39: if (trueValue.equals(falseValue)) {
40: throw new ConversionException(
41: "Target values can not be the same");
42: }
43: this .trueValue = trueValue;
44: this .falseValue = falseValue;
45: }
46:
47: public Object convert(Object data) throws ConversionException {
48: return convert((Boolean) data);
49: }
50:
51: public String convert(Boolean data) throws ConversionException {
52: if (data == null) {
53: throw new ConversionException("Data can not be null");
54: }
55: return Boolean.TRUE.equals(data) ? this .trueValue
56: : this .falseValue;
57: }
58:
59: public Object reverseConvert(Object data)
60: throws ConversionException {
61: if (data == null) {
62: throw new ConversionException("Data can not be null");
63: }
64: if (this .trueValue.equals(data)) {
65: return Boolean.TRUE;
66: }
67: if (this .falseValue.equals(data)) {
68: return Boolean.FALSE;
69: }
70: throw new ConversionException("Data " + data + " not supported");
71: }
72:
73: public Class getSourceType() {
74: return Boolean.class;
75: }
76:
77: public Class getDestinationType() {
78: return String.class;
79: }
80: }
|