1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 * Patrick Tasse - Initial API and implementation
11 * Francois Chouinard - Put in shape for 1.0
12 * Patrick Tasse - Updated for ranks in experiment location
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
;
17 import java
.util
.Arrays
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
23 * A convenience class to store trace location arrays. The main purpose is to
24 * provide an immutable and Comparable implementation for TmfExperimentLocation.
27 * @author Patrick Tasse
29 public final class TmfLocationArray
implements Comparable
<TmfLocationArray
> {
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 private final ITmfLocation
[] fLocations
;
36 private final long [] fRanks
;
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
43 * The standard constructor.
45 * @param locations the locations
46 * @param ranks the ranks
48 public TmfLocationArray(ITmfLocation
[] locations
, long[] ranks
) {
49 fLocations
= Arrays
.copyOf(locations
, locations
.length
);
50 fRanks
= Arrays
.copyOf(ranks
, ranks
.length
);
54 * The update constructor. Copies the arrays and updates a single entry.
56 * @param locationArray the location array
57 * @param index the updated index
58 * @param location the updated location
59 * @param rank the updated rank
61 public TmfLocationArray(TmfLocationArray locationArray
, int index
, ITmfLocation location
, long rank
) {
62 fLocations
= Arrays
.copyOf(locationArray
.fLocations
, locationArray
.fLocations
.length
);
63 fLocations
[index
] = location
;
64 fRanks
= Arrays
.copyOf(locationArray
.fRanks
, locationArray
.fRanks
.length
);
69 * The empty constructor.
71 * @param size the number of elements in the array
73 public TmfLocationArray(int size
) {
74 fLocations
= new ITmfLocation
[size
];
75 fRanks
= new long[size
];
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
83 * Returns the number of elements in this array.
85 * @return the number of elements in this array
88 return fLocations
.length
;
92 * Get the locations inside this array.
94 * @return a copy of the locations array
96 public ITmfLocation
[] getLocations() {
97 return Arrays
.copyOf(fLocations
, fLocations
.length
);
101 * Get a specific location
103 * @param index the location element
105 * @return the specific location (possibly null)
107 public ITmfLocation
getLocation(int index
) {
108 if (index
>= 0 && index
< fLocations
.length
) {
109 return fLocations
[index
];
115 * Get the ranks inside this array.
117 * @return a copy of the ranks array
119 public long[] getRanks() {
120 return Arrays
.copyOf(fRanks
, fRanks
.length
);
124 * Get a specific rank
126 * @param index the rank element
128 * @return the specific rank
130 public long getRank(int index
) {
131 if (index
>= 0 && index
< fRanks
.length
) {
132 return fRanks
[index
];
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
142 public int compareTo(TmfLocationArray o
) {
143 for (int i
= 0; i
< fRanks
.length
; i
++) {
144 long rank1
= fRanks
[i
];
145 long rank2
= o
.fRanks
[i
];
148 } else if (rank1
> rank2
) {
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
160 public int hashCode() {
161 final int prime
= 31;
163 result
= prime
* result
+ Arrays
.hashCode(fLocations
);
164 result
= prime
* result
+ Arrays
.hashCode(fRanks
);
169 public boolean equals(Object obj
) {
176 if (getClass() != obj
.getClass()) {
179 TmfLocationArray other
= (TmfLocationArray
) obj
;
180 if (!Arrays
.equals(fLocations
, other
.fLocations
)) {
183 if (!Arrays
.equals(fRanks
, other
.fRanks
)) {
190 @SuppressWarnings("nls")
191 public String
toString() {
192 StringBuilder sb
= new StringBuilder();
193 sb
.append(getClass().getSimpleName() + " [");
194 for (int i
= 0; i
< fLocations
.length
; i
++) {
198 sb
.append("[location=" + fLocations
[i
] + ",rank=" + fRanks
[i
] + "]");
201 return sb
.toString();