View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * Transforms a state variable value from/to strings of comma-separated elements.
28   * <p>
29   * A concrete implementation of this interface knows how to transform values of the
30   * declared type into a string of comma-separated list of elements, and how to read
31   * such strings back into individual values.
32   * </p>
33   * <p>
34   * Your action method returns a <code>CSV<...></code> instance as an output argument. It can
35   * also accept a concrecte subclass of this type as an input argument, e.g. <code>CSVString</code>.
36   * This type extends a regular <code>List</code>, so within your action method you can
37   * handle the elements as usual.
38   * </p>
39   *
40   * @author Christian Bauer
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  }