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 | ||
d62bb185 | 20 | |
0316808c FC |
21 | /** |
22 | * A convenience class to store trace location arrays. The main purpose is to | |
23 | * provide a Comparable implementation for TmfExperimentLocation. | |
9b749023 | 24 | * |
0316808c FC |
25 | * @version 1.0 |
26 | * @author Patrick Tasse | |
27 | */ | |
d62bb185 | 28 | public final class TmfLocationArray implements Comparable<TmfLocationArray> { |
0316808c FC |
29 | |
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
1e1bef82 | 34 | private final ITmfLocation[] fLocations; |
0316808c FC |
35 | |
36 | // ------------------------------------------------------------------------ | |
37 | // Constructors | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
40 | /** | |
41 | * The standard constructor | |
9b749023 | 42 | * |
0316808c FC |
43 | * @param locations the locations |
44 | */ | |
1e1bef82 | 45 | public TmfLocationArray(ITmfLocation[] locations) { |
0316808c FC |
46 | fLocations = locations; |
47 | } | |
48 | ||
0316808c | 49 | /** |
d62bb185 FC |
50 | * The "update" constructor. Copies the array of locations and updates |
51 | * a single entry. | |
9b749023 | 52 | * |
d62bb185 FC |
53 | * @param locations the locations |
54 | * @param index the entry to modify | |
55 | * @param location the new entry | |
0316808c | 56 | */ |
d62bb185 FC |
57 | public TmfLocationArray(TmfLocationArray locations, int index, ITmfLocation location) { |
58 | assert(locations != null && index >= 0 && index < locations.fLocations.length); | |
59 | fLocations = Arrays.copyOf(locations.fLocations, locations.fLocations.length); | |
60 | fLocations[index] = location; | |
0316808c FC |
61 | } |
62 | ||
63 | // ------------------------------------------------------------------------ | |
d62bb185 | 64 | // Getters |
0316808c FC |
65 | // ------------------------------------------------------------------------ |
66 | ||
d62bb185 FC |
67 | /** |
68 | * Get a specific location | |
69 | * | |
70 | * @param index the location element | |
71 | * | |
72 | * @return the specific location (possibly null) | |
0316808c | 73 | */ |
d62bb185 FC |
74 | public ITmfLocation getLocation(int index) { |
75 | if (fLocations != null && index >= 0 && index < fLocations.length) { | |
76 | return fLocations[index]; | |
0316808c | 77 | } |
d62bb185 | 78 | return null; |
0316808c FC |
79 | } |
80 | ||
81 | // ------------------------------------------------------------------------ | |
82 | // Comparable | |
83 | // ------------------------------------------------------------------------ | |
478e04a4 | 84 | |
0316808c | 85 | @Override |
1e1bef82 | 86 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
0316808c FC |
87 | public int compareTo(TmfLocationArray o) { |
88 | for (int i = 0; i < fLocations.length; i++) { | |
5976d44a FC |
89 | Comparable l1 = fLocations[i].getLocationInfo(); |
90 | Comparable l2 = o.fLocations[i].getLocationInfo(); | |
1e1bef82 | 91 | int result = l1.compareTo(l2); |
0316808c FC |
92 | if (result != 0) { |
93 | return result; | |
94 | } | |
95 | } | |
96 | return 0; | |
97 | } | |
98 | ||
99 | // ------------------------------------------------------------------------ | |
100 | // Object | |
101 | // ------------------------------------------------------------------------ | |
102 | ||
103 | /* (non-Javadoc) | |
104 | * @see java.lang.Object#hashCode() | |
105 | */ | |
478e04a4 PT |
106 | @Override |
107 | public int hashCode() { | |
108 | final int prime = 31; | |
109 | int result = 1; | |
0316808c | 110 | result = prime * result + Arrays.hashCode(fLocations); |
478e04a4 PT |
111 | return result; |
112 | } | |
113 | ||
0316808c FC |
114 | /* (non-Javadoc) |
115 | * @see java.lang.Object#equals(java.lang.Object) | |
116 | */ | |
478e04a4 PT |
117 | @Override |
118 | public boolean equals(Object obj) { | |
0316808c | 119 | if (this == obj) { |
478e04a4 | 120 | return true; |
0316808c FC |
121 | } |
122 | if (obj == null) { | |
478e04a4 | 123 | return false; |
0316808c FC |
124 | } |
125 | if (getClass() != obj.getClass()) { | |
478e04a4 | 126 | return false; |
0316808c | 127 | } |
478e04a4 | 128 | TmfLocationArray other = (TmfLocationArray) obj; |
0316808c | 129 | if (!Arrays.equals(fLocations, other.fLocations)) { |
478e04a4 | 130 | return false; |
0316808c | 131 | } |
478e04a4 PT |
132 | return true; |
133 | } | |
134 | ||
0316808c FC |
135 | /* (non-Javadoc) |
136 | * @see java.lang.Object#toString() | |
137 | */ | |
138 | @Override | |
139 | @SuppressWarnings("nls") | |
140 | public String toString() { | |
141 | return "TmfLocationArray [locations=" + Arrays.toString(fLocations) + "]"; | |
142 | } | |
a79913eb | 143 | |
0316808c | 144 | } |