Skip to main content
summaryrefslogtreecommitdiffstats
blob: 017d4d5deed1fd53daee1734dcea81e9d6688842 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*******************************************************************************
 * Copyright (c) 2009, 2017 Cloudsmith 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:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.core.helpers;

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

/**
 * Helper class that provides some useful support when dealing with collections.
 */
public class CollectionUtils {

	/**
	 * A unmodifiable {@link List} implementation on top of an object array.
	 * @param <E> The element type.
	 */
	private static class UnmodifiableArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable {
		private static final long serialVersionUID = 7435304230643855579L;
		final E[] array;

		UnmodifiableArrayList(E[] array) {
			this.array = array;
		}

		@Override
		public boolean contains(Object o) {
			return indexOf(o) != -1;
		}

		@Override
		public E get(int index) {
			return array[index];
		}

		@Override
		public int indexOf(Object o) {
			int size = array.length;
			if (o == null) {
				for (int i = 0; i < size; i++)
					if (array[i] == null)
						return i;
			} else {
				for (int i = 0; i < size; i++)
					if (o.equals(array[i]))
						return i;
			}
			return -1;
		}

		@Override
		public Iterator<E> iterator() {
			return listIterator();
		}

		/**
		 * Rapid iterator, motivated by the fact that we don't need to check
		 * for concurrent modifications.
		 */
		@Override
		public ListIterator<E> listIterator() {
			return new ListIterator<E>() {
				private int index = 0;

				@Override
				public boolean hasNext() {
					return index < array.length;
				}

				@Override
				public E next() {
					if (index >= array.length)
						throw new NoSuchElementException();
					return array[index++];
				}

				@Override
				public boolean hasPrevious() {
					return index > 0;
				}

				@Override
				public E previous() {
					if (--index < 0) {
						++index;
						throw new NoSuchElementException();
					}
					return array[index];
				}

				@Override
				public int nextIndex() {
					return index;
				}

				@Override
				public int previousIndex() {
					return index - 1;
				}

				@Override
				public void remove() {
					throw new UnsupportedOperationException();
				}

				@Override
				public void set(E e) {
					throw new UnsupportedOperationException();
				}

				@Override
				public void add(E e) {
					throw new UnsupportedOperationException();
				}
			};
		}

		@Override
		public int lastIndexOf(Object o) {
			int idx = array.length;
			if (o == null) {
				while (--idx >= 0)
					if (array[idx] == null)
						return idx;
			} else {
				while (--idx >= 0)
					if (o.equals(array[idx]))
						return idx;
			}
			return -1;
		}

		@Override
		public E set(int index, E element) {
			throw new UnsupportedOperationException();
		}

		@Override
		public int size() {
			return array.length;
		}

		@Override
		public Object[] toArray() {
			return array.clone();
		}

		@Override
		@SuppressWarnings("unchecked")
		public <T> T[] toArray(T[] a) {
			int size = array.length;
			if (a.length < size)
				a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
			System.arraycopy(this.array, 0, a, 0, size);
			while (size < a.length)
				a[size++] = null;
			return a;
		}
	}

	/**
	 * Creates a combined hash for an array of objects.
	 * @param objects The objects to hash
	 * @return The combined hashCode of the objects.
	 */
	public static int hashCode(Object objects[]) {
		if (objects == null)
			return 0;

		int result = 1;
		int idx = objects.length;
		while (--idx >= 0) {
			Object object = objects[idx];
			result = 17 * result + (object == null ? 0 : object.hashCode());
		}
		return result;
	}

	/**
	 * Returns an unmodifiable list that is backed by the <code>array</code>.
	 * @param <T> The list element type
	 * @param array The array of elements
	 * @return The unmodifiable list
	 */
	public static <T> List<T> unmodifiableList(T[] array) {
		return array == null || array.length == 0 ? Collections.<T> emptyList() : new UnmodifiableArrayList<>(array);
	}

	/**
	 * Reads a property list using the {@link Properties#load(InputStream)} method. The
	 * properties are stored in a map.
	 * @param stream The stream to read from
	 * @return The resulting map
	 * @throws IOException propagated from the load method.
	 */
	public static Map<String, String> loadProperties(InputStream stream) throws IOException {
		Properties properties = new Properties();
		properties.load(stream);
		return toMap(properties);
	}

	/**
	 * Copies all elements from <code>properties</code> into a Map. The returned map might be unmodifiable
	 * @param properties
	 * @return The map containing all elements
	 */
	public static Map<String, String> toMap(Properties properties) {
		if (properties == null || properties.isEmpty())
			return Collections.<String, String> emptyMap();

		Map<String, String> props = new HashMap<>(properties.size());
		putAll(properties, props);
		return props;
	}

	/**
	 * Copies all elements from <code>properties</code> into the given <code>result</code>.
	 * @param properties
	 * @param result
	 */
	public static void putAll(Properties properties, Map<String, String> result) {
		for (Enumeration<Object> keys = properties.keys(); keys.hasMoreElements();) {
			String key = (String) keys.nextElement();
			result.put(key, properties.getProperty(key));
		}
	}

	/**
	 * Stores the properties using {@link Properties#store(OutputStream, String)} on the given <code>stream</code>.
	 * @param properties The properties to store
	 * @param stream The stream to store to
	 * @param comment The comment to use
	 * @throws IOException
	 */
	public static void storeProperties(Map<String, String> properties, OutputStream stream, String comment) throws IOException {
		Properties props = new Properties();
		props.putAll(properties);
		props.store(stream, comment);
	}
}

Back to the top