1 /*
2 (C) Copyright IBM Corp. 2007, 2010
3
4 THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5 ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6 CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7
8 You can obtain a current copy of the Eclipse Public License from
9 http://www.opensource.org/licenses/eclipse-1.0.php
10
11 @author : Endre Bak, IBM, ebak@de.ibm.com
12 *
13 * Change History
14 * Flag Date Prog Description
15 *-------------------------------------------------------------------------------
16 * 1804402 2007-09-28 ebak IPv6 ready SLP
17 * 2003590 2008-06-30 blaschke-oss Change licensing from CPL to EPL
18 * 2524131 2009-01-21 raman_arora Upgrade client to JDK 1.5 (Phase 1)
19 * 2531371 2009-02-10 raman_arora Upgrade client to JDK 1.5 (Phase 2)
20 * 2795671 2009-05-22 raman_arora Add Type to Comparable <T>
21 * 3023135 2010-07-01 blaschke-oss DADescriptor equals/compareTo issue
22 */
23
24 package org.metricshub.wbem.sblim.slp.internal.msg;
25
26 /*-
27 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
28 * WBEM Java Client
29 * ჻჻჻჻჻჻
30 * Copyright 2023 - 2025 MetricsHub
31 * ჻჻჻჻჻჻
32 * Licensed under the Apache License, Version 2.0 (the "License");
33 * you may not use this file except in compliance with the License.
34 * You may obtain a copy of the License at
35 *
36 * http://www.apache.org/licenses/LICENSE-2.0
37 *
38 * Unless required by applicable law or agreed to in writing, software
39 * distributed under the License is distributed on an "AS IS" BASIS,
40 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41 * See the License for the specific language governing permissions and
42 * limitations under the License.
43 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
44 */
45
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.TreeSet;
49 import org.metricshub.wbem.sblim.slp.ServiceLocationAttribute;
50
51 /**
52 * <pre>
53 * This class contains the DA related information from a DAAdvert message.
54 * URL
55 * Scope list
56 * Attribute list
57 * </pre>
58 */
59 public class DADescriptor implements Comparable<DADescriptor> {
60 private String iURL;
61
62 private TreeSet<String> iScopeSet;
63
64 private List<ServiceLocationAttribute> iAttributes;
65
66 /**
67 * Ctor.
68 *
69 * @param pURL
70 * @param pScopeSet
71 * - set of scope Strings
72 * @param pAttributes
73 * - set of ServiceLocationAttributes
74 */
75 public DADescriptor(String pURL, TreeSet<String> pScopeSet, List<ServiceLocationAttribute> pAttributes) {
76 this.iURL = pURL;
77 this.iScopeSet = pScopeSet;
78 this.iAttributes = pAttributes;
79 }
80
81 /**
82 * getURL
83 *
84 * @return String
85 */
86 public String getURL() {
87 return this.iURL;
88 }
89
90 /**
91 * hasScope
92 *
93 * @param pScope
94 * @return boolean
95 */
96 public boolean hasScope(String pScope) {
97 if (this.iScopeSet == null) return false;
98 return this.iScopeSet.contains(pScope);
99 }
100
101 public int compareTo(DADescriptor o) {
102 DADescriptor that = o;
103 return this.iURL.compareTo(that.iURL);
104 }
105
106 @Override
107 public boolean equals(Object pObj) {
108 if (!(pObj instanceof DADescriptor)) return false;
109 DADescriptor that = (DADescriptor) pObj;
110 return this.iURL.equals(that.iURL);
111 }
112
113 private int iHashCode = 0;
114
115 private void incHashCode(int pHashCode) {
116 this.iHashCode *= 31;
117 this.iHashCode += pHashCode;
118 }
119
120 /*
121 * hashCode has to be independent of the order of scopes and attributes
122 * (non-Javadoc)
123 *
124 * @see java.lang.Object#hashCode()
125 */
126 @Override
127 public int hashCode() {
128 if (this.iHashCode == 0) {
129 this.iHashCode = this.iURL.hashCode();
130 Iterator<?> itr;
131 if (this.iScopeSet != null) {
132 itr = this.iScopeSet.iterator();
133 while (itr.hasNext()) incHashCode(itr.next().hashCode());
134 }
135 if (this.iAttributes != null) {
136 itr = this.iAttributes.iterator();
137 /*
138 * iHasCode is simply incremented, because attribute order
139 * mustn't be considered.
140 */
141 while (itr.hasNext()) this.iHashCode += itr.next().hashCode();
142 }
143 }
144 return this.iHashCode;
145 }
146
147 @Override
148 public String toString() {
149 StringBuffer strBuf = new StringBuffer("URL : " + this.iURL + "\nScopes : ");
150 if (this.iScopeSet != null) {
151 Iterator<String> itr = this.iScopeSet.iterator();
152 boolean more = false;
153 while (itr.hasNext()) {
154 if (more) strBuf.append(", "); else more = true;
155 strBuf.append(itr.next());
156 }
157 }
158
159 return strBuf.toString();
160 }
161 }