Skip to main content
summaryrefslogtreecommitdiffstats
blob: bdb0dfff725376a2e3a802ca29d0f320c558fe52 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************************
 * Copyright (c) 2007 Composent, Inc. and others. 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: Composent, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.discovery;

import java.util.*;

/**
 * Service properties implementation class for {@link IServiceProperties}.  Subclasses
 * may be created as appropriate.
 */
public class ServiceProperties implements IServiceProperties {

	private final Properties props;

	public ServiceProperties() {
		super();
		props = new Properties();
	}

	public ServiceProperties(Properties props) {
		super();
		this.props = (props == null) ? new Properties() : props;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.discovery.IServiceProperties#getPropertyNames()
	 */
	public Enumeration getPropertyNames() {
		return props.keys();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.discovery.IServiceProperties#getPropertyString(java.lang.String)
	 */
	public String getPropertyString(String name) {
		final Object val = props.get(name);
		if (val instanceof String) {
			return (String) val;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.discovery.IServiceProperties#getPropertyBytes(java.lang.String)
	 */
	public byte[] getPropertyBytes(String name) {
		final Object val = props.get(name);
		if (val instanceof ByteArrayWrapper) {
			ByteArrayWrapper baw = (ByteArrayWrapper) val;
			return baw.getByte();
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.discovery.IServiceProperties#getProperty(java.lang.String)
	 */
	public Object getProperty(String name) {
		return props.get(name);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceProperties#setProperty(java.lang.String, java.lang.Object)
	 */
	public Object setProperty(String name, Object value) {
		return props.put(name, value);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceProperties#setPropertyBytes(java.lang.String, byte[])
	 */
	public Object setPropertyBytes(String name, byte[] value) {
		return props.put(name, new ByteArrayWrapper(value));
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceProperties#setPropertyString(java.lang.String, java.lang.String)
	 */
	public Object setPropertyString(String name, String value) {
		return props.put(name, value);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (obj instanceof ServiceProperties) {
			ServiceProperties sp = (ServiceProperties) obj;
			return props.equals(sp.props);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return props.hashCode();
	}

	public String toString() {
		StringBuffer buf = new StringBuffer("ServiceProperties["); //$NON-NLS-1$
		buf.append(props).append("]"); //$NON-NLS-1$
		return buf.toString();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceProperties#size()
	 */
	public int size() {
		return props.size();
	}

	// proper equals/hashcode for byte[]
	private class ByteArrayWrapper {

		private final byte[] value;

		public ByteArrayWrapper(byte[] value) {
			this.value = value;
		}

		public byte[] getByte() {
			return value;
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		public boolean equals(Object obj) {
			if (obj instanceof ByteArrayWrapper) {
				ByteArrayWrapper baw = (ByteArrayWrapper) obj;
				return Arrays.equals(value, baw.value);
			}
			return false;
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		public int hashCode() {
			return value.hashCode();
		}

	}
}

Back to the top