Refactor TmfCheckpoint and dependencies (+JUnits)
[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 * <b><u>TmfCheckpoint</u></b>
20 * <p>
21 * This class maps an event timestamp to a generic location.
22 */
23 public class TmfCheckpoint implements ITmfCheckpoint {
24
25 // ------------------------------------------------------------------------
26 // Attributes
27 // ------------------------------------------------------------------------
28
29 // The checkpoint location
30 private ITmfLocation<? extends Comparable<?>> fLocation;
31
32 // The checkpoint timestamp
33 private ITmfTimestamp fTimestamp;
34
35 // ------------------------------------------------------------------------
36 // Constructors
37 // ------------------------------------------------------------------------
38
39 /**
40 * Default constructor
41 */
42 @SuppressWarnings("unused")
43 private TmfCheckpoint() {
44 }
45
46 /**
47 * Full constructor
48 *
49 * @param timestamp the checkpoint timestamp
50 * @param location the corresponding trace location
51 */
52 public TmfCheckpoint(final ITmfTimestamp timestamp, final ITmfLocation<? extends Comparable<?>> location) {
53 fTimestamp = timestamp;
54 fLocation = location;
55 }
56
57 /**
58 * Copy constructor
59 *
60 * @param other the other checkpoint
61 */
62 public TmfCheckpoint(final TmfCheckpoint other) {
63 if (other == null)
64 throw new IllegalArgumentException();
65 fTimestamp = other.fTimestamp;
66 fLocation = other.fLocation;
67 }
68
69 // ------------------------------------------------------------------------
70 // Cloneable
71 // ------------------------------------------------------------------------
72
73 /* (non-Javadoc)
74 * @see java.lang.Object#clone()
75 */
76 @Override
77 public TmfCheckpoint clone() {
78 TmfCheckpoint clone = null;
79 try {
80 clone = (TmfCheckpoint) super.clone();
81 clone.fLocation = (fLocation != null) ? fLocation.clone() : null;
82 clone.fTimestamp = (fTimestamp != null) ? fTimestamp.clone() : null;
83 } catch (final CloneNotSupportedException e) {
84 }
85 return clone;
86 }
87
88 // ------------------------------------------------------------------------
89 // ITmfCheckpoint
90 // ------------------------------------------------------------------------
91
92 /* (non-Javadoc)
93 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint#getTimestamp()
94 */
95 @Override
96 public ITmfTimestamp getTimestamp() {
97 return fTimestamp;
98 }
99
100 /* (non-Javadoc)
101 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint#getLocation()
102 */
103 @Override
104 public ITmfLocation<?> getLocation() {
105 return fLocation;
106 }
107
108 // ------------------------------------------------------------------------
109 // Comparable
110 // ------------------------------------------------------------------------
111
112 /* (non-Javadoc)
113 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint#compareTo(org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint)
114 *
115 * Compares the checkpoints timestamp. If either is null, compares the
116 * trace checkpoints locations.
117 */
118 @Override
119 @SuppressWarnings({ "unchecked", "rawtypes" })
120 public int compareTo(final ITmfCheckpoint other) {
121 if (fTimestamp == null || other.getTimestamp() == null) {
122 final Comparable location1 = fLocation.getLocation();
123 final Comparable location2 = other.getLocation().getLocation();
124 return location1.compareTo(location2);
125 }
126 return fTimestamp.compareTo(other.getTimestamp(), false);
127 }
128
129 // ------------------------------------------------------------------------
130 // Object
131 // ------------------------------------------------------------------------
132
133 /* (non-Javadoc)
134 * @see java.lang.Object#hashCode()
135 */
136 @Override
137 public int hashCode() {
138 final int prime = 31;
139 int result = 1;
140 result = prime * result + ((fLocation == null) ? 0 : fLocation.hashCode());
141 result = prime * result + ((fTimestamp == null) ? 0 : fTimestamp.hashCode());
142 return result;
143 }
144
145 /* (non-Javadoc)
146 * @see java.lang.Object#equals(java.lang.Object)
147 */
148 @Override
149 public boolean equals(final Object obj) {
150 if (this == obj)
151 return true;
152 if (obj == null)
153 return false;
154 if (!(obj instanceof TmfCheckpoint))
155 return false;
156 final TmfCheckpoint other = (TmfCheckpoint) obj;
157 if (fLocation == null) {
158 if (other.fLocation != null)
159 return false;
160 } else if (!fLocation.equals(other.fLocation))
161 return false;
162 if (fTimestamp == null) {
163 if (other.fTimestamp != null)
164 return false;
165 } else if (!fTimestamp.equals(other.fTimestamp))
166 return false;
167 return true;
168 }
169
170 /* (non-Javadoc)
171 * @see java.lang.Object#toString()
172 */
173 @Override
174 @SuppressWarnings("nls")
175 public String toString() {
176 return "TmfCheckpoint [fLocation=" + fLocation + ", fTimestamp=" + fTimestamp + "]";
177 }
178
179 }
This page took 0.033259 seconds and 5 git commands to generate.