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 BooleanToLongConverter implements Converter {
24: private static final long serialVersionUID = 1L;
25:
26: public static final Converter ONE_ZERO_CONVERTER = new BooleanToLongConverter(
27: new Long(1), new Long(0));
28:
29: protected Long trueValue;
30: protected Long falseValue;
31:
32: public BooleanToLongConverter(Long trueValue, Long falseValue) {
33: if (trueValue == null || falseValue == null) {
34: throw new ConversionException(
35: "Target values can not be null");
36: }
37: if (trueValue == falseValue || trueValue.equals(falseValue)) {
38: throw new ConversionException(
39: "Target values can not be the same");
40: }
41: this .trueValue = trueValue;
42: this .falseValue = falseValue;
43: }
44:
45: public Object convert(Object data) throws ConversionException {
46: return convert((Boolean) data);
47: }
48:
49: public Long convert(Boolean data) throws ConversionException {
50: if (data == null) {
51: throw new ConversionException("Data can not be null");
52: }
53: return Boolean.TRUE.equals(data) ? this .trueValue
54: : this .falseValue;
55: }
56:
57: public Object reverseConvert(Object data)
58: throws ConversionException {
59: if (data == null) {
60: throw new ConversionException("Data can not be null");
61: }
62: if (this .trueValue.equals(data)) {
63: return Boolean.TRUE;
64: }
65: if (this .falseValue.equals(data)) {
66: return Boolean.FALSE;
67: }
68: throw new ConversionException("Data " + data + " not supported");
69: }
70:
71: public Class getSourceType() {
72: return Boolean.class;
73: }
74:
75: public Class getDestinationType() {
76: return Long.class;
77: }
78: }
|