Commit | Line | Data |
---|---|---|
a79913eb | 1 | /******************************************************************************* |
0316808c | 2 | * Copyright (c) 2011, 2012 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 | |
a79913eb FC |
12 | *******************************************************************************/ |
13 | ||
9e0640dc | 14 | package org.eclipse.linuxtools.internal.tmf.core.trace; |
a79913eb | 15 | |
478e04a4 PT |
16 | import java.util.Arrays; |
17 | ||
5cc97265 FC |
18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
19 | ||
0316808c FC |
20 | /** |
21 | * A convenience class to store trace location arrays. The main purpose is to | |
22 | * provide a Comparable implementation for TmfExperimentLocation. | |
9b749023 | 23 | * |
0316808c FC |
24 | * @version 1.0 |
25 | * @author Patrick Tasse | |
26 | */ | |
a79913eb | 27 | public class TmfLocationArray implements Comparable<TmfLocationArray>, Cloneable { |
0316808c FC |
28 | |
29 | // ------------------------------------------------------------------------ | |
30 | // Attributes | |
31 | // ------------------------------------------------------------------------ | |
32 | ||
9b749023 | 33 | private final ITmfLocation<? extends Comparable<?>>[] fLocations; |
0316808c FC |
34 | |
35 | // ------------------------------------------------------------------------ | |
36 | // Constructors | |
37 | // ------------------------------------------------------------------------ | |
38 | ||
39 | /** | |
40 | * The standard constructor | |
9b749023 | 41 | * |
0316808c FC |
42 | * @param locations the locations |
43 | */ | |
5cc97265 | 44 | public TmfLocationArray(ITmfLocation<? extends Comparable<?>>[] locations) { |
0316808c FC |
45 | fLocations = locations; |
46 | } | |
47 | ||
48 | // ------------------------------------------------------------------------ | |
49 | // Getters | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
52 | /** | |
063f0d27 | 53 | * Get the locations inside this array |
9b749023 | 54 | * |
063f0d27 | 55 | * @return the locations |
0316808c | 56 | */ |
5cc97265 | 57 | public ITmfLocation<? extends Comparable<?>>[] getLocations() { |
0316808c FC |
58 | return fLocations; |
59 | } | |
60 | ||
61 | // ------------------------------------------------------------------------ | |
62 | // Cloneable | |
63 | // ------------------------------------------------------------------------ | |
64 | ||
65 | /* (non-Javadoc) | |
66 | * @see java.lang.Object#clone() | |
67 | */ | |
68 | @Override | |
69 | public TmfLocationArray clone() { | |
9b749023 | 70 | ITmfLocation<? extends Comparable<?>>[] clones = new ITmfLocation<?>[fLocations.length]; |
0316808c | 71 | for (int i = 0; i < fLocations.length; i++) { |
5cc97265 | 72 | ITmfLocation<?> location = fLocations[i]; |
1ac2de5c | 73 | clones[i] = (location != null) ? location.clone() : null; |
0316808c FC |
74 | } |
75 | return new TmfLocationArray(clones); | |
76 | } | |
77 | ||
78 | // ------------------------------------------------------------------------ | |
79 | // Comparable | |
80 | // ------------------------------------------------------------------------ | |
478e04a4 | 81 | |
0316808c | 82 | @Override |
063f0d27 | 83 | @SuppressWarnings("rawtypes") |
0316808c FC |
84 | public int compareTo(TmfLocationArray o) { |
85 | for (int i = 0; i < fLocations.length; i++) { | |
6bab4511 AM |
86 | ITmfLocation<? extends Comparable> l1 = (ITmfLocation<? extends Comparable>) fLocations[i].getLocationData(); |
87 | ITmfLocation<? extends Comparable> l2 = (ITmfLocation<? extends Comparable>) o.fLocations[i].getLocationData(); | |
88 | int result = l1.getLocationData().compareTo(l2.getLocationData()); | |
0316808c FC |
89 | if (result != 0) { |
90 | return result; | |
91 | } | |
92 | } | |
93 | return 0; | |
94 | } | |
95 | ||
96 | // ------------------------------------------------------------------------ | |
97 | // Object | |
98 | // ------------------------------------------------------------------------ | |
99 | ||
100 | /* (non-Javadoc) | |
101 | * @see java.lang.Object#hashCode() | |
102 | */ | |
478e04a4 PT |
103 | @Override |
104 | public int hashCode() { | |
105 | final int prime = 31; | |
106 | int result = 1; | |
0316808c | 107 | result = prime * result + Arrays.hashCode(fLocations); |
478e04a4 PT |
108 | return result; |
109 | } | |
110 | ||
0316808c FC |
111 | /* (non-Javadoc) |
112 | * @see java.lang.Object#equals(java.lang.Object) | |
113 | */ | |
478e04a4 PT |
114 | @Override |
115 | public boolean equals(Object obj) { | |
0316808c | 116 | if (this == obj) { |
478e04a4 | 117 | return true; |
0316808c FC |
118 | } |
119 | if (obj == null) { | |
478e04a4 | 120 | return false; |
0316808c FC |
121 | } |
122 | if (getClass() != obj.getClass()) { | |
478e04a4 | 123 | return false; |
0316808c | 124 | } |
478e04a4 | 125 | TmfLocationArray other = (TmfLocationArray) obj; |
0316808c | 126 | if (!Arrays.equals(fLocations, other.fLocations)) { |
478e04a4 | 127 | return false; |
0316808c | 128 | } |
478e04a4 PT |
129 | return true; |
130 | } | |
131 | ||
0316808c FC |
132 | /* (non-Javadoc) |
133 | * @see java.lang.Object#toString() | |
134 | */ | |
135 | @Override | |
136 | @SuppressWarnings("nls") | |
137 | public String toString() { | |
138 | return "TmfLocationArray [locations=" + Arrays.toString(fLocations) + "]"; | |
139 | } | |
a79913eb | 140 | |
0316808c | 141 | } |