fa5428e6be3e79b4f11bb06f0a55e6cd80c11770
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / internal / tmf / core / trace / experiment / TmfLocationArray.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * 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 *******************************************************************************/
14
15 package org.eclipse.tracecompass.internal.tmf.core.trace.experiment;
16
17 import java.util.Arrays;
18
19 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
20
21
22 /**
23 * A convenience class to store trace location arrays. The main purpose is to
24 * provide an immutable and Comparable implementation for TmfExperimentLocation.
25 *
26 * @version 1.0
27 * @author Patrick Tasse
28 */
29 public final class TmfLocationArray implements Comparable<TmfLocationArray> {
30
31 // ------------------------------------------------------------------------
32 // Attributes
33 // ------------------------------------------------------------------------
34
35 private final ITmfLocation[] fLocations;
36 private final long [] fRanks;
37
38 // ------------------------------------------------------------------------
39 // Constructors
40 // ------------------------------------------------------------------------
41
42 /**
43 * The standard constructor.
44 *
45 * @param locations the locations
46 * @param ranks the ranks
47 */
48 public TmfLocationArray(ITmfLocation[] locations, long[] ranks) {
49 fLocations = Arrays.copyOf(locations, locations.length);
50 fRanks = Arrays.copyOf(ranks, ranks.length);
51 }
52
53 /**
54 * The update constructor. Copies the arrays and updates a single entry.
55 *
56 * @param locationArray the location array
57 * @param index the updated index
58 * @param location the updated location
59 * @param rank the updated rank
60 */
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);
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];
76 }
77
78 // ------------------------------------------------------------------------
79 // Getters
80 // ------------------------------------------------------------------------
81
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
100 /**
101 * Get a specific location
102 *
103 * @param index the location element
104 *
105 * @return the specific location (possibly null)
106 */
107 public ITmfLocation getLocation(int index) {
108 if (index >= 0 && index < fLocations.length) {
109 return fLocations[index];
110 }
111 return null;
112 }
113
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
137 // ------------------------------------------------------------------------
138 // Comparable
139 // ------------------------------------------------------------------------
140
141 @Override
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];
146 if (rank1 < rank2) {
147 return -1;
148 } else if (rank1 > rank2) {
149 return 1;
150 }
151 }
152 return 0;
153 }
154
155 // ------------------------------------------------------------------------
156 // Object
157 // ------------------------------------------------------------------------
158
159 @Override
160 public int hashCode() {
161 final int prime = 31;
162 int result = 1;
163 result = prime * result + Arrays.hashCode(fLocations);
164 result = prime * result + Arrays.hashCode(fRanks);
165 return result;
166 }
167
168 @Override
169 public boolean equals(Object obj) {
170 if (this == obj) {
171 return true;
172 }
173 if (obj == null) {
174 return false;
175 }
176 if (getClass() != obj.getClass()) {
177 return false;
178 }
179 TmfLocationArray other = (TmfLocationArray) obj;
180 if (!Arrays.equals(fLocations, other.fLocations)) {
181 return false;
182 }
183 if (!Arrays.equals(fRanks, other.fRanks)) {
184 return false;
185 }
186 return true;
187 }
188
189 @Override
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++) {
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();
202 }
203
204 }
This page took 0.041494 seconds and 4 git commands to generate.