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;
17  
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  /**
22   * Representing the DLNA document and its version.
23   * <p>
24   * Someone ignored the device and service type construct of UPnP
25   * and invented a new and of course much better device type/version  * construct.
26   * </p>
27   *
28   * @author Christian Bauer
29   */
30  public class DLNADoc {
31  
32      public static final Pattern PATTERN = Pattern.compile("(.+?)[ -]([0-9].[0-9]{2})");
33  
34      public enum Version {
35          V1_0("1.00"),
36          V1_5("1.50");
37  
38          String s;
39  
40          Version(String s) {
41              this.s = s;
42          }
43  
44  
45          @Override
46          public String toString() {
47              return s;
48          }
49      }
50  
51      final private String devClass;
52      final private String version;
53  
54      public DLNADoc(String devClass, String version) {
55          this.devClass = devClass;
56          this.version = version;
57      }
58  
59      public DLNADoc(String devClass, Version version) {
60          this.devClass = devClass;
61          this.version = version.s;
62      }
63  
64      public String getDevClass() {
65          return devClass;
66      }
67  
68      public String getVersion() {
69          return version;
70      }
71  
72      public static DLNADoc valueOf(String s) throws InvalidValueException {
73          Matcher matcher = PATTERN.matcher(s);
74          if (matcher.matches()) {
75              return new DLNADoc(matcher.group(1), matcher.group(2));
76          } else {
77              throw new InvalidValueException("Can't parse DLNADoc: " + s);
78          }
79      }
80  
81      @Override
82      public boolean equals(Object o) {
83          if (this == o) return true;
84          if (o == null || getClass() != o.getClass()) return false;
85  
86          DLNADoc dlnaDoc = (DLNADoc) o;
87  
88          if (!devClass.equals(dlnaDoc.devClass)) return false;
89          if (!version.equals(dlnaDoc.version)) return false;
90  
91          return true;
92      }
93  
94      @Override
95      public int hashCode() {
96          int result = devClass.hashCode();
97          result = 31 * result + version.hashCode();
98          return result;
99      }
100 
101     @Override
102     public String toString() {
103         return getDevClass() + "-" + getVersion();
104     }
105 }