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  /**
19   * Combines a {@link UDN} with a {@link DeviceType}, string representation
20   * is separated by double-colon.
21   *
22   * @author Christian Bauer
23   */
24  public class NamedDeviceType {
25  
26      private UDN udn;
27      private DeviceType deviceType;
28  
29      public NamedDeviceType(UDN udn, DeviceType deviceType) {
30          this.udn = udn;
31          this.deviceType = deviceType;
32      }
33  
34      public UDN getUdn() {
35          return udn;
36      }
37  
38      public DeviceType getDeviceType() {
39          return deviceType;
40      }
41  
42      public static NamedDeviceType valueOf(String s) throws InvalidValueException {
43          String[] strings = s.split("::");
44          if (strings.length != 2) {
45              throw new InvalidValueException("Can't parse UDN::DeviceType from: " + s);
46          }
47  
48          UDN udn;
49          try {
50              udn = UDN.valueOf(strings[0]);
51          } catch (Exception ex) {
52              throw new InvalidValueException("Can't parse UDN: " + strings[0]);
53          }
54  
55          DeviceType deviceType = DeviceType.valueOf(strings[1]);
56          return new NamedDeviceType(udn, deviceType);
57      }
58  
59      @Override
60      public String toString() {
61          return getUdn().toString() + "::" + getDeviceType().toString();
62      }
63  
64      @Override
65      public boolean equals(Object o) {
66          if (this == o) return true;
67          if (o == null || !(o instanceof NamedDeviceType)) return false;
68  
69          NamedDeviceType that = (NamedDeviceType) o;
70  
71          if (!deviceType.equals(that.deviceType)) return false;
72          if (!udn.equals(that.udn)) return false;
73  
74          return true;
75      }
76  
77      @Override
78      public int hashCode() {
79          int result = udn.hashCode();
80          result = 31 * result + deviceType.hashCode();
81          return result;
82      }
83  }