Make the TmfLocation final and get rid of clone()
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / trace / TmfLocationArray.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 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 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.tmf.core.trace;
15
16 import java.util.Arrays;
17
18 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
19
20
21 /**
22 * A convenience class to store trace location arrays. The main purpose is to
23 * provide a Comparable implementation for TmfExperimentLocation.
24 *
25 * @version 1.0
26 * @author Patrick Tasse
27 */
28 public final class TmfLocationArray implements Comparable<TmfLocationArray> {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33
34 private final ITmfLocation[] fLocations;
35
36 // ------------------------------------------------------------------------
37 // Constructors
38 // ------------------------------------------------------------------------
39
40 /**
41 * The standard constructor
42 *
43 * @param locations the locations
44 */
45 public TmfLocationArray(ITmfLocation[] locations) {
46 fLocations = locations;
47 }
48
49 /**
50 * The "update" constructor. Copies the array of locations and updates
51 * a single entry.
52 *
53 * @param locations the locations
54 * @param index the entry to modify
55 * @param location the new entry
56 */
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;
61 }
62
63 // ------------------------------------------------------------------------
64 // Getters
65 // ------------------------------------------------------------------------
66
67 /**
68 * Get a specific location
69 *
70 * @param index the location element
71 *
72 * @return the specific location (possibly null)
73 */
74 public ITmfLocation getLocation(int index) {
75 if (fLocations != null && index >= 0 && index < fLocations.length) {
76 return fLocations[index];
77 }
78 return null;
79 }
80
81 // ------------------------------------------------------------------------
82 // Comparable
83 // ------------------------------------------------------------------------
84
85 @Override
86 @SuppressWarnings({ "unchecked", "rawtypes" })
87 public int compareTo(TmfLocationArray o) {
88 for (int i = 0; i < fLocations.length; i++) {
89 Comparable l1 = fLocations[i].getLocationInfo();
90 Comparable l2 = o.fLocations[i].getLocationInfo();
91 int result = l1.compareTo(l2);
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 */
106 @Override
107 public int hashCode() {
108 final int prime = 31;
109 int result = 1;
110 result = prime * result + Arrays.hashCode(fLocations);
111 return result;
112 }
113
114 /* (non-Javadoc)
115 * @see java.lang.Object#equals(java.lang.Object)
116 */
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj) {
120 return true;
121 }
122 if (obj == null) {
123 return false;
124 }
125 if (getClass() != obj.getClass()) {
126 return false;
127 }
128 TmfLocationArray other = (TmfLocationArray) obj;
129 if (!Arrays.equals(fLocations, other.fLocations)) {
130 return false;
131 }
132 return true;
133 }
134
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 }
143
144 }
This page took 0.035007 seconds and 5 git commands to generate.