Internalize lttng.core APIs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / statistics / model / FixedArray.java
CommitLineData
9dbeec54
FC
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
13package org.eclipse.linuxtools.lttng.ui.views.statistics.model;
14
9dbeec54 15import java.util.Arrays;
9dbeec54
FC
16import 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 */
c6f55e56 29public final class FixedArray implements RandomAccess, Cloneable {
9dbeec54
FC
30 /**
31 * Replace {@link java.util.Arrays#copyOf(Object[], int)} that do not exist in java 5.
9dbeec54
FC
32 * @param array Original array to copy from.
33 * @param newLength Length of the copy to be returned.
9dbeec54
FC
34 * @return A new array consisting of the elements specified.
35 */
b12f4544
FC
36 private static int[] copyOf(final int[] array, int newLength) {
37 int[] result = new int[newLength]; // Is it useful to use newInstance?
9dbeec54
FC
38 System.arraycopy(array, 0, result, 0, Math.min(array.length, newLength));
39 return result;
40 }
b12f4544 41
9dbeec54
FC
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 */
b12f4544
FC
50 private static int[] copyOfRange(final int[] array, int start, int end) {
51 int[] result = new int[end - start];
9dbeec54
FC
52 System.arraycopy(array, start, result, 0, end - start);
53 return result;
54 }
55 /**
56 * The array.
57 */
b12f4544
FC
58 private final int[] fArray;
59
9dbeec54
FC
60 /**
61 * Constructor.
62 * @param array Array to use. WILL NOT BE COPIED.
63 */
b12f4544 64 public FixedArray(final int... array) {
9dbeec54
FC
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 */
b12f4544
FC
72 public FixedArray append(final FixedArray value) {
73 FixedArray result = new FixedArray(copyOf(fArray, fArray.length + value.size()));
9dbeec54
FC
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 */
b12f4544 82 public FixedArray append(final FixedArray... values) {
9dbeec54 83 int newLength = 0;
b12f4544 84 for(FixedArray value : values)
9dbeec54 85 newLength += value.size();
b12f4544 86 FixedArray result = new FixedArray(copyOf(fArray, fArray.length + newLength));
9dbeec54 87 newLength = fArray.length;
b12f4544 88 for(FixedArray value : values)
9dbeec54
FC
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 */
b12f4544
FC
100 public FixedArray append(final int value) {
101 FixedArray result = new FixedArray(copyOf(fArray, fArray.length + 1));
102 result.fArray[fArray.length] = value;
9dbeec54
FC
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 */
b12f4544
FC
110 public FixedArray append(final int... values) {
111 FixedArray result = new FixedArray(copyOf(fArray, fArray.length + values.length));
9dbeec54 112 for(int i = 0; i < values.length; ++i)
b12f4544 113 result.fArray[fArray.length + i] = values[i];
9dbeec54
FC
114 return result;
115 }
116 /*
117 * (non-Javadoc)
118 * @see java.lang.Object#clone()
119 */
120 @Override
b12f4544
FC
121 public Object clone() {
122 return new FixedArray(copyOf(fArray, fArray.length));
9dbeec54 123 }
c6f55e56 124
9dbeec54
FC
125 /*
126 * (non-Javadoc)
b12f4544 127 * @see java.lang.Object#equals(java.lang.Object)
9dbeec54
FC
128 */
129 @Override
c6f55e56
FC
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
b12f4544
FC
141 /**
142 * Gets value of given index.
143 * @param index
144 * @return Value of given index
9dbeec54 145 */
b12f4544 146 public int get(int index) {
9dbeec54
FC
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 */
b12f4544 154 public int[] getArray() {
9dbeec54
FC
155 return fArray;
156 }
157 /*
158 * (non-Javadoc)
b12f4544 159 * @see java.lang.Object#hashCode()
9dbeec54
FC
160 */
161 @Override
162 public int hashCode() {
88f497f7 163 return Arrays.hashCode(fArray);
9dbeec54 164 }
b12f4544
FC
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;
9dbeec54
FC
174 return temp;
175 }
b12f4544
FC
176 /**
177 * Gets the size of the array.
178 * @return Size of the array.
9dbeec54 179 */
9dbeec54
FC
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 */
b12f4544
FC
188 public FixedArray subArray(int start) {
189 return new FixedArray(copyOfRange(fArray, start, fArray.length - 1));
9dbeec54
FC
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 */
b12f4544
FC
197 public FixedArray subArray(int start, int length) {
198 return new FixedArray(copyOfRange(fArray, start, length + start));
9dbeec54
FC
199 }
200 /*
201 * (non-Javadoc)
b12f4544 202 * @see java.lang.Object#toString()
9dbeec54
FC
203 */
204 @Override
205 public String toString() {
206 return Arrays.toString(fArray);
207 }
208}
This page took 0.035255 seconds and 5 git commands to generate.