2751 |
20 Oct 06 |
nicklas |
1 |
/** |
2751 |
20 Oct 06 |
nicklas |
$Id$ |
2751 |
20 Oct 06 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2006 Nicklas Nordborg |
2751 |
20 Oct 06 |
nicklas |
5 |
|
2751 |
20 Oct 06 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
2751 |
20 Oct 06 |
nicklas |
Available at http://base.thep.lu.se/ |
2751 |
20 Oct 06 |
nicklas |
8 |
|
2751 |
20 Oct 06 |
nicklas |
BASE is free software; you can redistribute it and/or |
2751 |
20 Oct 06 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
2751 |
20 Oct 06 |
nicklas |
of the License, or (at your option) any later version. |
2751 |
20 Oct 06 |
nicklas |
13 |
|
2751 |
20 Oct 06 |
nicklas |
BASE is distributed in the hope that it will be useful, |
2751 |
20 Oct 06 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
2751 |
20 Oct 06 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2751 |
20 Oct 06 |
nicklas |
GNU General Public License for more details. |
2751 |
20 Oct 06 |
nicklas |
18 |
|
2751 |
20 Oct 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/>. |
2751 |
20 Oct 06 |
nicklas |
21 |
*/ |
2751 |
20 Oct 06 |
nicklas |
22 |
package net.sf.basedb.util.parser; |
2751 |
20 Oct 06 |
nicklas |
23 |
|
7667 |
21 Mar 19 |
nicklas |
24 |
import java.util.Date; |
7667 |
21 Mar 19 |
nicklas |
25 |
|
2751 |
20 Oct 06 |
nicklas |
26 |
import net.sf.basedb.util.parser.FlatFileParser.Data; |
2751 |
20 Oct 06 |
nicklas |
27 |
|
2751 |
20 Oct 06 |
nicklas |
28 |
/** |
2751 |
20 Oct 06 |
nicklas |
A mapper that crops a string if it is longer than a specified |
2751 |
20 Oct 06 |
nicklas |
length. The actual mapping is delegated to a parent mapper. |
2751 |
20 Oct 06 |
nicklas |
31 |
|
2751 |
20 Oct 06 |
nicklas |
@author nicklas |
2751 |
20 Oct 06 |
nicklas |
@version 2.0 |
2751 |
20 Oct 06 |
nicklas |
@base.modified $Date$ |
2751 |
20 Oct 06 |
nicklas |
35 |
*/ |
2751 |
20 Oct 06 |
nicklas |
36 |
public class CropStringMapper |
2751 |
20 Oct 06 |
nicklas |
37 |
implements Mapper |
2751 |
20 Oct 06 |
nicklas |
38 |
{ |
2751 |
20 Oct 06 |
nicklas |
39 |
|
2751 |
20 Oct 06 |
nicklas |
40 |
private final Mapper parent; |
2751 |
20 Oct 06 |
nicklas |
41 |
private final int maxLength; |
2751 |
20 Oct 06 |
nicklas |
42 |
|
2751 |
20 Oct 06 |
nicklas |
43 |
/** |
2751 |
20 Oct 06 |
nicklas |
Create a new CropStringMapper. |
2751 |
20 Oct 06 |
nicklas |
@param parent The parent mapper that does the actual mapping |
2751 |
20 Oct 06 |
nicklas |
@param maxLength The maximum length of stings |
2751 |
20 Oct 06 |
nicklas |
47 |
*/ |
2751 |
20 Oct 06 |
nicklas |
48 |
public CropStringMapper(Mapper parent, int maxLength) |
2751 |
20 Oct 06 |
nicklas |
49 |
{ |
2751 |
20 Oct 06 |
nicklas |
50 |
this.parent = parent; |
2751 |
20 Oct 06 |
nicklas |
51 |
this.maxLength = maxLength; |
2751 |
20 Oct 06 |
nicklas |
52 |
} |
2751 |
20 Oct 06 |
nicklas |
53 |
|
2751 |
20 Oct 06 |
nicklas |
54 |
/* |
2751 |
20 Oct 06 |
nicklas |
From the Mapper interface |
2751 |
20 Oct 06 |
nicklas |
56 |
------------------------------------------- |
2751 |
20 Oct 06 |
nicklas |
57 |
*/ |
7666 |
20 Mar 19 |
nicklas |
58 |
@Override |
7666 |
20 Mar 19 |
nicklas |
59 |
@Deprecated |
7666 |
20 Mar 19 |
nicklas |
60 |
public String getValue(Data data) |
7666 |
20 Mar 19 |
nicklas |
61 |
{ |
7666 |
20 Mar 19 |
nicklas |
62 |
return getString(data); |
7666 |
20 Mar 19 |
nicklas |
63 |
} |
2751 |
20 Oct 06 |
nicklas |
64 |
/** |
2751 |
20 Oct 06 |
nicklas |
Use the parent mapper to get the string value. Then check the |
2751 |
20 Oct 06 |
nicklas |
length against the maximum length and crop if neccessary. |
2751 |
20 Oct 06 |
nicklas |
67 |
*/ |
6127 |
14 Sep 12 |
nicklas |
68 |
@Override |
7666 |
20 Mar 19 |
nicklas |
69 |
public String getString(Data data) |
2751 |
20 Oct 06 |
nicklas |
70 |
{ |
7666 |
20 Mar 19 |
nicklas |
71 |
String s = parent.getString(data); |
2751 |
20 Oct 06 |
nicklas |
72 |
if (s != null && s.length() > maxLength) s = s.substring(0, maxLength); |
2751 |
20 Oct 06 |
nicklas |
73 |
return s; |
2751 |
20 Oct 06 |
nicklas |
74 |
} |
6127 |
14 Sep 12 |
nicklas |
75 |
@Override |
2751 |
20 Oct 06 |
nicklas |
76 |
public Integer getInt(Data data) |
2751 |
20 Oct 06 |
nicklas |
77 |
{ |
2751 |
20 Oct 06 |
nicklas |
78 |
return parent.getInt(data); |
2751 |
20 Oct 06 |
nicklas |
79 |
} |
6127 |
14 Sep 12 |
nicklas |
80 |
@Override |
7668 |
21 Mar 19 |
nicklas |
81 |
public Long getLong(Data data) |
7668 |
21 Mar 19 |
nicklas |
82 |
{ |
7668 |
21 Mar 19 |
nicklas |
83 |
return parent.getLong(data); |
7668 |
21 Mar 19 |
nicklas |
84 |
} |
7668 |
21 Mar 19 |
nicklas |
85 |
@Override |
2751 |
20 Oct 06 |
nicklas |
86 |
public Float getFloat(Data data) |
2751 |
20 Oct 06 |
nicklas |
87 |
{ |
2751 |
20 Oct 06 |
nicklas |
88 |
return parent.getFloat(data); |
2751 |
20 Oct 06 |
nicklas |
89 |
} |
7667 |
21 Mar 19 |
nicklas |
90 |
@Override |
7668 |
21 Mar 19 |
nicklas |
91 |
public Double getDouble(Data data) |
7668 |
21 Mar 19 |
nicklas |
92 |
{ |
7668 |
21 Mar 19 |
nicklas |
93 |
return parent.getDouble(data); |
7668 |
21 Mar 19 |
nicklas |
94 |
} |
7668 |
21 Mar 19 |
nicklas |
95 |
@Override |
7667 |
21 Mar 19 |
nicklas |
96 |
public Date getDate(Data data) |
7667 |
21 Mar 19 |
nicklas |
97 |
{ |
7667 |
21 Mar 19 |
nicklas |
98 |
return parent.getDate(data); |
7667 |
21 Mar 19 |
nicklas |
99 |
} |
2751 |
20 Oct 06 |
nicklas |
100 |
// ------------------------------------------- |
2751 |
20 Oct 06 |
nicklas |
101 |
|
2751 |
20 Oct 06 |
nicklas |
102 |
} |