2203 |
28 Apr 06 |
nicklas |
1 |
/* |
2203 |
28 Apr 06 |
nicklas |
$Id$ |
2203 |
28 Apr 06 |
nicklas |
3 |
|
2203 |
28 Apr 06 |
nicklas |
Copyright (C) 2006 Nicklas Nordborg |
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Johan Enell, Nicklas Nordborg |
2203 |
28 Apr 06 |
nicklas |
6 |
|
2203 |
28 Apr 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2203 |
28 Apr 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2203 |
28 Apr 06 |
nicklas |
9 |
|
2203 |
28 Apr 06 |
nicklas |
BASE is free software; you can redistribute it and/or modify it |
2203 |
28 Apr 06 |
nicklas |
under the terms of the GNU General Public License as published by |
4479 |
05 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
2203 |
28 Apr 06 |
nicklas |
(at your option) any later version. |
2203 |
28 Apr 06 |
nicklas |
14 |
|
2203 |
28 Apr 06 |
nicklas |
BASE is distributed in the hope that it will be useful, but |
2203 |
28 Apr 06 |
nicklas |
WITHOUT ANY WARRANTY; without even the implied warranty of |
2203 |
28 Apr 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2203 |
28 Apr 06 |
nicklas |
General Public License for more details. |
2203 |
28 Apr 06 |
nicklas |
19 |
|
2203 |
28 Apr 06 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
2203 |
28 Apr 06 |
nicklas |
22 |
*/ |
2203 |
28 Apr 06 |
nicklas |
23 |
package net.sf.basedb.util.parser; |
2203 |
28 Apr 06 |
nicklas |
24 |
|
3023 |
12 Dec 06 |
nicklas |
25 |
import java.text.NumberFormat; |
7667 |
21 Mar 19 |
nicklas |
26 |
import java.util.Date; |
3023 |
12 Dec 06 |
nicklas |
27 |
|
7667 |
21 Mar 19 |
nicklas |
28 |
import net.sf.basedb.core.DateUtil; |
3264 |
23 Apr 07 |
enell |
29 |
import net.sf.basedb.core.Type; |
7667 |
21 Mar 19 |
nicklas |
30 |
import net.sf.basedb.util.formatter.Formatter; |
2203 |
28 Apr 06 |
nicklas |
31 |
import net.sf.basedb.util.parser.FlatFileParser.Data; |
2203 |
28 Apr 06 |
nicklas |
32 |
|
2203 |
28 Apr 06 |
nicklas |
33 |
/** |
2203 |
28 Apr 06 |
nicklas |
A constant string mapper. The <code>getValue()</code> always |
2203 |
28 Apr 06 |
nicklas |
return the same string which is set in the contructor. |
2203 |
28 Apr 06 |
nicklas |
36 |
|
2203 |
28 Apr 06 |
nicklas |
@author nicklas |
2203 |
28 Apr 06 |
nicklas |
@version 2.0 |
2302 |
22 May 06 |
nicklas |
@base.modified $Date$ |
2203 |
28 Apr 06 |
nicklas |
40 |
*/ |
2203 |
28 Apr 06 |
nicklas |
41 |
public class ConstantMapper |
2203 |
28 Apr 06 |
nicklas |
42 |
implements Mapper |
2203 |
28 Apr 06 |
nicklas |
43 |
{ |
2203 |
28 Apr 06 |
nicklas |
44 |
private final String constant; |
3264 |
23 Apr 07 |
enell |
45 |
private final NumberFormat parser; |
7667 |
21 Mar 19 |
nicklas |
46 |
private final Formatter<Date> dateParser; |
3472 |
11 Jun 07 |
nicklas |
47 |
private final boolean nullIfException; |
3264 |
23 Apr 07 |
enell |
48 |
private boolean needToParseNumeric; |
7667 |
21 Mar 19 |
nicklas |
49 |
private boolean needToParseDate; |
7668 |
21 Mar 19 |
nicklas |
50 |
private Double asDouble; |
7668 |
21 Mar 19 |
nicklas |
51 |
private Long asLong; |
7667 |
21 Mar 19 |
nicklas |
52 |
private Date asDate; |
7667 |
21 Mar 19 |
nicklas |
53 |
private RuntimeException parseException; |
2203 |
28 Apr 06 |
nicklas |
54 |
|
3023 |
12 Dec 06 |
nicklas |
55 |
/** |
3023 |
12 Dec 06 |
nicklas |
Create a constant mapper. |
4026 |
30 Nov 07 |
martin |
@param constant The constant expression. |
3023 |
12 Dec 06 |
nicklas |
58 |
*/ |
2203 |
28 Apr 06 |
nicklas |
59 |
public ConstantMapper(String constant) |
2203 |
28 Apr 06 |
nicklas |
60 |
{ |
5590 |
16 Mar 11 |
nicklas |
61 |
this(constant, null, false); |
3023 |
12 Dec 06 |
nicklas |
62 |
} |
3023 |
12 Dec 06 |
nicklas |
63 |
|
3023 |
12 Dec 06 |
nicklas |
64 |
/** |
3023 |
12 Dec 06 |
nicklas |
Create a constant mapper using a specific number formatter as it's parser. |
3023 |
12 Dec 06 |
nicklas |
@param constant The constant expression |
3590 |
23 Jul 07 |
nicklas |
@param parser The number format to use or null to use Double.valueOf() |
3472 |
11 Jun 07 |
nicklas |
@param nullIfException If TRUE, the mapper returns null for unparsable numeric |
3472 |
11 Jun 07 |
nicklas |
values, otherwise an excption is thrown |
3472 |
11 Jun 07 |
nicklas |
@since 2.4 |
3472 |
11 Jun 07 |
nicklas |
71 |
*/ |
3472 |
11 Jun 07 |
nicklas |
72 |
public ConstantMapper(String constant, NumberFormat parser, boolean nullIfException) |
3472 |
11 Jun 07 |
nicklas |
73 |
{ |
7667 |
21 Mar 19 |
nicklas |
74 |
this(constant, parser, null, nullIfException); |
7667 |
21 Mar 19 |
nicklas |
75 |
} |
7667 |
21 Mar 19 |
nicklas |
76 |
|
7667 |
21 Mar 19 |
nicklas |
77 |
/** |
7667 |
21 Mar 19 |
nicklas |
Create a constant mapper using a specific number formatter as it's parser. |
7667 |
21 Mar 19 |
nicklas |
@param constant The constant expression |
7667 |
21 Mar 19 |
nicklas |
@param parser The number format to use or null to use Double.valueOf() |
7667 |
21 Mar 19 |
nicklas |
@param nullIfException If TRUE, the mapper returns null for unparsable numeric |
7667 |
21 Mar 19 |
nicklas |
values, otherwise an excption is thrown |
7667 |
21 Mar 19 |
nicklas |
@since 3.15 |
7667 |
21 Mar 19 |
nicklas |
84 |
*/ |
7667 |
21 Mar 19 |
nicklas |
85 |
public ConstantMapper(String constant, NumberFormat parser, Formatter<Date> dateParser, boolean nullIfException) |
7667 |
21 Mar 19 |
nicklas |
86 |
{ |
2203 |
28 Apr 06 |
nicklas |
87 |
this.constant = constant; |
3264 |
23 Apr 07 |
enell |
88 |
this.parser = parser; |
7667 |
21 Mar 19 |
nicklas |
89 |
this.dateParser = dateParser; |
3472 |
11 Jun 07 |
nicklas |
90 |
this.nullIfException = nullIfException; |
3264 |
23 Apr 07 |
enell |
91 |
this.needToParseNumeric = constant != null; |
7667 |
21 Mar 19 |
nicklas |
92 |
this.needToParseDate = constant != null; |
2203 |
28 Apr 06 |
nicklas |
93 |
} |
2203 |
28 Apr 06 |
nicklas |
94 |
|
2225 |
09 May 06 |
nicklas |
95 |
public ConstantMapper(Integer constant) |
2225 |
09 May 06 |
nicklas |
96 |
{ |
2225 |
09 May 06 |
nicklas |
97 |
this.constant = constant.toString(); |
7668 |
21 Mar 19 |
nicklas |
98 |
this.asLong = constant.longValue(); |
7668 |
21 Mar 19 |
nicklas |
99 |
this.asDouble = constant.doubleValue(); |
3264 |
23 Apr 07 |
enell |
100 |
this.parser = null; |
3264 |
23 Apr 07 |
enell |
101 |
this.needToParseNumeric = false; |
3472 |
11 Jun 07 |
nicklas |
102 |
this.nullIfException = false; |
7667 |
21 Mar 19 |
nicklas |
103 |
this.dateParser = null; |
7667 |
21 Mar 19 |
nicklas |
104 |
this.needToParseDate = false; |
2225 |
09 May 06 |
nicklas |
105 |
} |
2225 |
09 May 06 |
nicklas |
106 |
|
2225 |
09 May 06 |
nicklas |
107 |
public ConstantMapper(Float constant) |
2225 |
09 May 06 |
nicklas |
108 |
{ |
2225 |
09 May 06 |
nicklas |
109 |
this.constant = constant.toString(); |
7668 |
21 Mar 19 |
nicklas |
110 |
this.asDouble = constant.doubleValue(); |
7668 |
21 Mar 19 |
nicklas |
111 |
this.asLong = constant.longValue(); |
3264 |
23 Apr 07 |
enell |
112 |
this.parser = null; |
3264 |
23 Apr 07 |
enell |
113 |
this.needToParseNumeric = false; |
3472 |
11 Jun 07 |
nicklas |
114 |
this.nullIfException = false; |
7667 |
21 Mar 19 |
nicklas |
115 |
this.dateParser = null; |
7667 |
21 Mar 19 |
nicklas |
116 |
this.needToParseDate = false; |
2225 |
09 May 06 |
nicklas |
117 |
} |
2225 |
09 May 06 |
nicklas |
118 |
|
7667 |
21 Mar 19 |
nicklas |
119 |
public ConstantMapper(Date constant, Formatter<Date> dateParser) |
7667 |
21 Mar 19 |
nicklas |
120 |
{ |
7667 |
21 Mar 19 |
nicklas |
121 |
this.constant = dateParser != null ? dateParser.format(constant) : DateUtil.formatDate(constant); |
7667 |
21 Mar 19 |
nicklas |
122 |
this.asDate = constant; |
7668 |
21 Mar 19 |
nicklas |
123 |
this.asDouble = null; |
7668 |
21 Mar 19 |
nicklas |
124 |
this.asLong = null; |
7667 |
21 Mar 19 |
nicklas |
125 |
this.parser = null; |
7667 |
21 Mar 19 |
nicklas |
126 |
this.needToParseNumeric = false; |
7667 |
21 Mar 19 |
nicklas |
127 |
this.nullIfException = false; |
7667 |
21 Mar 19 |
nicklas |
128 |
this.dateParser = dateParser; |
7667 |
21 Mar 19 |
nicklas |
129 |
this.needToParseDate = false; |
7667 |
21 Mar 19 |
nicklas |
130 |
} |
7667 |
21 Mar 19 |
nicklas |
131 |
|
2203 |
28 Apr 06 |
nicklas |
132 |
/* |
2203 |
28 Apr 06 |
nicklas |
From the Mapper interface |
2203 |
28 Apr 06 |
nicklas |
134 |
------------------------------------------- |
2203 |
28 Apr 06 |
nicklas |
135 |
*/ |
2203 |
28 Apr 06 |
nicklas |
136 |
/** |
2203 |
28 Apr 06 |
nicklas |
@return The string set in the constructor |
2203 |
28 Apr 06 |
nicklas |
138 |
*/ |
6127 |
14 Sep 12 |
nicklas |
139 |
@Override |
7666 |
20 Mar 19 |
nicklas |
140 |
@Deprecated |
2203 |
28 Apr 06 |
nicklas |
141 |
public String getValue(Data data) |
2203 |
28 Apr 06 |
nicklas |
142 |
{ |
2203 |
28 Apr 06 |
nicklas |
143 |
return constant; |
2203 |
28 Apr 06 |
nicklas |
144 |
} |
6127 |
14 Sep 12 |
nicklas |
145 |
@Override |
7666 |
20 Mar 19 |
nicklas |
146 |
public String getString(Data data) |
7666 |
20 Mar 19 |
nicklas |
147 |
{ |
7666 |
20 Mar 19 |
nicklas |
148 |
return constant; |
7666 |
20 Mar 19 |
nicklas |
149 |
} |
7666 |
20 Mar 19 |
nicklas |
150 |
@Override |
2225 |
09 May 06 |
nicklas |
151 |
public Integer getInt(Data data) |
2225 |
09 May 06 |
nicklas |
152 |
{ |
7667 |
21 Mar 19 |
nicklas |
153 |
if (needToParseNumeric) parseNumeric(); |
3264 |
23 Apr 07 |
enell |
154 |
if (parseException != null) throw parseException; |
7668 |
21 Mar 19 |
nicklas |
155 |
return asLong == null ? null : asLong.intValue(); |
2225 |
09 May 06 |
nicklas |
156 |
} |
6127 |
14 Sep 12 |
nicklas |
157 |
@Override |
7668 |
21 Mar 19 |
nicklas |
158 |
public Long getLong(Data data) |
7668 |
21 Mar 19 |
nicklas |
159 |
{ |
7668 |
21 Mar 19 |
nicklas |
160 |
if (needToParseNumeric) parseNumeric(); |
7668 |
21 Mar 19 |
nicklas |
161 |
if (parseException != null) throw parseException; |
7668 |
21 Mar 19 |
nicklas |
162 |
return asLong; |
7668 |
21 Mar 19 |
nicklas |
163 |
} |
7668 |
21 Mar 19 |
nicklas |
164 |
@Override |
2225 |
09 May 06 |
nicklas |
165 |
public Float getFloat(Data data) |
2225 |
09 May 06 |
nicklas |
166 |
{ |
7667 |
21 Mar 19 |
nicklas |
167 |
if (needToParseNumeric) parseNumeric(); |
3264 |
23 Apr 07 |
enell |
168 |
if (parseException != null) throw parseException; |
7668 |
21 Mar 19 |
nicklas |
169 |
return asDouble == null ? null : asDouble.floatValue(); |
2225 |
09 May 06 |
nicklas |
170 |
} |
7667 |
21 Mar 19 |
nicklas |
171 |
@Override |
7668 |
21 Mar 19 |
nicklas |
172 |
public Double getDouble(Data data) |
7668 |
21 Mar 19 |
nicklas |
173 |
{ |
7668 |
21 Mar 19 |
nicklas |
174 |
if (needToParseNumeric) parseNumeric(); |
7668 |
21 Mar 19 |
nicklas |
175 |
if (parseException != null) throw parseException; |
7668 |
21 Mar 19 |
nicklas |
176 |
return asDouble; |
7668 |
21 Mar 19 |
nicklas |
177 |
} |
7668 |
21 Mar 19 |
nicklas |
178 |
@Override |
7667 |
21 Mar 19 |
nicklas |
179 |
public Date getDate(Data data) |
7667 |
21 Mar 19 |
nicklas |
180 |
{ |
7667 |
21 Mar 19 |
nicklas |
181 |
if (needToParseDate) parseDate(); |
7667 |
21 Mar 19 |
nicklas |
182 |
if (parseException != null) throw parseException; |
7667 |
21 Mar 19 |
nicklas |
183 |
return asDate; |
7667 |
21 Mar 19 |
nicklas |
184 |
} |
2203 |
28 Apr 06 |
nicklas |
185 |
// ------------------------------------------- |
2203 |
28 Apr 06 |
nicklas |
186 |
|
2203 |
28 Apr 06 |
nicklas |
187 |
/* |
2203 |
28 Apr 06 |
nicklas |
From the Object class |
2203 |
28 Apr 06 |
nicklas |
189 |
------------------------------------------- |
2203 |
28 Apr 06 |
nicklas |
190 |
*/ |
6127 |
14 Sep 12 |
nicklas |
191 |
@Override |
2203 |
28 Apr 06 |
nicklas |
192 |
public String toString() |
2203 |
28 Apr 06 |
nicklas |
193 |
{ |
2203 |
28 Apr 06 |
nicklas |
194 |
return constant; |
2203 |
28 Apr 06 |
nicklas |
195 |
} |
2203 |
28 Apr 06 |
nicklas |
196 |
// ------------------------------------------- |
3264 |
23 Apr 07 |
enell |
197 |
|
3264 |
23 Apr 07 |
enell |
198 |
private void parseNumeric() |
3264 |
23 Apr 07 |
enell |
199 |
{ |
3264 |
23 Apr 07 |
enell |
200 |
assert constant != null : "constant == null"; |
3264 |
23 Apr 07 |
enell |
201 |
needToParseNumeric = false; |
3264 |
23 Apr 07 |
enell |
202 |
try |
3264 |
23 Apr 07 |
enell |
203 |
{ |
7668 |
21 Mar 19 |
nicklas |
204 |
asDouble = (Double)Type.DOUBLE.parseString(constant, parser, false); |
3264 |
23 Apr 07 |
enell |
205 |
} |
7667 |
21 Mar 19 |
nicklas |
206 |
catch (RuntimeException ex) |
3264 |
23 Apr 07 |
enell |
207 |
{ |
3472 |
11 Jun 07 |
nicklas |
208 |
if (!nullIfException) parseException = ex; |
3264 |
23 Apr 07 |
enell |
209 |
} |
7668 |
21 Mar 19 |
nicklas |
210 |
if (asDouble != null) |
3264 |
23 Apr 07 |
enell |
211 |
{ |
7668 |
21 Mar 19 |
nicklas |
212 |
asLong = asDouble.longValue(); |
3264 |
23 Apr 07 |
enell |
213 |
} |
3264 |
23 Apr 07 |
enell |
214 |
} |
7667 |
21 Mar 19 |
nicklas |
215 |
|
7667 |
21 Mar 19 |
nicklas |
216 |
private void parseDate() |
7667 |
21 Mar 19 |
nicklas |
217 |
{ |
7667 |
21 Mar 19 |
nicklas |
218 |
assert constant != null : "constant == null"; |
7667 |
21 Mar 19 |
nicklas |
219 |
needToParseDate = false; |
7667 |
21 Mar 19 |
nicklas |
220 |
try |
7667 |
21 Mar 19 |
nicklas |
221 |
{ |
7667 |
21 Mar 19 |
nicklas |
222 |
asDate = (Date)Type.DATE.parseString(constant, dateParser, false); |
7667 |
21 Mar 19 |
nicklas |
223 |
} |
7667 |
21 Mar 19 |
nicklas |
224 |
catch (RuntimeException ex) |
7667 |
21 Mar 19 |
nicklas |
225 |
{ |
7667 |
21 Mar 19 |
nicklas |
226 |
if (!nullIfException) parseException = ex; |
7667 |
21 Mar 19 |
nicklas |
227 |
} |
7667 |
21 Mar 19 |
nicklas |
228 |
} |
2203 |
28 Apr 06 |
nicklas |
229 |
} |