1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.model.types.csv;
17
18 import org.fourthline.cling.model.types.Datatype;
19 import org.fourthline.cling.model.types.InvalidValueException;
20 import org.fourthline.cling.model.ModelUtil;
21 import org.seamless.util.Reflections;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public abstract class CSV<T> extends ArrayList<T> {
43
44 protected final Datatype.Builtin datatype;
45
46 public CSV() {
47 datatype = getBuiltinDatatype();
48 }
49
50 public CSV(String s) throws InvalidValueException {
51 datatype = getBuiltinDatatype();
52 addAll(parseString(s));
53 }
54
55 protected List parseString(String s) throws InvalidValueException {
56 String[] strings = ModelUtil.fromCommaSeparatedList(s);
57 List values = new ArrayList<>();
58 for (String string : strings) {
59 values.add(datatype.getDatatype().valueOf(string));
60 }
61 return values;
62 }
63
64 protected Datatype.Builtin getBuiltinDatatype() throws InvalidValueException {
65 Class csvType = Reflections.getTypeArguments(ArrayList.class, getClass()).get(0);
66 Datatype.Default defaultType = Datatype.Default.getByJavaType(csvType);
67 if (defaultType == null) {
68 throw new InvalidValueException("No built-in UPnP datatype for Java type of CSV: " + csvType);
69 }
70 return defaultType.getBuiltinType();
71 }
72
73 @Override
74 public String toString() {
75 List<String> stringValues = new ArrayList<>();
76 for (T t : this) {
77 stringValues.add(datatype.getDatatype().getString(t));
78 }
79 return ModelUtil.toCommaSeparatedList(stringValues.toArray(new Object[stringValues.size()]));
80 }
81 }