5434 |
30 Sep 10 |
nicklas |
1 |
/** |
5434 |
30 Sep 10 |
nicklas |
$Id$ |
5434 |
30 Sep 10 |
nicklas |
3 |
|
5434 |
30 Sep 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5434 |
30 Sep 10 |
nicklas |
5 |
|
5434 |
30 Sep 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5434 |
30 Sep 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5434 |
30 Sep 10 |
nicklas |
8 |
|
5434 |
30 Sep 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5434 |
30 Sep 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5434 |
30 Sep 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5434 |
30 Sep 10 |
nicklas |
of the License, or (at your option) any later version. |
5434 |
30 Sep 10 |
nicklas |
13 |
|
5434 |
30 Sep 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5434 |
30 Sep 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5434 |
30 Sep 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5434 |
30 Sep 10 |
nicklas |
GNU General Public License for more details. |
5434 |
30 Sep 10 |
nicklas |
18 |
|
5434 |
30 Sep 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5434 |
30 Sep 10 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5434 |
30 Sep 10 |
nicklas |
21 |
*/ |
5434 |
30 Sep 10 |
nicklas |
22 |
package net.sf.basedb.util.encode; |
5434 |
30 Sep 10 |
nicklas |
23 |
|
5434 |
30 Sep 10 |
nicklas |
24 |
import java.util.ArrayList; |
5434 |
30 Sep 10 |
nicklas |
25 |
import java.util.Collection; |
5434 |
30 Sep 10 |
nicklas |
26 |
import java.util.List; |
5434 |
30 Sep 10 |
nicklas |
27 |
|
5434 |
30 Sep 10 |
nicklas |
28 |
/** |
5434 |
30 Sep 10 |
nicklas |
Utility functions for encoding/decoding strings. |
5434 |
30 Sep 10 |
nicklas |
30 |
|
5434 |
30 Sep 10 |
nicklas |
@author Nicklas |
5434 |
30 Sep 10 |
nicklas |
@since 2.16 |
5434 |
30 Sep 10 |
nicklas |
@base.modified $Date$ |
5434 |
30 Sep 10 |
nicklas |
34 |
*/ |
5434 |
30 Sep 10 |
nicklas |
35 |
public class EncodeUtil |
5434 |
30 Sep 10 |
nicklas |
36 |
{ |
5434 |
30 Sep 10 |
nicklas |
37 |
|
5434 |
30 Sep 10 |
nicklas |
38 |
/** |
5434 |
30 Sep 10 |
nicklas |
Encode all strings in a collection. |
5434 |
30 Sep 10 |
nicklas |
@param values A collection with values that should be encoded |
5434 |
30 Sep 10 |
nicklas |
@param encoder The encoder implemenation to use |
5434 |
30 Sep 10 |
nicklas |
@return A list with the encoded values |
5434 |
30 Sep 10 |
nicklas |
43 |
*/ |
5434 |
30 Sep 10 |
nicklas |
44 |
public static List<String> encodeAll(Collection<String> values, EncoderDecoder encoder) |
5434 |
30 Sep 10 |
nicklas |
45 |
{ |
5434 |
30 Sep 10 |
nicklas |
46 |
List<String> encoded = new ArrayList<String>(values.size()); |
5434 |
30 Sep 10 |
nicklas |
47 |
for (String s : values) |
5434 |
30 Sep 10 |
nicklas |
48 |
{ |
5434 |
30 Sep 10 |
nicklas |
49 |
encoded.add(encoder.encode(s)); |
5434 |
30 Sep 10 |
nicklas |
50 |
} |
5434 |
30 Sep 10 |
nicklas |
51 |
return encoded; |
5434 |
30 Sep 10 |
nicklas |
52 |
} |
5434 |
30 Sep 10 |
nicklas |
53 |
|
5434 |
30 Sep 10 |
nicklas |
54 |
/** |
5434 |
30 Sep 10 |
nicklas |
Decode all strings in a collection. |
5434 |
30 Sep 10 |
nicklas |
@param values A collection with values that should be decoded |
5434 |
30 Sep 10 |
nicklas |
@param decoder The decoder implemenation to use |
5434 |
30 Sep 10 |
nicklas |
@return A list with the decoded values |
5434 |
30 Sep 10 |
nicklas |
59 |
*/ |
5434 |
30 Sep 10 |
nicklas |
60 |
public static List<String> decodeAll(Collection<String> values, EncoderDecoder decoder) |
5434 |
30 Sep 10 |
nicklas |
61 |
{ |
5434 |
30 Sep 10 |
nicklas |
62 |
List<String> decoded = new ArrayList<String>(values.size()); |
5434 |
30 Sep 10 |
nicklas |
63 |
for (String s : values) |
5434 |
30 Sep 10 |
nicklas |
64 |
{ |
5434 |
30 Sep 10 |
nicklas |
65 |
decoded.add(decoder.decode(s)); |
5434 |
30 Sep 10 |
nicklas |
66 |
} |
5434 |
30 Sep 10 |
nicklas |
67 |
return decoded; |
5434 |
30 Sep 10 |
nicklas |
68 |
} |
5434 |
30 Sep 10 |
nicklas |
69 |
|
5434 |
30 Sep 10 |
nicklas |
70 |
|
5434 |
30 Sep 10 |
nicklas |
71 |
/** |
5434 |
30 Sep 10 |
nicklas |
Encode all strings in a collection and join them into a single |
5434 |
30 Sep 10 |
nicklas |
string. |
5434 |
30 Sep 10 |
nicklas |
74 |
|
5434 |
30 Sep 10 |
nicklas |
@param values A collection with values that should be encoded |
5434 |
30 Sep 10 |
nicklas |
@param encoder The encoder implemenation to use |
5434 |
30 Sep 10 |
nicklas |
@param separator The separator string that is inserted between the decoded values |
5434 |
30 Sep 10 |
nicklas |
@param skipNull TRUE to skip null values (before and after encoding) |
5434 |
30 Sep 10 |
nicklas |
@return A string with the encoded values joined together |
5434 |
30 Sep 10 |
nicklas |
80 |
*/ |
5434 |
30 Sep 10 |
nicklas |
81 |
public static String encodeAndJoin(Collection<String> values, EncoderDecoder encoder, String separator, boolean skipNull) |
5434 |
30 Sep 10 |
nicklas |
82 |
{ |
5434 |
30 Sep 10 |
nicklas |
83 |
StringBuilder sb = new StringBuilder(); |
5434 |
30 Sep 10 |
nicklas |
84 |
boolean firstElement = true; |
5434 |
30 Sep 10 |
nicklas |
85 |
for (String s : values) |
5434 |
30 Sep 10 |
nicklas |
86 |
{ |
5434 |
30 Sep 10 |
nicklas |
87 |
if (s == null && skipNull) continue; |
5434 |
30 Sep 10 |
nicklas |
88 |
s = encoder.encode(s); |
5434 |
30 Sep 10 |
nicklas |
89 |
if (s == null && skipNull) continue; |
5434 |
30 Sep 10 |
nicklas |
90 |
if (!firstElement) sb.append(separator); |
5434 |
30 Sep 10 |
nicklas |
91 |
sb.append(s); |
5434 |
30 Sep 10 |
nicklas |
92 |
firstElement = false; |
5434 |
30 Sep 10 |
nicklas |
93 |
} |
5434 |
30 Sep 10 |
nicklas |
94 |
return sb.toString(); |
5434 |
30 Sep 10 |
nicklas |
95 |
} |
5434 |
30 Sep 10 |
nicklas |
96 |
|
5434 |
30 Sep 10 |
nicklas |
97 |
} |