blob: fcd8f4a311d70a969649559dd47961b5511f448d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*******************************************************************************
* Copyright (c) 2007 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Contributors:
* Oracle - initial API and implementation
******************************************************************************/
package org.eclipse.jpt.utility.internal;
/**
* Used by various "pluggable" classes to transform objects
* into strings and vice versa.
*
* If anyone can come up with a better class name
* and/or method name, I would love to hear it. ~bjv
*/
public interface BidiStringConverter<T> extends StringConverter<T> {
/**
* Convert the specified string into an object.
* The semantics of "convert to object" is determined by the
* contract between the client and the server.
* Typically, if the string is null, null is returned.
*/
T convertToObject(String s);
final class Default<S> implements BidiStringConverter<S> {
@SuppressWarnings("unchecked")
public static final BidiStringConverter INSTANCE = new Default();
@SuppressWarnings("unchecked")
public static <R> BidiStringConverter<R> instance() {
return INSTANCE;
}
// ensure single instance
private Default() {
super();
}
// simply return the object's #toString() result
public String convertToString(S o) {
return (o == null) ? null : o.toString();
}
// simply return the string
@SuppressWarnings("unchecked")
public S convertToObject(String s) {
return (S) s;
}
@Override
public String toString() {
return "BidiStringConverter.Default";
}
}
final class Disabled<S> implements BidiStringConverter<S> {
@SuppressWarnings("unchecked")
public static final BidiStringConverter INSTANCE = new Disabled();
@SuppressWarnings("unchecked")
public static <R> BidiStringConverter<R> instance() {
return INSTANCE;
}
// ensure single instance
private Disabled() {
super();
}
// throw an exception
public String convertToString(S o) {
throw new UnsupportedOperationException();
}
// throw an exception
public S convertToObject(String s) {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return "BidiStringConverter.Disabled";
}
}
final class BooleanConverter implements BidiStringConverter<Boolean> {
public static final BidiStringConverter<Boolean> INSTANCE = new BooleanConverter();
public static BidiStringConverter<Boolean> instance() {
return INSTANCE;
}
// ensure single instance
private BooleanConverter() {
super();
}
/** Return "true" if the Boolean is true, otherwise return "false". */
public String convertToString(Boolean b) {
return (b == null) ? null : b.toString();
}
/** Return Boolean.TRUE if the string is "true" (case-insensitive), otherwise return Boolean.FALSE. */
public Boolean convertToObject(String s) {
return (s == null) ? null : Boolean.valueOf(s);
}
@Override
public String toString() {
return "BidiStringConverter.BooleanConverter";
}
}
final class IntegerConverter implements BidiStringConverter<Integer> {
public static final BidiStringConverter<Integer> INSTANCE = new IntegerConverter();
public static BidiStringConverter<Integer> instance() {
return INSTANCE;
}
// ensure single instance
private IntegerConverter() {
super();
}
/** Integer's #toString() works well. */
public String convertToString(Integer integer) {
return (integer == null) ? null : integer.toString();
}
/** Convert the string to an Integer, if possible. */
public Integer convertToObject(String s) {
return (s == null) ? null : Integer.valueOf(s);
}
@Override
public String toString() {
return "BidiStringConverter.IntegerConverter";
}
}
}
|