Add more warnings source to 1.6) and fix warnings
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / Exporter.java
CommitLineData
db31c358
NR
1package be.nikiroo.utils.serial;
2
3import java.io.NotSerializableException;
4import java.util.HashMap;
5import java.util.Map;
6
7import 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 */
17public 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}