7766 |
06 Feb 20 |
nicklas |
1 |
package net.sf.basedb.util.formatter; |
7766 |
06 Feb 20 |
nicklas |
2 |
|
7766 |
06 Feb 20 |
nicklas |
3 |
import java.util.Collection; |
7766 |
06 Feb 20 |
nicklas |
4 |
|
7766 |
06 Feb 20 |
nicklas |
5 |
import net.sf.basedb.util.Values; |
7766 |
06 Feb 20 |
nicklas |
6 |
|
7766 |
06 Feb 20 |
nicklas |
7 |
/** |
7766 |
06 Feb 20 |
nicklas |
Formatter implementation that formats a collection of values to a |
7766 |
06 Feb 20 |
nicklas |
string by concatenating the values with a given separator. |
7766 |
06 Feb 20 |
nicklas |
Reverse parsing is not supported. |
7766 |
06 Feb 20 |
nicklas |
11 |
|
7766 |
06 Feb 20 |
nicklas |
@author nicklas |
7766 |
06 Feb 20 |
nicklas |
@since 3.16 |
7766 |
06 Feb 20 |
nicklas |
14 |
*/ |
7766 |
06 Feb 20 |
nicklas |
15 |
public class CollectionFormatter<I> |
7766 |
06 Feb 20 |
nicklas |
16 |
implements Formatter<Collection<I>> |
7766 |
06 Feb 20 |
nicklas |
17 |
{ |
7766 |
06 Feb 20 |
nicklas |
18 |
|
7766 |
06 Feb 20 |
nicklas |
19 |
private final Formatter<? super I> parent; |
7766 |
06 Feb 20 |
nicklas |
20 |
private final String separator; |
7766 |
06 Feb 20 |
nicklas |
21 |
private final boolean skipNull; |
7766 |
06 Feb 20 |
nicklas |
22 |
|
7766 |
06 Feb 20 |
nicklas |
23 |
/** |
7766 |
06 Feb 20 |
nicklas |
Create a collection formatting using ", " as separator and skipping null values. |
7766 |
06 Feb 20 |
nicklas |
25 |
*/ |
7766 |
06 Feb 20 |
nicklas |
26 |
public CollectionFormatter(Formatter<? super I> parent) |
7766 |
06 Feb 20 |
nicklas |
27 |
{ |
7766 |
06 Feb 20 |
nicklas |
28 |
this(", ", true, parent); |
7766 |
06 Feb 20 |
nicklas |
29 |
} |
7766 |
06 Feb 20 |
nicklas |
30 |
|
7766 |
06 Feb 20 |
nicklas |
31 |
public CollectionFormatter(String separator, boolean skipNull, Formatter<? super I> parent) |
7766 |
06 Feb 20 |
nicklas |
32 |
{ |
7766 |
06 Feb 20 |
nicklas |
33 |
this.parent = parent; |
7766 |
06 Feb 20 |
nicklas |
34 |
this.separator = separator; |
7766 |
06 Feb 20 |
nicklas |
35 |
this.skipNull = skipNull; |
7766 |
06 Feb 20 |
nicklas |
36 |
} |
7766 |
06 Feb 20 |
nicklas |
37 |
|
7766 |
06 Feb 20 |
nicklas |
38 |
@Override |
7766 |
06 Feb 20 |
nicklas |
39 |
public String format(Collection<I> values) |
7766 |
06 Feb 20 |
nicklas |
40 |
{ |
7766 |
06 Feb 20 |
nicklas |
41 |
return Values.getString(values, separator, skipNull, parent); |
7766 |
06 Feb 20 |
nicklas |
42 |
} |
7766 |
06 Feb 20 |
nicklas |
43 |
|
7766 |
06 Feb 20 |
nicklas |
44 |
@Override |
7766 |
06 Feb 20 |
nicklas |
45 |
public Collection<I> parseString(String value) |
7766 |
06 Feb 20 |
nicklas |
46 |
{ |
7766 |
06 Feb 20 |
nicklas |
47 |
throw new UnsupportedOperationException("parseString"); |
7766 |
06 Feb 20 |
nicklas |
48 |
} |
7766 |
06 Feb 20 |
nicklas |
49 |
|
7766 |
06 Feb 20 |
nicklas |
50 |
} |