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.meta;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.fourthline.cling.model.Constants;
22  import org.fourthline.cling.model.Validatable;
23  import org.fourthline.cling.model.ValidationError;
24  import org.fourthline.cling.model.types.UDN;
25  
26  /**
27   * Unique device name, received and offered during discovery with SSDP.
28   *
29   * @author Christian Bauer
30   */
31  public class DeviceIdentity implements Validatable {
32  
33      final private UDN udn;
34      final private Integer maxAgeSeconds;
35  
36      public DeviceIdentity(UDN udn, DeviceIdentity template) {
37          this.udn = udn;
38          this.maxAgeSeconds = template.getMaxAgeSeconds();
39      }
40  
41      public DeviceIdentity(UDN udn) {
42          this.udn = udn;
43          this.maxAgeSeconds = Constants.MIN_ADVERTISEMENT_AGE_SECONDS;
44      }
45  
46      public DeviceIdentity(UDN udn, Integer maxAgeSeconds) {
47          this.udn = udn;
48          this.maxAgeSeconds = maxAgeSeconds;
49      }
50  
51      public UDN getUdn() {
52          return udn;
53      }
54  
55      public Integer getMaxAgeSeconds() {
56          return maxAgeSeconds;
57      }
58  
59      @Override
60      public boolean equals(Object o) {
61          if (this == o) return true;
62          if (o == null || getClass() != o.getClass()) return false;
63  
64          DeviceIdentity that = (DeviceIdentity) o;
65  
66          if (!udn.equals(that.udn)) return false;
67  
68          return true;
69      }
70  
71      @Override
72      public int hashCode() {
73          return udn.hashCode();
74      }
75  
76      @Override
77      public String toString() {
78          return "(" + getClass().getSimpleName() + ") UDN: " + getUdn();
79      }
80  
81      @Override
82      public List<ValidationError> validate() {
83      	List<ValidationError> errors = new ArrayList<>();
84  
85      	if (getUdn() == null) {
86      		errors.add(new ValidationError(
87      				getClass(),
88      				"major",
89      				"Device has no UDN"
90      				));
91      	}
92  
93      	return errors;
94      }
95  }