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;
17  
18  import org.fourthline.cling.model.types.ServiceId;
19  import org.fourthline.cling.model.types.UDN;
20  
21  /**
22   * Combines a {@link org.fourthline.cling.model.types.UDN} and a {@link org.fourthline.cling.model.types.ServiceId}.
23   * <p>
24   * A service reference is useful to remember a service. For example, if a control point has accessed
25   * a service once, it can remember the service with {@link org.fourthline.cling.model.meta.Service#getReference()}.
26   * Before every action invocation, it can now resolve the reference to an actually registered service with
27   * {@link org.fourthline.cling.registry.Registry#getService(ServiceReference)}. If the registry doesn't return
28   * a service for the given reference, the service is currently not available.
29   * </p>
30   * <p>
31   * This simplifies implementing disconnect/reconnect behavior in a control point.
32   * </p>
33   * 
34   * @author Christian Bauer
35   */
36  public class ServiceReference {
37  
38      public static final String DELIMITER = "/";
39  
40      final private UDN udn;
41      final private ServiceId serviceId;
42  
43      public ServiceReference(String s) {
44          String[] split = s.split("/");
45          if (split.length == 2) {
46              this.udn =  UDN.valueOf(split[0]);
47              this.serviceId = ServiceId.valueOf(split[1]);
48          } else {
49              this.udn = null;
50              this.serviceId = null;
51          }
52      }
53  
54      public ServiceReference(UDN udn, ServiceId serviceId) {
55          this.udn = udn;
56          this.serviceId = serviceId;
57      }
58  
59      public UDN getUdn() {
60          return udn;
61      }
62  
63      public ServiceId getServiceId() {
64          return serviceId;
65      }
66  
67      @Override
68      public boolean equals(Object o) {
69          if (this == o) return true;
70          if (o == null || getClass() != o.getClass()) return false;
71  
72          ServiceReference that = (ServiceReference) o;
73  
74          if (!serviceId.equals(that.serviceId)) return false;
75          if (!udn.equals(that.udn)) return false;
76  
77          return true;
78      }
79  
80      @Override
81      public int hashCode() {
82          int result = udn.hashCode();
83          result = 31 * result + serviceId.hashCode();
84          return result;
85      }
86  
87      @Override
88      public String toString() {
89          return udn == null || serviceId == null ? "" : udn.toString() + DELIMITER + serviceId.toString();
90      }
91  
92  }