Commit | Line | Data |
---|---|---|
a79913eb | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson |
9b749023 | 3 | * |
a79913eb FC |
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 | |
9b749023 | 8 | * |
a79913eb | 9 | * Contributors: |
0316808c FC |
10 | * Patrick Tasse - Initial API and implementation |
11 | * Francois Chouinard - Put in shape for 1.0 | |
ea271da6 | 12 | * Patrick Tasse - Updated for ranks in experiment location |
a79913eb FC |
13 | *******************************************************************************/ |
14 | ||
9e0640dc | 15 | package org.eclipse.linuxtools.internal.tmf.core.trace; |
a79913eb | 16 | |
478e04a4 PT |
17 | import java.util.Arrays; |
18 | ||
a3db8436 | 19 | import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation; |
5cc97265 | 20 | |
d62bb185 | 21 | |
0316808c FC |
22 | /** |
23 | * A convenience class to store trace location arrays. The main purpose is to | |
ea271da6 | 24 | * provide an immutable and Comparable implementation for TmfExperimentLocation. |
9b749023 | 25 | * |
0316808c FC |
26 | * @version 1.0 |
27 | * @author Patrick Tasse | |
28 | */ | |
d62bb185 | 29 | public final class TmfLocationArray implements Comparable<TmfLocationArray> { |
0316808c FC |
30 | |
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
1e1bef82 | 35 | private final ITmfLocation[] fLocations; |
ea271da6 | 36 | private final long [] fRanks; |
0316808c FC |
37 | |
38 | // ------------------------------------------------------------------------ | |
39 | // Constructors | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
42 | /** | |
ea271da6 | 43 | * The standard constructor. |
9b749023 | 44 | * |
0316808c | 45 | * @param locations the locations |
ea271da6 | 46 | * @param ranks the ranks |
0316808c | 47 | */ |
ea271da6 PT |
48 | public TmfLocationArray(ITmfLocation[] locations, long[] ranks) { |
49 | fLocations = Arrays.copyOf(locations, locations.length); | |
50 | fRanks = Arrays.copyOf(ranks, ranks.length); | |
0316808c FC |
51 | } |
52 | ||
0316808c | 53 | /** |
ea271da6 | 54 | * The update constructor. Copies the arrays and updates a single entry. |
9b749023 | 55 | * |
ea271da6 PT |
56 | * @param locationArray the location array |
57 | * @param index the updated index | |
58 | * @param location the updated location | |
59 | * @param rank the updated rank | |
0316808c | 60 | */ |
ea271da6 PT |
61 | public TmfLocationArray(TmfLocationArray locationArray, int index, ITmfLocation location, long rank) { |
62 | fLocations = Arrays.copyOf(locationArray.fLocations, locationArray.fLocations.length); | |
d62bb185 | 63 | fLocations[index] = location; |
ea271da6 PT |
64 | fRanks = Arrays.copyOf(locationArray.fRanks, locationArray.fRanks.length); |
65 | fRanks[index] = rank; | |
66 | } | |
67 | ||
68 | /** | |
69 | * The empty constructor. | |
70 | * | |
71 | * @param size the number of elements in the array | |
72 | */ | |
73 | public TmfLocationArray(int size) { | |
74 | fLocations = new ITmfLocation[size]; | |
75 | fRanks = new long[size]; | |
0316808c FC |
76 | } |
77 | ||
78 | // ------------------------------------------------------------------------ | |
d62bb185 | 79 | // Getters |
0316808c FC |
80 | // ------------------------------------------------------------------------ |
81 | ||
ea271da6 PT |
82 | /** |
83 | * Returns the number of elements in this array. | |
84 | * | |
85 | * @return the number of elements in this array | |
86 | */ | |
87 | public int size() { | |
88 | return fLocations.length; | |
89 | } | |
90 | ||
91 | /** | |
92 | * Get the locations inside this array. | |
93 | * | |
94 | * @return a copy of the locations array | |
95 | */ | |
96 | public ITmfLocation[] getLocations() { | |
97 | return Arrays.copyOf(fLocations, fLocations.length); | |
98 | } | |
99 | ||
d62bb185 FC |
100 | /** |
101 | * Get a specific location | |
102 | * | |
103 | * @param index the location element | |
104 | * | |
105 | * @return the specific location (possibly null) | |
0316808c | 106 | */ |
d62bb185 | 107 | public ITmfLocation getLocation(int index) { |
ea271da6 | 108 | if (index >= 0 && index < fLocations.length) { |
d62bb185 | 109 | return fLocations[index]; |
0316808c | 110 | } |
d62bb185 | 111 | return null; |
0316808c FC |
112 | } |
113 | ||
ea271da6 PT |
114 | /** |
115 | * Get the ranks inside this array. | |
116 | * | |
117 | * @return a copy of the ranks array | |
118 | */ | |
119 | public long[] getRanks() { | |
120 | return Arrays.copyOf(fRanks, fRanks.length); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Get a specific rank | |
125 | * | |
126 | * @param index the rank element | |
127 | * | |
128 | * @return the specific rank | |
129 | */ | |
130 | public long getRank(int index) { | |
131 | if (index >= 0 && index < fRanks.length) { | |
132 | return fRanks[index]; | |
133 | } | |
134 | return 0; | |
135 | } | |
136 | ||
0316808c FC |
137 | // ------------------------------------------------------------------------ |
138 | // Comparable | |
139 | // ------------------------------------------------------------------------ | |
478e04a4 | 140 | |
0316808c | 141 | @Override |
0316808c | 142 | public int compareTo(TmfLocationArray o) { |
ea271da6 | 143 | for (int i = 0; i < fRanks.length; i++) { |
0126a8ca AM |
144 | long rank1 = fRanks[i]; |
145 | long rank2 = o.fRanks[i]; | |
146 | if (rank1 < rank2) { | |
147 | return -1; | |
148 | } else if (rank1 > rank2) { | |
149 | return 1; | |
0316808c FC |
150 | } |
151 | } | |
152 | return 0; | |
153 | } | |
154 | ||
155 | // ------------------------------------------------------------------------ | |
156 | // Object | |
157 | // ------------------------------------------------------------------------ | |
158 | ||
478e04a4 PT |
159 | @Override |
160 | public int hashCode() { | |
161 | final int prime = 31; | |
162 | int result = 1; | |
0316808c | 163 | result = prime * result + Arrays.hashCode(fLocations); |
ea271da6 | 164 | result = prime * result + Arrays.hashCode(fRanks); |
478e04a4 PT |
165 | return result; |
166 | } | |
167 | ||
168 | @Override | |
169 | public boolean equals(Object obj) { | |
0316808c | 170 | if (this == obj) { |
478e04a4 | 171 | return true; |
0316808c FC |
172 | } |
173 | if (obj == null) { | |
478e04a4 | 174 | return false; |
0316808c FC |
175 | } |
176 | if (getClass() != obj.getClass()) { | |
478e04a4 | 177 | return false; |
0316808c | 178 | } |
478e04a4 | 179 | TmfLocationArray other = (TmfLocationArray) obj; |
0316808c | 180 | if (!Arrays.equals(fLocations, other.fLocations)) { |
478e04a4 | 181 | return false; |
0316808c | 182 | } |
ea271da6 PT |
183 | if (!Arrays.equals(fRanks, other.fRanks)) { |
184 | return false; | |
185 | } | |
478e04a4 PT |
186 | return true; |
187 | } | |
188 | ||
0316808c FC |
189 | @Override |
190 | @SuppressWarnings("nls") | |
191 | public String toString() { | |
ea271da6 PT |
192 | StringBuilder sb = new StringBuilder(); |
193 | sb.append(getClass().getSimpleName() + " ["); | |
194 | for (int i = 0; i < fLocations.length; i++) { | |
195 | if (i > 0) { | |
196 | sb.append(", "); | |
197 | } | |
198 | sb.append("[location=" + fLocations[i] + ",rank=" + fRanks[i] + "]"); | |
199 | } | |
200 | sb.append("]"); | |
201 | return sb.toString(); | |
0316808c | 202 | } |
a79913eb | 203 | |
0316808c | 204 | } |