1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 Ericsson
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
10 * Francois Godin <copelnug@gmail.com> - Initial design and implementation
11 * Mathieu Denis <mathieu.denis@polymtl.ca> - Correction and refactoring
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.util
;
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
;
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 :
26 * <li>{@link #hashCode()}</li>
27 * <li>{@link #equals(Object)}</li>
29 * Those functions allow to use the FixedArray as the key of a
30 * {@link java.util.HashMap}.
33 * @author Francois Godin
36 * Type of the array content.
38 public final class TmfFixedArray
<T
> extends AbstractList
<T
> implements RandomAccess
, Cloneable
{
40 * Replace {@link java.util.Arrays#copyOf(Object[], int)} that do not exist
44 * Content of the array.
46 * Original array to copy from.
48 * Length of the copy to be returned.
49 * @return A new array consisting of the elements specified.
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
));
59 * Replace {@link java.util.Arrays#copyOf(Object[], int, Class)} that do not
63 * Content of the array.
65 * Original array to copy from.
67 * Length of the copy to be returned.
69 * Type of the array to be returned.
70 * @return A new array consisting of the elements specified.
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
));
79 * Replace {@link java.util.Arrays#copyOfRange(Object[], int, int)} that do
80 * not exist in java 5.
83 * Content of the array.
85 * Original array to copy from.
87 * Starting position of the range, inclusive.
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
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
);
102 private final T
[] fArray
;
108 * Array to use. WILL NOT BE COPIED.
110 public TmfFixedArray(final T
... array
) {
115 * Append a FixedArray to this FixedArray.
118 * The FixedArray to append.
119 * @return A new FixedArray with the elements of the two FixedArray.
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
);
128 * Append in order many FixedArray to this FixedArray.
131 * The FixedArrays to append.
132 * @return A new FixedArray with the element of all the FixedArray.
134 public TmfFixedArray
<T
> append(final TmfFixedArray
<T
>... values
) {
136 for (TmfFixedArray
<T
> value
: values
) {
137 newLength
+= value
.size();
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
;
149 * Append an element to the array.
153 * @return A new FixedArray with the element appended.
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
);
162 * Append an array of element to the array.
165 * Elements array to append.
166 * @return A new FixedArray with the elements appended.
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
]);
179 * @see java.lang.Object#clone()
182 public Object
clone() {
183 return new TmfFixedArray
<T
>(copyOf(fArray
, fArray
.length
));
189 * @see java.util.AbstractList#equals(java.lang.Object)
192 public boolean equals(Object o
) {
193 if (o
instanceof TmfFixedArray
<?
>) {
194 return Arrays
.equals(fArray
, ((TmfFixedArray
<?
>) o
).fArray
);
196 if (!(o
instanceof List
)) {
199 for (int i
= 0; i
< fArray
.length
; ++i
) {
200 if (!fArray
[i
].equals(o
)) {
210 * @see java.util.AbstractList#get(int)
213 public T
get(int index
) {
214 return fArray
[index
];
218 * Get the array reference.
220 * @return The array reference.
221 * @see #toArray FixedArray.toArray() to get a copy of the array.
223 public T
[] getArray() {
230 * @see java.util.AbstractList#hashCode()
233 public int hashCode() {
234 return Arrays
.hashCode(fArray
);
240 * @see java.util.AbstractList#set(int, java.lang.Object)
243 public T
set(int index
, T element
) {
244 T temp
= fArray
[index
];
245 fArray
[index
] = element
;
252 * @see java.util.AbstractCollection#size()
256 return fArray
.length
;
260 * Get a array covering only a part of the array.
263 * Starting position of the new array.
264 * @return A new array covering the elements specified.
266 public TmfFixedArray
<T
> subArray(int start
) {
267 return new TmfFixedArray
<T
>(copyOfRange(fArray
, start
, fArray
.length
));
271 * Get a array covering only a part of the array.
274 * Starting position of the new array.
276 * Number of element to include in the new array.
277 * @return A new array covering the elements specified.
279 public TmfFixedArray
<T
> subArray(int start
, int length
) {
280 return new TmfFixedArray
<T
>(copyOfRange(fArray
, start
, length
+ start
));
286 * @see java.util.AbstractCollection#toArray()
289 public T
[] toArray() {
290 return copyOf(fArray
, fArray
.length
);
296 * @see java.util.AbstractCollection#toArray(T[])
299 public <E
> E
[] toArray(E
[] array
) {
300 if (array
.length
< fArray
.length
) {
301 return copyOf(fArray
, fArray
.length
, (Class
<?
extends E
[]>) array
.getClass());
303 System
.arraycopy(fArray
, 0, array
, 0, fArray
.length
);
304 if (array
.length
> fArray
.length
) {
305 array
[fArray
.length
] = null;
313 * @see java.util.AbstractCollection#toString()
316 public String
toString() {
317 return Arrays
.toString(fArray
);
This page took 0.036577 seconds and 5 git commands to generate.