Skip to main content
summaryrefslogtreecommitdiffstats
blob: 84e83b2fddb83111db3277acf687c9c15f6a25e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*******************************************************************************
 * Copyright (c) 2007 Versant Corp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Markus Kuppe (mkuppe <at> versant <dot> com) - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.tests.discovery;

import java.net.URI;
import java.util.Comparator;
import java.util.Enumeration;

import org.eclipse.ecf.discovery.IServiceInfo;
import org.eclipse.ecf.discovery.IServiceProperties;

/**
 * Used for testing equality 
 */
public class ServiceInfoComparator implements Comparator {

	protected boolean compareServiceProperties(IServiceProperties p1, IServiceProperties p2) {
		if (p1.size() != p2.size())
			return false;
		for (final Enumeration e = p1.getPropertyNames(); e.hasMoreElements();) {
			final String key = (String) e.nextElement();
			final Object o1 = p1.getProperty(key);
			final Object o2 = p2.getProperty(key);
			if ((o1 instanceof byte[]) && (o2 instanceof byte[])) {
				final byte[] b1 = (byte[]) o1;
				final byte[] b2 = (byte[]) o2;
				for (int i = 0; i < b1.length; i++)
					if (b1[i] != b2[i])
						return false;
			} else if (!o1.equals(o2))
				return false;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object arg0, Object arg1) {
		if (arg0 instanceof IServiceInfo && arg1 instanceof IServiceInfo) {
			final IServiceInfo first = (IServiceInfo) arg0;
			final IServiceInfo second = (IServiceInfo) arg1;
			final URI uri1 = first.getLocation();
			final URI uri2 = second.getLocation();
			final boolean result = (first.getServiceID().equals(second.getServiceID()) && uri1.getHost().equals(uri2.getHost()) && uri1.getPort() == uri2.getPort() && first.getPriority() == second.getPriority() && first.getWeight() == second.getWeight() && compareServiceProperties(first.getServiceProperties(), second.getServiceProperties()));
			if (result == true) {
				return 0;
			}
		}
		return -1;
	}

}

Back to the top