Support for multiple events for same timestamp at checkpoint
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfCheckpoint.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Trace Model 1.0
12 ******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.trace;
15
16 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
17
18 /**
19 * A basic implementation of ITmfCheckpoint. It simply maps an event timestamp
20 * to a generic location.
21 *
22 * @version 1.0
23 * @author Francois Chouinard
24 *
25 * @see ITmfLocation
26 * @see ITmfTimestamp
27 */
28 public class TmfCheckpoint implements ITmfCheckpoint, Cloneable {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33
34 // The checkpoint context
35 private ITmfContext fContext;
36
37 // The checkpoint timestamp
38 private ITmfTimestamp fTimestamp;
39
40 // ------------------------------------------------------------------------
41 // Constructors
42 // ------------------------------------------------------------------------
43
44 /**
45 * Default constructor
46 */
47 @SuppressWarnings("unused")
48 private TmfCheckpoint() {
49 }
50
51 /**
52 * Full constructor
53 *
54 * @param timestamp the checkpoint timestamp
55 * @param context the corresponding trace location
56 */
57 public TmfCheckpoint(final ITmfTimestamp timestamp, final ITmfContext context) {
58 fTimestamp = timestamp;
59 fContext = context;
60 }
61
62 /**
63 * Copy constructor
64 *
65 * @param other the other checkpoint
66 */
67 public TmfCheckpoint(final TmfCheckpoint other) {
68 if (other == null) {
69 throw new IllegalArgumentException();
70 }
71 fTimestamp = other.fTimestamp;
72 fContext = other.fContext;
73 }
74
75 // ------------------------------------------------------------------------
76 // Cloneable
77 // ------------------------------------------------------------------------
78
79 /* (non-Javadoc)
80 * @see java.lang.Object#clone()
81 */
82 @Override
83 public TmfCheckpoint clone() {
84 TmfCheckpoint clone = null;
85 try {
86 clone = (TmfCheckpoint) super.clone();
87 clone.fContext = (fContext != null) ? fContext.clone() : null;
88 clone.fTimestamp = fTimestamp;
89 } catch (final CloneNotSupportedException e) {
90 }
91 return clone;
92 }
93
94 // ------------------------------------------------------------------------
95 // ITmfCheckpoint
96 // ------------------------------------------------------------------------
97
98 /* (non-Javadoc)
99 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint#getTimestamp()
100 */
101 @Override
102 public ITmfTimestamp getTimestamp() {
103 return fTimestamp;
104 }
105
106 /* (non-Javadoc)
107 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint#getLocation()
108 */
109 @Override
110 public ITmfContext getContext() {
111 return fContext;
112 }
113
114 /* (non-Javadoc)
115 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint#getLocation()
116 */
117 @Override
118 public ITmfLocation getLocation() {
119 return fContext.getLocation();
120 }
121
122 // ------------------------------------------------------------------------
123 // Comparable
124 // ------------------------------------------------------------------------
125
126 /* (non-Javadoc)
127 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint#compareTo(org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint)
128 *
129 * Compares the checkpoints timestamp. If either is null, compares the
130 * trace checkpoints locations.
131 */
132 @Override
133 @SuppressWarnings({ "unchecked", "rawtypes" })
134 public int compareTo(final ITmfCheckpoint other) {
135 int comp = 0;
136 if ((fTimestamp != null) && (other.getTimestamp() != null)) {
137 comp = fTimestamp.compareTo(other.getTimestamp(), false);
138 if (comp != 0) {
139 return comp;
140 }
141 // compare locations if timestamps are the same
142 }
143
144 if ((getContext() == null) && (other.getContext() == null)) {
145 return 0;
146 }
147
148 // treat location of other as null location which is before any location
149 if ((getContext() != null) && (other.getContext() == null)) {
150 return 1;
151 }
152
153 // treat this as null location which is before any other locations
154 if ((getContext() == null) && (other.getContext() != null)) {
155 return -1;
156 }
157
158 // compare location
159 final Comparable location1 = getLocation().getLocationInfo();
160 final Comparable location2 = other.getLocation().getLocationInfo();
161 return location1.compareTo(location2);
162 }
163
164 // ------------------------------------------------------------------------
165 // Object
166 // ------------------------------------------------------------------------
167
168 /* (non-Javadoc)
169 * @see java.lang.Object#hashCode()
170 */
171 @Override
172 public int hashCode() {
173 final int prime = 31;
174 int result = 1;
175 result = prime * result + ((fContext == null) ? 0 : fContext.hashCode());
176 result = prime * result + ((fTimestamp == null) ? 0 : fTimestamp.hashCode());
177 return result;
178 }
179
180 /* (non-Javadoc)
181 * @see java.lang.Object#equals(java.lang.Object)
182 */
183 @Override
184 public boolean equals(final Object obj) {
185 if (this == obj) {
186 return true;
187 }
188 if (obj == null) {
189 return false;
190 }
191 if (!(obj instanceof TmfCheckpoint)) {
192 return false;
193 }
194 final TmfCheckpoint other = (TmfCheckpoint) obj;
195 if (fContext == null) {
196 if (other.fContext != null) {
197 return false;
198 }
199 } else if (!fContext.equals(other.fContext)) {
200 return false;
201 }
202 if (fTimestamp == null) {
203 if (other.fTimestamp != null) {
204 return false;
205 }
206 } else if (!fTimestamp.equals(other.fTimestamp)) {
207 return false;
208 }
209 return true;
210 }
211
212 /* (non-Javadoc)
213 * @see java.lang.Object#toString()
214 */
215 @Override
216 @SuppressWarnings("nls")
217 public String toString() {
218 return "TmfCheckpoint [fContext=" + fContext + ", fTimestamp=" + fTimestamp + "]";
219 }
220
221 }
This page took 0.036835 seconds and 5 git commands to generate.