1 /*******************************************************************************
2 * Copyright (c) 2011 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
.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 * <h4>Allow to create a List object that contain an already existing array.</h4>
24 * <p>Works like {@link java.util.Arrays#asList} but offers more functions :
26 * <li>{@link #hashCode()}</li>
27 * <li>{@link #equals(Object)}</li>
29 * <p>Those functions allow to use the FixedArray as the key of a {@link java.util.HashMap}.</p>
31 * @param <T> Type of the array content.
33 public final class TmfFixedArray
<T
> extends AbstractList
<T
> implements RandomAccess
{
35 * Replace {@link java.util.Arrays#copyOf(Object[], int)} that do not exist in java 5.
36 * @param <E> Content of the array.
37 * @param array Original array to copy from.
38 * @param newLength Length of the copy to be returned.
39 * @return A new array consisting of the elements specified.
41 @SuppressWarnings("unchecked")
42 private static <E
> E
[] copyOf(final E
[] array
, int newLength
) {
43 E
[] result
= (E
[])Array
.newInstance(array
.getClass().getComponentType(), newLength
); // Is it useful to use newInstance?
44 System
.arraycopy(array
, 0, result
, 0, Math
.min(array
.length
, newLength
));
48 * Replace {@link java.util.Arrays#copyOf(Object[], int, Class)} that do not exist in java 5.
49 * @param <E> Content of the array.
50 * @param array Original array to copy from.
51 * @param newLength Length of the copy to be returned.
52 * @param newType Type of the array to be returned.
53 * @return A new array consisting of the elements specified.
55 @SuppressWarnings("unchecked")
56 private static <E
, U
> E
[] copyOf(final U
[] array
, int newLength
, Class
<?
extends E
[]> newType
) {
57 E
[] result
= (E
[])Array
.newInstance(newType
.getComponentType(), newLength
);
58 System
.arraycopy(array
, 0, result
, 0, Math
.min(array
.length
, newLength
));
62 * Replace {@link java.util.Arrays#copyOfRange(Object[], int, int)} that do not exist in java 5.
63 * @param <E> Content of the array.
64 * @param array Original array to copy from.
65 * @param start Starting position of the range, inclusive.
66 * @param end Ending position of the range, exclusive.
67 * @return A new array consisting of the elements specified. The length of the new array is equal to end-start
69 @SuppressWarnings("unchecked")
70 private static <E
> E
[] copyOfRange(final E
[] array
, int start
, int end
) {
71 E
[] result
= (E
[])Array
.newInstance(array
.getClass().getComponentType(), end
- start
);
72 System
.arraycopy(array
, start
, result
, 0, end
- start
);
78 private final T
[] fArray
;
81 * @param array Array to use. WILL NOT BE COPIED.
83 public TmfFixedArray(final T
... array
) {
87 * Append a FixedArray to this FixedArray.
88 * @param value The FixedArray to append.
89 * @return A new FixedArray with the elements of the two FixedArray.
91 public TmfFixedArray
<T
> append(final TmfFixedArray
<T
> value
) {
92 TmfFixedArray
<T
> result
= new TmfFixedArray
<T
>(copyOf(fArray
, fArray
.length
+ value
.size()));
93 System
.arraycopy(value
.fArray
, 0, result
.fArray
, fArray
.length
, value
.fArray
.length
);
97 * Append in order many FixedArray to this FixedArray.
98 * @param values The FixedArrays to append.
99 * @return A new FixedArray with the element of all the FixedArray.
101 public TmfFixedArray
<T
> append(final TmfFixedArray
<T
>... values
) {
103 for(TmfFixedArray
<T
> value
: values
)
104 newLength
+= value
.size();
105 TmfFixedArray
<T
> result
= new TmfFixedArray
<T
>(copyOf(fArray
, fArray
.length
+ newLength
));
106 newLength
= fArray
.length
;
107 for(TmfFixedArray
<T
> value
: values
)
109 System
.arraycopy(value
.fArray
, 0, result
.fArray
, newLength
, value
.fArray
.length
);
110 newLength
+= value
.fArray
.length
;
115 * Append an element to the array.
116 * @param value Element to append.
117 * @return A new FixedArray with the element appended.
119 public TmfFixedArray
<T
> append(final T value
) {
120 TmfFixedArray
<T
> result
= new TmfFixedArray
<T
>(copyOf(fArray
, fArray
.length
+ 1));
121 result
.set(fArray
.length
, value
);
125 * Append an array of element to the array.
126 * @param values Elements array to append.
127 * @return A new FixedArray with the elements appended.
129 public TmfFixedArray
<T
> append(final T
... values
) {
130 TmfFixedArray
<T
> result
= new TmfFixedArray
<T
>(copyOf(fArray
, fArray
.length
+ values
.length
));
131 for(int i
= 0; i
< values
.length
; ++i
)
132 result
.set(fArray
.length
+ i
, values
[i
]);
137 * @see java.lang.Object#clone()
140 public Object
clone()
142 return new TmfFixedArray
<T
>(copyOf(fArray
, fArray
.length
));
146 * @see java.util.AbstractList#equals(java.lang.Object)
149 public boolean equals(Object o
) {
150 if(o
instanceof TmfFixedArray
<?
>)
151 return Arrays
.equals(fArray
, ((TmfFixedArray
<?
>)o
).fArray
);
152 if(!(o
instanceof List
))
154 for(int i
= 0; i
< fArray
.length
; ++i
)
155 if(!fArray
[i
].equals((List
<?
>)o
))
161 * @see java.util.AbstractList#get(int)
164 public T
get(int index
) {
165 return fArray
[index
];
168 * Get the array reference.
169 * @return The array reference.
170 * @see #toArray FixedArray.toArray() to get a copy of the array.
172 public T
[] getArray() {
177 * @see java.util.AbstractList#hashCode()
180 public int hashCode() {
181 return Arrays
.hashCode(fArray
);
185 * @see java.util.AbstractList#set(int, java.lang.Object)
188 public T
set(int index
, T element
) {
189 T temp
= fArray
[index
];
190 fArray
[index
] = element
;
195 * @see java.util.AbstractCollection#size()
199 return fArray
.length
;
202 * Get a array covering only a part of the array.
203 * @param start Starting position of the new array.
204 * @return A new array covering the elements specified.
206 public TmfFixedArray
<T
> subArray(int start
) {
207 return new TmfFixedArray
<T
>(copyOfRange(fArray
, start
, fArray
.length
));
210 * Get a array covering only a part of the array.
211 * @param start Starting position of the new array.
212 * @param length Number of element to include in the new array.
213 * @return A new array covering the elements specified.
215 public TmfFixedArray
<T
> subArray(int start
, int length
) {
216 return new TmfFixedArray
<T
>(copyOfRange(fArray
, start
, length
+ start
));
220 * @see java.util.AbstractCollection#toArray()
225 return copyOf(fArray
, fArray
.length
);
229 * @see java.util.AbstractCollection#toArray(T[])
232 @SuppressWarnings("unchecked")
233 public <E
> E
[] toArray(E
[] array
)
235 if(array
.length
< fArray
.length
)
236 return copyOf(fArray
, fArray
.length
,(Class
<?
extends E
[]>)array
.getClass());
237 System
.arraycopy(fArray
, 0, array
, 0, fArray
.length
);
238 if(array
.length
> fArray
.length
)
239 array
[fArray
.length
] = null;
244 * @see java.util.AbstractCollection#toString()
247 public String
toString() {
248 return Arrays
.toString(fArray
);
This page took 0.035863 seconds and 5 git commands to generate.