tmf: formatting of tmf.ui.statistics
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / util / TmfFixedArray.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Godin <copelnug@gmail.com> - Initial design and implementation
11 * Mathieu Denis <mathieu.denis@polymtl.ca> - Correction and refactoring
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.util;
15
16 import java.lang.reflect.Array;
17 import java.util.AbstractList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.RandomAccess;
21
22 /**
23 * Allow to create a List object that contain an already existing array. Works
24 * like {@link java.util.Arrays#asList} but contains more functions :
25 * <ul>
26 * <li>{@link #hashCode()}</li>
27 * <li>{@link #equals(Object)}</li>
28 * </ul>
29 * Those functions allow to use the FixedArray as the key of a
30 * {@link java.util.HashMap}.
31 *
32 * @version 1.0
33 * @author Francois Godin
34 *
35 * @param <T>
36 * Type of the array content.
37 */
38 public final class TmfFixedArray<T> extends AbstractList<T> implements RandomAccess, Cloneable {
39 /**
40 * Replace {@link java.util.Arrays#copyOf(Object[], int)} that do not exist
41 * in java 5.
42 *
43 * @param <E>
44 * Content of the array.
45 * @param array
46 * Original array to copy from.
47 * @param newLength
48 * Length of the copy to be returned.
49 * @return A new array consisting of the elements specified.
50 */
51 private static <E> E[] copyOf(final E[] array, int newLength) {
52 // FIXME Is it useful to use newInstance?
53 E[] result = (E[]) Array.newInstance(array.getClass().getComponentType(), newLength);
54 System.arraycopy(array, 0, result, 0, Math.min(array.length, newLength));
55 return result;
56 }
57
58 /**
59 * Replace {@link java.util.Arrays#copyOf(Object[], int, Class)} that do not
60 * exist in java 5.
61 *
62 * @param <E>
63 * Content of the array.
64 * @param array
65 * Original array to copy from.
66 * @param newLength
67 * Length of the copy to be returned.
68 * @param newType
69 * Type of the array to be returned.
70 * @return A new array consisting of the elements specified.
71 */
72 private static <E, U> E[] copyOf(final U[] array, int newLength, Class<? extends E[]> newType) {
73 E[] result = (E[])Array.newInstance(newType.getComponentType(), newLength);
74 System.arraycopy(array, 0, result, 0, Math.min(array.length, newLength));
75 return result;
76 }
77
78 /**
79 * Replace {@link java.util.Arrays#copyOfRange(Object[], int, int)} that do
80 * not exist in java 5.
81 *
82 * @param <E>
83 * Content of the array.
84 * @param array
85 * Original array to copy from.
86 * @param start
87 * Starting position of the range, inclusive.
88 * @param end
89 * Ending position of the range, exclusive.
90 * @return A new array consisting of the elements specified. The length of
91 * the new array is equal to end-start
92 */
93 private static <E> E[] copyOfRange(final E[] array, int start, int end) {
94 E[] result = (E[])Array.newInstance(array.getClass().getComponentType(), end - start);
95 System.arraycopy(array, start, result, 0, end - start);
96 return result;
97 }
98
99 /**
100 * The array.
101 */
102 private final T[] fArray;
103
104 /**
105 * Constructor.
106 *
107 * @param array
108 * Array to use. WILL NOT BE COPIED.
109 */
110 public TmfFixedArray(final T... array) {
111 fArray = array;
112 }
113
114 /**
115 * Append a FixedArray to this FixedArray.
116 *
117 * @param value
118 * The FixedArray to append.
119 * @return A new FixedArray with the elements of the two FixedArray.
120 */
121 public TmfFixedArray<T> append(final TmfFixedArray<T> value) {
122 TmfFixedArray<T> result = new TmfFixedArray<T>(copyOf(fArray, fArray.length + value.size()));
123 System.arraycopy(value.fArray, 0, result.fArray, fArray.length, value.fArray.length);
124 return result;
125 }
126
127 /**
128 * Append in order many FixedArray to this FixedArray.
129 *
130 * @param values
131 * The FixedArrays to append.
132 * @return A new FixedArray with the element of all the FixedArray.
133 */
134 public TmfFixedArray<T> append(final TmfFixedArray<T>... values) {
135 int newLength = 0;
136 for (TmfFixedArray<T> value : values) {
137 newLength += value.size();
138 }
139 TmfFixedArray<T> result = new TmfFixedArray<T>(copyOf(fArray, fArray.length + newLength));
140 newLength = fArray.length;
141 for (TmfFixedArray<T> value : values) {
142 System.arraycopy(value.fArray, 0, result.fArray, newLength, value.fArray.length);
143 newLength += value.fArray.length;
144 }
145 return result;
146 }
147
148 /**
149 * Append an element to the array.
150 *
151 * @param value
152 * Element to append.
153 * @return A new FixedArray with the element appended.
154 */
155 public TmfFixedArray<T> append(final T value) {
156 TmfFixedArray<T> result = new TmfFixedArray<T>(copyOf(fArray, fArray.length + 1));
157 result.set(fArray.length, value);
158 return result;
159 }
160
161 /**
162 * Append an array of element to the array.
163 *
164 * @param values
165 * Elements array to append.
166 * @return A new FixedArray with the elements appended.
167 */
168 public TmfFixedArray<T> append(final T... values) {
169 TmfFixedArray<T> result = new TmfFixedArray<T>(copyOf(fArray, fArray.length + values.length));
170 for (int i = 0; i < values.length; ++i) {
171 result.set(fArray.length + i, values[i]);
172 }
173 return result;
174 }
175
176 /*
177 * (non-Javadoc)
178 *
179 * @see java.lang.Object#clone()
180 */
181 @Override
182 public Object clone() {
183 return new TmfFixedArray<T>(copyOf(fArray, fArray.length));
184 }
185
186 /*
187 * (non-Javadoc)
188 *
189 * @see java.util.AbstractList#equals(java.lang.Object)
190 */
191 @Override
192 public boolean equals(Object o) {
193 if (o instanceof TmfFixedArray<?>) {
194 return Arrays.equals(fArray, ((TmfFixedArray<?>) o).fArray);
195 }
196 if (!(o instanceof List)) {
197 return false;
198 }
199 for (int i = 0; i < fArray.length; ++i) {
200 if (!fArray[i].equals(o)) {
201 return false;
202 }
203 }
204 return true;
205 }
206
207 /*
208 * (non-Javadoc)
209 *
210 * @see java.util.AbstractList#get(int)
211 */
212 @Override
213 public T get(int index) {
214 return fArray[index];
215 }
216
217 /**
218 * Get the array reference.
219 *
220 * @return The array reference.
221 * @see #toArray FixedArray.toArray() to get a copy of the array.
222 */
223 public T[] getArray() {
224 return fArray;
225 }
226
227 /*
228 * (non-Javadoc)
229 *
230 * @see java.util.AbstractList#hashCode()
231 */
232 @Override
233 public int hashCode() {
234 return Arrays.hashCode(fArray);
235 }
236
237 /*
238 * (non-Javadoc)
239 *
240 * @see java.util.AbstractList#set(int, java.lang.Object)
241 */
242 @Override
243 public T set(int index, T element) {
244 T temp = fArray[index];
245 fArray[index] = element;
246 return temp;
247 }
248
249 /*
250 * (non-Javadoc)
251 *
252 * @see java.util.AbstractCollection#size()
253 */
254 @Override
255 public int size() {
256 return fArray.length;
257 }
258
259 /**
260 * Get a array covering only a part of the array.
261 *
262 * @param start
263 * Starting position of the new array.
264 * @return A new array covering the elements specified.
265 */
266 public TmfFixedArray<T> subArray(int start) {
267 return new TmfFixedArray<T>(copyOfRange(fArray, start, fArray.length));
268 }
269
270 /**
271 * Get a array covering only a part of the array.
272 *
273 * @param start
274 * Starting position of the new array.
275 * @param length
276 * Number of element to include in the new array.
277 * @return A new array covering the elements specified.
278 */
279 public TmfFixedArray<T> subArray(int start, int length) {
280 return new TmfFixedArray<T>(copyOfRange(fArray, start, length + start));
281 }
282
283 /*
284 * (non-Javadoc)
285 *
286 * @see java.util.AbstractCollection#toArray()
287 */
288 @Override
289 public T[] toArray() {
290 return copyOf(fArray, fArray.length);
291 }
292
293 /*
294 * (non-Javadoc)
295 *
296 * @see java.util.AbstractCollection#toArray(T[])
297 */
298 @Override
299 public <E> E[] toArray(E[] array) {
300 if (array.length < fArray.length) {
301 return copyOf(fArray, fArray.length, (Class<? extends E[]>) array.getClass());
302 }
303 System.arraycopy(fArray, 0, array, 0, fArray.length);
304 if (array.length > fArray.length) {
305 array[fArray.length] = null;
306 }
307 return array;
308 }
309
310 /*
311 * (non-Javadoc)
312 *
313 * @see java.util.AbstractCollection#toString()
314 */
315 @Override
316 public String toString() {
317 return Arrays.toString(fArray);
318 }
319 }
This page took 0.036577 seconds and 5 git commands to generate.