Internalize lttng.ui APIs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / internal / lttng / ui / views / statistics / model / FixedArray.java
1 /*******************************************************************************
2 * Copyright (c) 2010 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.lttng.ui.views.statistics.model;
14
15 import java.util.Arrays;
16 import java.util.RandomAccess;
17
18 /**
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 :
21 * <ul>
22 * <li>{@link #hashCode()}</li>
23 * <li>{@link #equals(Object)}</li>
24 * </ul></p>
25 * <p>Those functions allow to use the FixedArray as the key of a {@link java.util.HashMap}.</p>
26 *
27 * @param <T> Type of the array content.
28 */
29 public final class FixedArray implements RandomAccess, Cloneable {
30 /**
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.
35 */
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));
39 return result;
40 }
41
42 /**
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.
49 */
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);
53 return result;
54 }
55 /**
56 * The array.
57 */
58 private final int[] fArray;
59
60 /**
61 * Constructor.
62 * @param array Array to use. WILL NOT BE COPIED.
63 */
64 public FixedArray(final int... array) {
65 fArray = array;
66 }
67 /**
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.
71 */
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);
75 return result;
76 }
77 /**
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.
81 */
82 public FixedArray append(final FixedArray... values) {
83 int newLength = 0;
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)
89 {
90 System.arraycopy(value.fArray, 0, result.fArray, newLength, value.fArray.length);
91 newLength += value.fArray.length;
92 }
93 return result;
94 }
95 /**
96 * Append an element to the array.
97 * @param value Element to append.
98 * @return A new FixedArray with the element appended.
99 */
100 public FixedArray append(final int value) {
101 FixedArray result = new FixedArray(copyOf(fArray, fArray.length + 1));
102 result.fArray[fArray.length] = value;
103 return result;
104 }
105 /**
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.
109 */
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];
114 return result;
115 }
116 /*
117 * (non-Javadoc)
118 * @see java.lang.Object#clone()
119 */
120 @Override
121 public Object clone() {
122 return new FixedArray(copyOf(fArray, fArray.length));
123 }
124
125 /*
126 * (non-Javadoc)
127 * @see java.lang.Object#equals(java.lang.Object)
128 */
129 @Override
130 public boolean equals(Object other) {
131 if (this == other)
132 return true;
133 if (other == null)
134 return false;
135 if (!(other instanceof FixedArray))
136 return false;
137 FixedArray array = (FixedArray) other;
138 return Arrays.equals(fArray, array.fArray);
139 }
140
141 /**
142 * Gets value of given index.
143 * @param index
144 * @return Value of given index
145 */
146 public int get(int index) {
147 return fArray[index];
148 }
149 /**
150 * Get the array reference.
151 * @return The array reference.
152 * @see #toArray FixedArray.toArray() to get a copy of the array.
153 */
154 public int[] getArray() {
155 return fArray;
156 }
157 /*
158 * (non-Javadoc)
159 * @see java.lang.Object#hashCode()
160 */
161 @Override
162 public int hashCode() {
163 return Arrays.hashCode(fArray);
164 }
165 /**
166 * Sets value at given index.
167 * @param index
168 * @param value
169 * @return returns old value.
170 */
171 public int set(int index, int value) {
172 int temp = fArray[index];
173 fArray[index] = value;
174 return temp;
175 }
176 /**
177 * Gets the size of the array.
178 * @return Size of the array.
179 */
180 public int size() {
181 return fArray.length;
182 }
183 /**
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.
187 */
188 public FixedArray subArray(int start) {
189 return new FixedArray(copyOfRange(fArray, start, fArray.length - 1));
190 }
191 /**
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.
196 */
197 public FixedArray subArray(int start, int length) {
198 return new FixedArray(copyOfRange(fArray, start, length + start));
199 }
200 /*
201 * (non-Javadoc)
202 * @see java.lang.Object#toString()
203 */
204 @Override
205 public String toString() {
206 return Arrays.toString(fArray);
207 }
208 }
This page took 0.0397 seconds and 5 git commands to generate.