7f50fb9e64bae47c62cfe030ceff64f295dc4ebd
1 /*******************************************************************************
2 * Copyright (c) 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.statistics
.model
;
15 import java
.util
.Arrays
;
16 import java
.util
.RandomAccess
;
19 * <h4>Allow to create a List object that contain an already existing array.</h4>
20 * <p>Works like {@link java.util.Arrays#asList} but offers more functions :
22 * <li>{@link #hashCode()}</li>
23 * <li>{@link #equals(Object)}</li>
25 * <p>Those functions allow to use the FixedArray as the key of a {@link java.util.HashMap}.</p>
27 * @param <T> Type of the array content.
29 public final class FixedArray
implements RandomAccess
, Cloneable
{
31 * Replace {@link java.util.Arrays#copyOf(Object[], int)} that do not exist in java 5.
32 * @param array Original array to copy from.
33 * @param newLength Length of the copy to be returned.
34 * @return A new array consisting of the elements specified.
36 private static int[] copyOf(final int[] array
, int newLength
) {
37 int[] result
= new int[newLength
]; // Is it useful to use newInstance?
38 System
.arraycopy(array
, 0, result
, 0, Math
.min(array
.length
, newLength
));
43 * Replace {@link java.util.Arrays#copyOfRange(Object[], int, int)} that do not exist in java 5.
44 * @param <E> Content of the array.
45 * @param array Original array to copy from.
46 * @param start Starting position of the range.
47 * @param end Ending position of the range.
48 * @return A new array consisting of the elements specified.
50 private static int[] copyOfRange(final int[] array
, int start
, int end
) {
51 int[] result
= new int[end
- start
];
52 System
.arraycopy(array
, start
, result
, 0, end
- start
);
58 private final int[] fArray
;
62 * @param array Array to use. WILL NOT BE COPIED.
64 public FixedArray(final int... array
) {
68 * Append a FixedArray to this FixedArray.
69 * @param value The FixedArray to append.
70 * @return A new FixedArray with the elements of the two FixedArray.
72 public FixedArray
append(final FixedArray value
) {
73 FixedArray result
= new FixedArray(copyOf(fArray
, fArray
.length
+ value
.size()));
74 System
.arraycopy(value
.fArray
, 0, result
.fArray
, fArray
.length
, value
.fArray
.length
);
78 * Append in order many FixedArray to this FixedArray.
79 * @param values The FixedArrays to append.
80 * @return A new FixedArray with the element of all the FixedArray.
82 public FixedArray
append(final FixedArray
... values
) {
84 for(FixedArray value
: values
)
85 newLength
+= value
.size();
86 FixedArray result
= new FixedArray(copyOf(fArray
, fArray
.length
+ newLength
));
87 newLength
= fArray
.length
;
88 for(FixedArray value
: values
)
90 System
.arraycopy(value
.fArray
, 0, result
.fArray
, newLength
, value
.fArray
.length
);
91 newLength
+= value
.fArray
.length
;
96 * Append an element to the array.
97 * @param value Element to append.
98 * @return A new FixedArray with the element appended.
100 public FixedArray
append(final int value
) {
101 FixedArray result
= new FixedArray(copyOf(fArray
, fArray
.length
+ 1));
102 result
.fArray
[fArray
.length
] = value
;
106 * Append an array of element to the array.
107 * @param values Elements array to append.
108 * @return A new FixedArray with the elements appended.
110 public FixedArray
append(final int... values
) {
111 FixedArray result
= new FixedArray(copyOf(fArray
, fArray
.length
+ values
.length
));
112 for(int i
= 0; i
< values
.length
; ++i
)
113 result
.fArray
[fArray
.length
+ i
] = values
[i
];
118 * @see java.lang.Object#clone()
121 public Object
clone() {
122 return new FixedArray(copyOf(fArray
, fArray
.length
));
127 * @see java.lang.Object#equals(java.lang.Object)
130 public boolean equals(Object other
) {
135 if (!(other
instanceof FixedArray
))
137 FixedArray array
= (FixedArray
) other
;
138 return Arrays
.equals(fArray
, array
.fArray
);
142 * Gets value of given index.
144 * @return Value of given index
146 public int get(int index
) {
147 return fArray
[index
];
150 * Get the array reference.
151 * @return The array reference.
152 * @see #toArray FixedArray.toArray() to get a copy of the array.
154 public int[] getArray() {
159 * @see java.lang.Object#hashCode()
162 public int hashCode() {
163 return Arrays
.hashCode(fArray
);
166 * Sets value at given index.
169 * @return returns old value.
171 public int set(int index
, int value
) {
172 int temp
= fArray
[index
];
173 fArray
[index
] = value
;
177 * Gets the size of the array.
178 * @return Size of the array.
181 return fArray
.length
;
184 * Get a array covering only a part of the array.
185 * @param start Starting position of the new array.
186 * @return A new array covering the elements specified.
188 public FixedArray
subArray(int start
) {
189 return new FixedArray(copyOfRange(fArray
, start
, fArray
.length
- 1));
192 * Get a array covering only a part of the array.
193 * @param start Starting position of the new array.
194 * @param length Number of element to include in the new array.
195 * @return A new array covering the elements specified.
197 public FixedArray
subArray(int start
, int length
) {
198 return new FixedArray(copyOfRange(fArray
, start
, length
+ start
));
202 * @see java.lang.Object#toString()
205 public String
toString() {
206 return Arrays
.toString(fArray
);
This page took 0.035389 seconds and 4 git commands to generate.