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 |
|
3264 |
23 Apr 07 |
enell |
25 |
import java.text.NumberFormat; |
7667 |
21 Mar 19 |
nicklas |
26 |
import java.util.Date; |
2203 |
28 Apr 06 |
nicklas |
27 |
import java.util.List; |
2203 |
28 Apr 06 |
nicklas |
28 |
|
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 compound mapper that gets its value by invoking multiple other mappers |
2203 |
28 Apr 06 |
nicklas |
which are set in the constructor. This type of mapper is used to combine |
2203 |
28 Apr 06 |
nicklas |
constant strings with column values. |
2203 |
28 Apr 06 |
nicklas |
37 |
|
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 |
41 |
*/ |
2203 |
28 Apr 06 |
nicklas |
42 |
public class CompoundMapper |
2203 |
28 Apr 06 |
nicklas |
43 |
implements Mapper |
2203 |
28 Apr 06 |
nicklas |
44 |
{ |
2203 |
28 Apr 06 |
nicklas |
45 |
private final List<Mapper> mappers; |
3264 |
23 Apr 07 |
enell |
46 |
private final NumberFormat parser; |
7667 |
21 Mar 19 |
nicklas |
47 |
private final Formatter<Date> dateParser; |
3472 |
11 Jun 07 |
nicklas |
48 |
private final boolean nullIfException; |
2203 |
28 Apr 06 |
nicklas |
49 |
|
2203 |
28 Apr 06 |
nicklas |
50 |
/** |
2203 |
28 Apr 06 |
nicklas |
Create a new compound mapper. |
2203 |
28 Apr 06 |
nicklas |
@param mappers A list of other mappers that will be invoked in |
2203 |
28 Apr 06 |
nicklas |
the order they appear in the list |
3264 |
23 Apr 07 |
enell |
54 |
*/ |
2203 |
28 Apr 06 |
nicklas |
55 |
public CompoundMapper(List<Mapper> mappers) |
2203 |
28 Apr 06 |
nicklas |
56 |
{ |
7667 |
21 Mar 19 |
nicklas |
57 |
this(mappers, null, null, false); |
3264 |
23 Apr 07 |
enell |
58 |
} |
3264 |
23 Apr 07 |
enell |
59 |
|
3264 |
23 Apr 07 |
enell |
60 |
/** |
3264 |
23 Apr 07 |
enell |
Create a new compound mapper, using a specific number formatter as it's parser. |
3264 |
23 Apr 07 |
enell |
@param mappers A list of other mappers that will be invoked in |
3264 |
23 Apr 07 |
enell |
the order they appear in the list |
3264 |
23 Apr 07 |
enell |
@param parser The number format to use or null to use Float.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 |
68 |
*/ |
3472 |
11 Jun 07 |
nicklas |
69 |
public CompoundMapper(List<Mapper> mappers, NumberFormat parser, boolean nullIfException) |
3472 |
11 Jun 07 |
nicklas |
70 |
{ |
7667 |
21 Mar 19 |
nicklas |
71 |
this(mappers, parser, null, nullIfException); |
7667 |
21 Mar 19 |
nicklas |
72 |
} |
7667 |
21 Mar 19 |
nicklas |
73 |
|
7667 |
21 Mar 19 |
nicklas |
74 |
/** |
7667 |
21 Mar 19 |
nicklas |
Create a new compound mapper, using a specific number formatter as it's parser. |
7667 |
21 Mar 19 |
nicklas |
@param mappers A list of other mappers that will be invoked in |
7667 |
21 Mar 19 |
nicklas |
the order they appear in the list |
7667 |
21 Mar 19 |
nicklas |
@param parser The date format to use |
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 |
82 |
*/ |
7667 |
21 Mar 19 |
nicklas |
83 |
public CompoundMapper(List<Mapper> mappers, NumberFormat parser, Formatter<Date> dateParser, boolean nullIfException) |
7667 |
21 Mar 19 |
nicklas |
84 |
{ |
2203 |
28 Apr 06 |
nicklas |
85 |
this.mappers = mappers; |
3264 |
23 Apr 07 |
enell |
86 |
this.parser = parser; |
7667 |
21 Mar 19 |
nicklas |
87 |
this.dateParser = dateParser; |
3472 |
11 Jun 07 |
nicklas |
88 |
this.nullIfException = nullIfException; |
2203 |
28 Apr 06 |
nicklas |
89 |
} |
7667 |
21 Mar 19 |
nicklas |
90 |
|
2203 |
28 Apr 06 |
nicklas |
91 |
/* |
2203 |
28 Apr 06 |
nicklas |
From the Mapper interface |
2203 |
28 Apr 06 |
nicklas |
93 |
------------------------------------------- |
2203 |
28 Apr 06 |
nicklas |
94 |
*/ |
7666 |
20 Mar 19 |
nicklas |
95 |
@Override |
7666 |
20 Mar 19 |
nicklas |
96 |
@Deprecated |
7666 |
20 Mar 19 |
nicklas |
97 |
public String getValue(Data data) |
7666 |
20 Mar 19 |
nicklas |
98 |
{ |
7666 |
20 Mar 19 |
nicklas |
99 |
return getString(data); |
7666 |
20 Mar 19 |
nicklas |
100 |
} |
2203 |
28 Apr 06 |
nicklas |
101 |
/** |
2203 |
28 Apr 06 |
nicklas |
Get a value by invoking each mapper in the order they appear in |
2203 |
28 Apr 06 |
nicklas |
the list and concatenating the result. |
2203 |
28 Apr 06 |
nicklas |
@return The concatenated value |
2203 |
28 Apr 06 |
nicklas |
105 |
*/ |
6127 |
14 Sep 12 |
nicklas |
106 |
@Override |
7666 |
20 Mar 19 |
nicklas |
107 |
public String getString(Data data) |
2203 |
28 Apr 06 |
nicklas |
108 |
{ |
2203 |
28 Apr 06 |
nicklas |
109 |
StringBuilder sb = new StringBuilder(); |
2203 |
28 Apr 06 |
nicklas |
110 |
for (Mapper m : mappers) |
2203 |
28 Apr 06 |
nicklas |
111 |
{ |
7666 |
20 Mar 19 |
nicklas |
112 |
sb.append(m.getString(data)); |
2203 |
28 Apr 06 |
nicklas |
113 |
} |
2203 |
28 Apr 06 |
nicklas |
114 |
return sb.toString(); |
2203 |
28 Apr 06 |
nicklas |
115 |
} |
6127 |
14 Sep 12 |
nicklas |
116 |
@Override |
2225 |
09 May 06 |
nicklas |
117 |
public Integer getInt(Data data) |
2225 |
09 May 06 |
nicklas |
118 |
{ |
7667 |
21 Mar 19 |
nicklas |
119 |
if (mappers.size() == 1) return mappers.get(0).getInt(data); |
7667 |
21 Mar 19 |
nicklas |
120 |
String stringValue = getString(data); |
3264 |
23 Apr 07 |
enell |
121 |
Integer intValue = null; |
3264 |
23 Apr 07 |
enell |
122 |
if (stringValue != null) |
3264 |
23 Apr 07 |
enell |
123 |
{ |
7667 |
21 Mar 19 |
nicklas |
124 |
intValue = (Integer)Type.INT.parseString(stringValue, parser, nullIfException); |
3264 |
23 Apr 07 |
enell |
125 |
} |
3264 |
23 Apr 07 |
enell |
126 |
return intValue; |
2225 |
09 May 06 |
nicklas |
127 |
} |
6127 |
14 Sep 12 |
nicklas |
128 |
@Override |
7668 |
21 Mar 19 |
nicklas |
129 |
public Long getLong(Data data) |
7668 |
21 Mar 19 |
nicklas |
130 |
{ |
7668 |
21 Mar 19 |
nicklas |
131 |
if (mappers.size() == 1) return mappers.get(0).getLong(data); |
7668 |
21 Mar 19 |
nicklas |
132 |
String stringValue = getString(data); |
7668 |
21 Mar 19 |
nicklas |
133 |
Long longValue = null; |
7668 |
21 Mar 19 |
nicklas |
134 |
if (stringValue != null) |
7668 |
21 Mar 19 |
nicklas |
135 |
{ |
7668 |
21 Mar 19 |
nicklas |
136 |
longValue = (Long)Type.LONG.parseString(stringValue, parser, nullIfException); |
7668 |
21 Mar 19 |
nicklas |
137 |
} |
7668 |
21 Mar 19 |
nicklas |
138 |
return longValue; |
7668 |
21 Mar 19 |
nicklas |
139 |
} |
7668 |
21 Mar 19 |
nicklas |
140 |
@Override |
2225 |
09 May 06 |
nicklas |
141 |
public Float getFloat(Data data) |
2225 |
09 May 06 |
nicklas |
142 |
{ |
7667 |
21 Mar 19 |
nicklas |
143 |
if (mappers.size() == 1) return mappers.get(0).getFloat(data); |
7667 |
21 Mar 19 |
nicklas |
144 |
String stringValue = getString(data); |
3264 |
23 Apr 07 |
enell |
145 |
Float floatValue = null; |
3264 |
23 Apr 07 |
enell |
146 |
if (stringValue != null) |
3264 |
23 Apr 07 |
enell |
147 |
{ |
7667 |
21 Mar 19 |
nicklas |
148 |
floatValue = (Float)Type.FLOAT.parseString(stringValue, parser, nullIfException); |
3264 |
23 Apr 07 |
enell |
149 |
} |
3264 |
23 Apr 07 |
enell |
150 |
return floatValue; |
2225 |
09 May 06 |
nicklas |
151 |
} |
7667 |
21 Mar 19 |
nicklas |
152 |
@Override |
7668 |
21 Mar 19 |
nicklas |
153 |
public Double getDouble(Data data) |
7668 |
21 Mar 19 |
nicklas |
154 |
{ |
7668 |
21 Mar 19 |
nicklas |
155 |
if (mappers.size() == 1) return mappers.get(0).getDouble(data); |
7668 |
21 Mar 19 |
nicklas |
156 |
String stringValue = getString(data); |
7668 |
21 Mar 19 |
nicklas |
157 |
Double doubleValue = null; |
7668 |
21 Mar 19 |
nicklas |
158 |
if (stringValue != null) |
7668 |
21 Mar 19 |
nicklas |
159 |
{ |
7668 |
21 Mar 19 |
nicklas |
160 |
doubleValue = (Double)Type.DOUBLE.parseString(stringValue, parser, nullIfException); |
7668 |
21 Mar 19 |
nicklas |
161 |
} |
7668 |
21 Mar 19 |
nicklas |
162 |
return doubleValue; |
7668 |
21 Mar 19 |
nicklas |
163 |
} |
7668 |
21 Mar 19 |
nicklas |
164 |
@Override |
7667 |
21 Mar 19 |
nicklas |
165 |
public Date getDate(Data data) |
7667 |
21 Mar 19 |
nicklas |
166 |
{ |
7667 |
21 Mar 19 |
nicklas |
167 |
if (mappers.size() == 1) return mappers.get(0).getDate(data); |
7667 |
21 Mar 19 |
nicklas |
168 |
String stringValue = getString(data); |
7667 |
21 Mar 19 |
nicklas |
169 |
Date dateValue = null; |
7667 |
21 Mar 19 |
nicklas |
170 |
if (stringValue != null) |
7667 |
21 Mar 19 |
nicklas |
171 |
{ |
7667 |
21 Mar 19 |
nicklas |
172 |
dateValue = (Date)Type.DATE.parseString(stringValue, dateParser, nullIfException); |
7667 |
21 Mar 19 |
nicklas |
173 |
} |
7667 |
21 Mar 19 |
nicklas |
174 |
return dateValue; |
7667 |
21 Mar 19 |
nicklas |
175 |
} |
2203 |
28 Apr 06 |
nicklas |
176 |
// ------------------------------------------- |
2203 |
28 Apr 06 |
nicklas |
177 |
|
2203 |
28 Apr 06 |
nicklas |
178 |
/* |
2203 |
28 Apr 06 |
nicklas |
From the Object class |
2203 |
28 Apr 06 |
nicklas |
180 |
------------------------------------------- |
2203 |
28 Apr 06 |
nicklas |
181 |
*/ |
6127 |
14 Sep 12 |
nicklas |
182 |
@Override |
2203 |
28 Apr 06 |
nicklas |
183 |
public String toString() |
2203 |
28 Apr 06 |
nicklas |
184 |
{ |
2203 |
28 Apr 06 |
nicklas |
185 |
StringBuilder sb = new StringBuilder(); |
2203 |
28 Apr 06 |
nicklas |
186 |
for (Mapper m : mappers) |
2203 |
28 Apr 06 |
nicklas |
187 |
{ |
2203 |
28 Apr 06 |
nicklas |
188 |
sb.append(m.toString()); |
2203 |
28 Apr 06 |
nicklas |
189 |
} |
2203 |
28 Apr 06 |
nicklas |
190 |
return sb.toString(); |
2203 |
28 Apr 06 |
nicklas |
191 |
} |
2203 |
28 Apr 06 |
nicklas |
192 |
// ------------------------------------------- |
2203 |
28 Apr 06 |
nicklas |
193 |
} |
2203 |
28 Apr 06 |
nicklas |
194 |
|