4544 |
25 Sep 08 |
nicklas |
1 |
/** |
4544 |
25 Sep 08 |
nicklas |
$Id $ |
4544 |
25 Sep 08 |
nicklas |
3 |
|
4544 |
25 Sep 08 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4544 |
25 Sep 08 |
nicklas |
5 |
|
4544 |
25 Sep 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4544 |
25 Sep 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4544 |
25 Sep 08 |
nicklas |
8 |
|
4544 |
25 Sep 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4544 |
25 Sep 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4544 |
25 Sep 08 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4544 |
25 Sep 08 |
nicklas |
of the License, or (at your option) any later version. |
4544 |
25 Sep 08 |
nicklas |
13 |
|
4544 |
25 Sep 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4544 |
25 Sep 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4544 |
25 Sep 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4544 |
25 Sep 08 |
nicklas |
GNU General Public License for more details. |
4544 |
25 Sep 08 |
nicklas |
18 |
|
4544 |
25 Sep 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4544 |
25 Sep 08 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4544 |
25 Sep 08 |
nicklas |
21 |
*/ |
4544 |
25 Sep 08 |
nicklas |
22 |
package net.sf.basedb.util.formatter; |
4544 |
25 Sep 08 |
nicklas |
23 |
|
4544 |
25 Sep 08 |
nicklas |
24 |
|
4544 |
25 Sep 08 |
nicklas |
25 |
/** |
4544 |
25 Sep 08 |
nicklas |
Formats a value with optional prefix and suffix. This formatter uses |
4544 |
25 Sep 08 |
nicklas |
a parent formatter to format the actual value and then adds |
4544 |
25 Sep 08 |
nicklas |
prefix and suffix as needed to the start and end of the |
4544 |
25 Sep 08 |
nicklas |
formatted value. |
4544 |
25 Sep 08 |
nicklas |
<p> |
4544 |
25 Sep 08 |
nicklas |
The {@link #parseString(String)} method will first check if |
4544 |
25 Sep 08 |
nicklas |
the string start or ends with the given prefix/suffix. If it does, |
4544 |
25 Sep 08 |
nicklas |
the prefix/suffix is removed before parsing is delegated to the |
4544 |
25 Sep 08 |
nicklas |
parent. |
4544 |
25 Sep 08 |
nicklas |
35 |
|
4544 |
25 Sep 08 |
nicklas |
@author nicklas |
4544 |
25 Sep 08 |
nicklas |
@version 2.9 |
4544 |
25 Sep 08 |
nicklas |
@base.modified $Date$ |
4544 |
25 Sep 08 |
nicklas |
39 |
*/ |
4544 |
25 Sep 08 |
nicklas |
40 |
public class PrefixSuffixFormatter<T> |
4544 |
25 Sep 08 |
nicklas |
41 |
implements Formatter<T> |
4544 |
25 Sep 08 |
nicklas |
42 |
{ |
4544 |
25 Sep 08 |
nicklas |
43 |
|
4544 |
25 Sep 08 |
nicklas |
44 |
/** |
4544 |
25 Sep 08 |
nicklas |
The character representing a non-breaking space. Usefule to |
4544 |
25 Sep 08 |
nicklas |
avoid line wrapping between prefix/suffix and value. |
4544 |
25 Sep 08 |
nicklas |
47 |
*/ |
4544 |
25 Sep 08 |
nicklas |
48 |
public static final char NBSP = 0xA0; |
4544 |
25 Sep 08 |
nicklas |
49 |
|
4544 |
25 Sep 08 |
nicklas |
50 |
private final String prefix; |
4544 |
25 Sep 08 |
nicklas |
51 |
private final Formatter<T> parent; |
4544 |
25 Sep 08 |
nicklas |
52 |
private final String suffix; |
6268 |
11 Apr 13 |
nicklas |
53 |
private final boolean usePrefixForNull; |
6268 |
11 Apr 13 |
nicklas |
54 |
private final boolean useSuffixForNull; |
4544 |
25 Sep 08 |
nicklas |
55 |
|
4544 |
25 Sep 08 |
nicklas |
56 |
/** |
6268 |
11 Apr 13 |
nicklas |
Create a new formatter. Prefix and suffix are included for null values. |
4544 |
25 Sep 08 |
nicklas |
@param prefix The prefix, or null to not use any prefix |
4544 |
25 Sep 08 |
nicklas |
@param parent The parent formatter, if null a {@link ToStringFormatter} |
4544 |
25 Sep 08 |
nicklas |
is automatically created |
4544 |
25 Sep 08 |
nicklas |
@param suffix The suffix, or null to not use any suffix |
4544 |
25 Sep 08 |
nicklas |
62 |
*/ |
4544 |
25 Sep 08 |
nicklas |
63 |
public PrefixSuffixFormatter(String prefix, Formatter<T> parent, String suffix) |
4544 |
25 Sep 08 |
nicklas |
64 |
{ |
6268 |
11 Apr 13 |
nicklas |
65 |
this(prefix, true, parent, suffix, true); |
6268 |
11 Apr 13 |
nicklas |
66 |
} |
6268 |
11 Apr 13 |
nicklas |
67 |
|
6268 |
11 Apr 13 |
nicklas |
68 |
/** |
6268 |
11 Apr 13 |
nicklas |
Create a new formatter. |
6268 |
11 Apr 13 |
nicklas |
@param prefix The prefix, or null to not use any prefix |
6268 |
11 Apr 13 |
nicklas |
@param usePrefixForNull If set, the prefix is always included, otherwise only for non-null |
6268 |
11 Apr 13 |
nicklas |
values |
6268 |
11 Apr 13 |
nicklas |
@param parent The parent formatter, if null a {@link ToStringFormatter} |
6268 |
11 Apr 13 |
nicklas |
is automatically created |
6268 |
11 Apr 13 |
nicklas |
@param suffix The suffix, or null to not use any suffix |
6268 |
11 Apr 13 |
nicklas |
@param useSuffixForNull If set, the suffix is always included, otherwise only for non-null |
6268 |
11 Apr 13 |
nicklas |
values |
6268 |
11 Apr 13 |
nicklas |
78 |
*/ |
6268 |
11 Apr 13 |
nicklas |
79 |
public PrefixSuffixFormatter(String prefix, boolean usePrefixForNull, Formatter<T> parent, String suffix, boolean useSuffixForNull) |
6268 |
11 Apr 13 |
nicklas |
80 |
{ |
4544 |
25 Sep 08 |
nicklas |
81 |
this.prefix = prefix == null ? "" : prefix; |
6268 |
11 Apr 13 |
nicklas |
82 |
this.usePrefixForNull = usePrefixForNull; |
4544 |
25 Sep 08 |
nicklas |
83 |
this.parent = parent == null ? new ToStringFormatter<T>() : parent; |
4544 |
25 Sep 08 |
nicklas |
84 |
this.suffix = suffix == null ? "" : suffix; |
6268 |
11 Apr 13 |
nicklas |
85 |
this.useSuffixForNull = useSuffixForNull; |
4544 |
25 Sep 08 |
nicklas |
86 |
} |
6268 |
11 Apr 13 |
nicklas |
87 |
|
4544 |
25 Sep 08 |
nicklas |
88 |
|
4544 |
25 Sep 08 |
nicklas |
89 |
/* |
4544 |
25 Sep 08 |
nicklas |
From the Formatter interface |
4544 |
25 Sep 08 |
nicklas |
91 |
------------------------------------------- |
4544 |
25 Sep 08 |
nicklas |
92 |
*/ |
6127 |
14 Sep 12 |
nicklas |
93 |
@Override |
4544 |
25 Sep 08 |
nicklas |
94 |
public String format(T value) |
4544 |
25 Sep 08 |
nicklas |
95 |
{ |
6268 |
11 Apr 13 |
nicklas |
96 |
StringBuilder sb = new StringBuilder(); |
6268 |
11 Apr 13 |
nicklas |
97 |
if (value != null || usePrefixForNull) sb.append(prefix); |
6268 |
11 Apr 13 |
nicklas |
98 |
sb.append(parent.format(value)); |
6268 |
11 Apr 13 |
nicklas |
99 |
if (value != null || useSuffixForNull) sb.append(suffix); |
6268 |
11 Apr 13 |
nicklas |
100 |
return sb.toString(); |
4544 |
25 Sep 08 |
nicklas |
101 |
} |
6127 |
14 Sep 12 |
nicklas |
102 |
@Override |
4544 |
25 Sep 08 |
nicklas |
103 |
public T parseString(String value) |
4544 |
25 Sep 08 |
nicklas |
104 |
{ |
4544 |
25 Sep 08 |
nicklas |
105 |
if (value != null) |
4544 |
25 Sep 08 |
nicklas |
106 |
{ |
4544 |
25 Sep 08 |
nicklas |
107 |
if (prefix.length() > 0 && value.startsWith(prefix)) |
4544 |
25 Sep 08 |
nicklas |
108 |
{ |
4544 |
25 Sep 08 |
nicklas |
109 |
value = value.substring(prefix.length()); |
4544 |
25 Sep 08 |
nicklas |
110 |
} |
4544 |
25 Sep 08 |
nicklas |
111 |
if (suffix.length() > 0 && value.endsWith(suffix)) |
4544 |
25 Sep 08 |
nicklas |
112 |
{ |
4544 |
25 Sep 08 |
nicklas |
113 |
value = value.substring(0, value.length()-suffix.length()); |
4544 |
25 Sep 08 |
nicklas |
114 |
} |
4544 |
25 Sep 08 |
nicklas |
115 |
} |
4544 |
25 Sep 08 |
nicklas |
116 |
return parent.parseString(value); |
4544 |
25 Sep 08 |
nicklas |
117 |
} |
4544 |
25 Sep 08 |
nicklas |
118 |
// ------------------------------------------- |
4544 |
25 Sep 08 |
nicklas |
119 |
|
4544 |
25 Sep 08 |
nicklas |
120 |
} |