/*******************************************************************************
- * Copyright (c) 2011, 2012 Ericsson
- *
+ * Copyright (c) 2011, 2013 Ericsson
+ *
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- *
+ *
* Contributors:
* Patrick Tasse - Initial API and implementation
* Francois Chouinard - Put in shape for 1.0
+ * Patrick Tasse - Updated for ranks in experiment location
*******************************************************************************/
package org.eclipse.linuxtools.internal.tmf.core.trace;
import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
+
/**
* A convenience class to store trace location arrays. The main purpose is to
- * provide a Comparable implementation for TmfExperimentLocation.
- *
+ * provide an immutable and Comparable implementation for TmfExperimentLocation.
+ *
* @version 1.0
* @author Patrick Tasse
*/
-public class TmfLocationArray implements Comparable<TmfLocationArray>, Cloneable {
+public final class TmfLocationArray implements Comparable<TmfLocationArray> {
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
- private ITmfLocation<? extends Comparable<?>>[] fLocations;
+ private final ITmfLocation[] fLocations;
+ private final long [] fRanks;
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
/**
- * The standard constructor
- *
+ * The standard constructor.
+ *
* @param locations the locations
+ * @param ranks the ranks
+ */
+ public TmfLocationArray(ITmfLocation[] locations, long[] ranks) {
+ fLocations = Arrays.copyOf(locations, locations.length);
+ fRanks = Arrays.copyOf(ranks, ranks.length);
+ }
+
+ /**
+ * The update constructor. Copies the arrays and updates a single entry.
+ *
+ * @param locationArray the location array
+ * @param index the updated index
+ * @param location the updated location
+ * @param rank the updated rank
*/
- public TmfLocationArray(ITmfLocation<? extends Comparable<?>>[] locations) {
- fLocations = locations;
+ public TmfLocationArray(TmfLocationArray locationArray, int index, ITmfLocation location, long rank) {
+ fLocations = Arrays.copyOf(locationArray.fLocations, locationArray.fLocations.length);
+ fLocations[index] = location;
+ fRanks = Arrays.copyOf(locationArray.fRanks, locationArray.fRanks.length);
+ fRanks[index] = rank;
+ }
+
+ /**
+ * The empty constructor.
+ *
+ * @param size the number of elements in the array
+ */
+ public TmfLocationArray(int size) {
+ fLocations = new ITmfLocation[size];
+ fRanks = new long[size];
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
/**
- * The standard constructor
- *
- * @param locations the locations
+ * Returns the number of elements in this array.
+ *
+ * @return the number of elements in this array
*/
- public ITmfLocation<? extends Comparable<?>>[] getLocations() {
- return fLocations;
+ public int size() {
+ return fLocations.length;
}
- // ------------------------------------------------------------------------
- // Cloneable
- // ------------------------------------------------------------------------
+ /**
+ * Get the locations inside this array.
+ *
+ * @return a copy of the locations array
+ */
+ public ITmfLocation[] getLocations() {
+ return Arrays.copyOf(fLocations, fLocations.length);
+ }
- /* (non-Javadoc)
- * @see java.lang.Object#clone()
+ /**
+ * Get a specific location
+ *
+ * @param index the location element
+ *
+ * @return the specific location (possibly null)
*/
- @Override
- public TmfLocationArray clone() {
- ITmfLocation<? extends Comparable<?>>[] clones = (ITmfLocation<? extends Comparable<?>>[]) new ITmfLocation<?>[fLocations.length];
- for (int i = 0; i < fLocations.length; i++) {
- clones[i] = fLocations[i].clone();
+ public ITmfLocation getLocation(int index) {
+ if (index >= 0 && index < fLocations.length) {
+ return fLocations[index];
}
- return new TmfLocationArray(clones);
+ return null;
+ }
+
+ /**
+ * Get the ranks inside this array.
+ *
+ * @return a copy of the ranks array
+ */
+ public long[] getRanks() {
+ return Arrays.copyOf(fRanks, fRanks.length);
+ }
+
+ /**
+ * Get a specific rank
+ *
+ * @param index the rank element
+ *
+ * @return the specific rank
+ */
+ public long getRank(int index) {
+ if (index >= 0 && index < fRanks.length) {
+ return fRanks[index];
+ }
+ return 0;
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
@Override
- @SuppressWarnings({ "unchecked", "rawtypes" })
public int compareTo(TmfLocationArray o) {
- for (int i = 0; i < fLocations.length; i++) {
- ITmfLocation<? extends Comparable> l1 = (ITmfLocation<? extends Comparable>) fLocations[i].getLocation();
- ITmfLocation<? extends Comparable> l2 = (ITmfLocation<? extends Comparable>) o.fLocations[i].getLocation();
- int result = l1.getLocation().compareTo(l2.getLocation());
+ for (int i = 0; i < fRanks.length; i++) {
+ Long rank1 = fRanks[i];
+ Long rank2 = o.fRanks[i];
+ int result = rank1.compareTo(rank2);
if (result != 0) {
return result;
}
// Object
// ------------------------------------------------------------------------
- /* (non-Javadoc)
- * @see java.lang.Object#hashCode()
- */
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(fLocations);
+ result = prime * result + Arrays.hashCode(fRanks);
return result;
}
- /* (non-Javadoc)
- * @see java.lang.Object#equals(java.lang.Object)
- */
@Override
public boolean equals(Object obj) {
if (this == obj) {
if (!Arrays.equals(fLocations, other.fLocations)) {
return false;
}
+ if (!Arrays.equals(fRanks, other.fRanks)) {
+ return false;
+ }
return true;
}
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
@Override
@SuppressWarnings("nls")
public String toString() {
- return "TmfLocationArray [locations=" + Arrays.toString(fLocations) + "]";
+ StringBuilder sb = new StringBuilder();
+ sb.append(getClass().getSimpleName() + " [");
+ for (int i = 0; i < fLocations.length; i++) {
+ if (i > 0) {
+ sb.append(", ");
+ }
+ sb.append("[location=" + fLocations[i] + ",rank=" + fRanks[i] + "]");
+ }
+ sb.append("]");
+ return sb.toString();
}
}