View Javadoc
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.sentrysoftware.wbem.sblim.slp.internal.msg;
25  
26  /*-
27   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
28   * WBEM Java Client
29   * ჻჻჻჻჻჻
30   * Copyright (C) 2023 Sentry Software
31   * ჻჻჻჻჻჻
32   * This program is free software: you can redistribute it and/or modify
33   * it under the terms of the GNU Lesser General Public License as
34   * published by the Free Software Foundation, either version 3 of the
35   * License, or (at your option) any later version.
36   *
37   * This program is distributed in the hope that it will be useful,
38   * but WITHOUT ANY WARRANTY; without even the implied warranty of
39   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40   * GNU General Lesser Public License for more details.
41   *
42   * You should have received a copy of the GNU General Lesser Public
43   * License along with this program.  If not, see
44   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
45   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
46   */
47  
48  import java.util.Iterator;
49  import java.util.List;
50  import java.util.TreeSet;
51  
52  import org.sentrysoftware.wbem.sblim.slp.ServiceLocationAttribute;
53  
54  /**
55   * <pre>
56   * This class contains the DA related information from a DAAdvert message.
57   * URL
58   * Scope list
59   * Attribute list
60   * </pre>
61   */
62  public class DADescriptor implements Comparable<DADescriptor> {
63  
64  	private String iURL;
65  
66  	private TreeSet<String> iScopeSet;
67  
68  	private List<ServiceLocationAttribute> iAttributes;
69  
70  	/**
71  	 * Ctor.
72  	 * 
73  	 * @param pURL
74  	 * @param pScopeSet
75  	 *            - set of scope Strings
76  	 * @param pAttributes
77  	 *            - set of ServiceLocationAttributes
78  	 */
79  	public DADescriptor(String pURL, TreeSet<String> pScopeSet,
80  			List<ServiceLocationAttribute> pAttributes) {
81  		this.iURL = pURL;
82  		this.iScopeSet = pScopeSet;
83  		this.iAttributes = pAttributes;
84  	}
85  
86  	/**
87  	 * getURL
88  	 * 
89  	 * @return String
90  	 */
91  	public String getURL() {
92  		return this.iURL;
93  	}
94  
95  	/**
96  	 * hasScope
97  	 * 
98  	 * @param pScope
99  	 * @return boolean
100 	 */
101 	public boolean hasScope(String pScope) {
102 		if (this.iScopeSet == null) return false;
103 		return this.iScopeSet.contains(pScope);
104 	}
105 
106 	public int compareTo(DADescriptor o) {
107 		DADescriptor that = o;
108 		return this.iURL.compareTo(that.iURL);
109 	}
110 
111 	@Override
112 	public boolean equals(Object pObj) {
113 		if (!(pObj instanceof DADescriptor)) return false;
114 		DADescriptor that = (DADescriptor) pObj;
115 		return this.iURL.equals(that.iURL);
116 	}
117 
118 	private int iHashCode = 0;
119 
120 	private void incHashCode(int pHashCode) {
121 		this.iHashCode *= 31;
122 		this.iHashCode += pHashCode;
123 	}
124 
125 	/*
126 	 * hashCode has to be independent of the order of scopes and attributes
127 	 * (non-Javadoc)
128 	 * 
129 	 * @see java.lang.Object#hashCode()
130 	 */
131 	@Override
132 	public int hashCode() {
133 		if (this.iHashCode == 0) {
134 			this.iHashCode = this.iURL.hashCode();
135 			Iterator<?> itr;
136 			if (this.iScopeSet != null) {
137 				itr = this.iScopeSet.iterator();
138 				while (itr.hasNext())
139 					incHashCode(itr.next().hashCode());
140 			}
141 			if (this.iAttributes != null) {
142 				itr = this.iAttributes.iterator();
143 				/*
144 				 * iHasCode is simply incremented, because attribute order
145 				 * mustn't be considered.
146 				 */
147 				while (itr.hasNext())
148 					this.iHashCode += itr.next().hashCode();
149 			}
150 		}
151 		return this.iHashCode;
152 	}
153 
154 	@Override
155 	public String toString() {
156 		StringBuffer strBuf = new StringBuffer("URL : " + this.iURL + "\nScopes : ");
157 		if (this.iScopeSet != null) {
158 			Iterator<String> itr = this.iScopeSet.iterator();
159 			boolean more = false;
160 			while (itr.hasNext()) {
161 				if (more) strBuf.append(", ");
162 				else more = true;
163 				strBuf.append(itr.next());
164 			}
165 		}
166 
167 		return strBuf.toString();
168 	}
169 
170 }