Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.serial; |
2 | ||
3 | import java.io.NotSerializableException; | |
4 | import java.util.HashMap; | |
5 | import java.util.Map; | |
6 | ||
7 | import be.nikiroo.utils.StringUtils; | |
8 | ||
9 | /** | |
10 | * A simple class to serialise objects to {@link String}. | |
11 | * <p> | |
12 | * This class does not support inner classes (it does support nested classes, | |
13 | * though). | |
14 | * | |
15 | * @author niki | |
16 | */ | |
17 | public class Exporter { | |
18 | private Map<Integer, Object> map; | |
19 | private StringBuilder builder; | |
20 | ||
21 | public Exporter() { | |
22 | map = new HashMap<Integer, Object>(); | |
23 | builder = new StringBuilder(); | |
24 | } | |
25 | ||
26 | public Exporter append(Object o) throws NotSerializableException { | |
27 | SerialUtils.append(builder, o, map); | |
28 | return this; | |
29 | } | |
30 | ||
31 | public void clear() { | |
32 | builder.setLength(0); | |
33 | map.clear(); | |
34 | } | |
35 | ||
36 | // null = auto | |
37 | public String toString(Boolean zip) { | |
38 | if (zip == null) { | |
39 | zip = builder.length() > 128; | |
40 | } | |
41 | ||
42 | if (zip) { | |
43 | return "ZIP:" + StringUtils.zip64(builder.toString()); | |
db31c358 | 44 | } |
cd0c27d2 NR |
45 | |
46 | return builder.toString(); | |
db31c358 NR |
47 | } |
48 | ||
49 | /** | |
50 | * The exported items in a serialised form. | |
51 | * | |
52 | * @return the items currently in this {@link Exporter}. | |
53 | */ | |
54 | @Override | |
55 | public String toString() { | |
56 | return toString(null); | |
57 | } | |
58 | } |