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  package org.fourthline.cling.support.model.dlna;
16  
17  import java.util.EnumSet;
18  import java.util.Locale;
19  
20  /**
21   * @author Mario Franco
22   */
23  public class DLNAOperationsAttribute extends DLNAAttribute<EnumSet<DLNAOperations>> {
24  
25      public DLNAOperationsAttribute() {
26          setValue(EnumSet.of(DLNAOperations.NONE));
27      }
28  
29      public DLNAOperationsAttribute(DLNAOperations... op) {
30          if (op != null && op.length > 0) {
31              DLNAOperations first = op[0];
32              if (op.length > 1) {
33                  System.arraycopy(op, 1, op, 0, op.length - 1);
34                  setValue(EnumSet.of(first, op));
35              } else {
36                  setValue(EnumSet.of(first));
37              }
38          }
39      }
40  
41      public void setString(String s, String cf) throws InvalidDLNAProtocolAttributeException {
42          EnumSet<DLNAOperations> value = EnumSet.noneOf(DLNAOperations.class);
43          try {
44              int parseInt = Integer.parseInt(s, 16);
45              for (DLNAOperations op : DLNAOperations.values()) {
46                  int code = op.getCode() & parseInt;
47                  if (op != DLNAOperations.NONE && (op.getCode() == code)) {
48                      value.add(op);
49                  }
50              }
51          } catch (NumberFormatException numberFormatException) {
52          }
53  
54          if (value.isEmpty())
55              throw new InvalidDLNAProtocolAttributeException("Can't parse DLNA operations integer from: " + s);
56  
57          setValue(value);
58      }
59  
60      public String getString() {
61          int code = DLNAOperations.NONE.getCode();
62          for (DLNAOperations op : getValue()) {
63              code |= op.getCode();
64          }
65          return String.format(Locale.ROOT, "%02x", code);
66      }
67  }