Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2009, 2014 Ericsson |
1e1bef82 | 3 | * |
8c8bf09f ASL |
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 | |
1e1bef82 | 8 | * |
8c8bf09f ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
cbdacf03 | 11 | * Francois Chouinard - Updated as per TMF Trace Model 1.0 |
ea271da6 | 12 | * Patrick Tasse - Updated for removal of context clone |
8c8bf09f ASL |
13 | *******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.core.trace; |
8c8bf09f | 16 | |
38db0431 MK |
17 | import java.util.Objects; |
18 | ||
19 | import org.eclipse.jdt.annotation.Nullable; | |
2bdf0193 | 20 | import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation; |
a3db8436 | 21 | |
8c8bf09f | 22 | /** |
2848c377 FC |
23 | * A basic implementation of ITmfContext. |
24 | * <p> | |
25 | * It ties a trace location to an event rank. The context should be enough to | |
26 | * restore the trace state so the corresponding event can be read. | |
1e1bef82 | 27 | * |
f7703ed6 FC |
28 | * @author Francois Chouinard |
29 | * | |
f7703ed6 | 30 | * @see ITmfLocation |
8c8bf09f | 31 | */ |
81a2d02e | 32 | public class TmfContext implements ITmfContext { |
8c8bf09f | 33 | |
cbdacf03 FC |
34 | // ------------------------------------------------------------------------ |
35 | // Attributes | |
36 | // ------------------------------------------------------------------------ | |
37 | ||
38 | // The trace location | |
38db0431 | 39 | private @Nullable ITmfLocation fLocation; |
cbdacf03 FC |
40 | |
41 | // The event rank | |
948b0607 FC |
42 | private long fRank; |
43 | ||
44 | // ------------------------------------------------------------------------ | |
45 | // Constructors | |
46 | // ------------------------------------------------------------------------ | |
47 | ||
cbdacf03 FC |
48 | /** |
49 | * Default constructor | |
50 | */ | |
51 | public TmfContext() { | |
52 | this(null, UNKNOWN_RANK); | |
948b0607 FC |
53 | } |
54 | ||
cbdacf03 FC |
55 | /** |
56 | * Simple constructor (unknown rank) | |
1e1bef82 | 57 | * |
cbdacf03 FC |
58 | * @param location the event location |
59 | */ | |
1e1bef82 | 60 | public TmfContext(final ITmfLocation location) { |
948b0607 FC |
61 | this(location, UNKNOWN_RANK); |
62 | } | |
63 | ||
cbdacf03 FC |
64 | /** |
65 | * Full constructor | |
1e1bef82 | 66 | * |
cbdacf03 FC |
67 | * @param location the event location |
68 | * @param rank the event rank | |
69 | */ | |
1e1bef82 | 70 | public TmfContext(final ITmfLocation location, final long rank) { |
cbdacf03 FC |
71 | fLocation = location; |
72 | fRank = rank; | |
948b0607 FC |
73 | } |
74 | ||
cbdacf03 FC |
75 | /** |
76 | * Copy constructor | |
1e1bef82 | 77 | * |
cbdacf03 FC |
78 | * @param context the other context |
79 | */ | |
5d837f9b | 80 | public TmfContext(final TmfContext context) { |
0316808c | 81 | if (context == null) { |
5d837f9b | 82 | throw new IllegalArgumentException(); |
0316808c | 83 | } |
5d837f9b FC |
84 | fLocation = context.fLocation; |
85 | fRank = context.fRank; | |
cbdacf03 FC |
86 | } |
87 | ||
948b0607 FC |
88 | // ------------------------------------------------------------------------ |
89 | // ITmfContext | |
90 | // ------------------------------------------------------------------------ | |
91 | ||
92 | @Override | |
38db0431 | 93 | public @Nullable ITmfLocation getLocation() { |
cbdacf03 | 94 | return fLocation; |
948b0607 FC |
95 | } |
96 | ||
97 | @Override | |
1e1bef82 | 98 | public void setLocation(final ITmfLocation location) { |
948b0607 FC |
99 | fLocation = location; |
100 | } | |
101 | ||
102 | @Override | |
cbdacf03 FC |
103 | public long getRank() { |
104 | return fRank; | |
948b0607 FC |
105 | } |
106 | ||
107 | @Override | |
5d837f9b | 108 | public void setRank(final long rank) { |
948b0607 FC |
109 | fRank = rank; |
110 | } | |
111 | ||
112 | @Override | |
cbdacf03 | 113 | public void increaseRank() { |
0316808c | 114 | if (hasValidRank()) { |
cbdacf03 | 115 | fRank++; |
0316808c | 116 | } |
948b0607 FC |
117 | } |
118 | ||
119 | @Override | |
cbdacf03 FC |
120 | public boolean hasValidRank() { |
121 | return fRank != UNKNOWN_RANK; | |
948b0607 FC |
122 | } |
123 | ||
124 | @Override | |
cbdacf03 | 125 | public void dispose() { |
948b0607 FC |
126 | } |
127 | ||
128 | // ------------------------------------------------------------------------ | |
129 | // Object | |
130 | // ------------------------------------------------------------------------ | |
ff4ed569 FC |
131 | |
132 | @Override | |
133 | public int hashCode() { | |
38db0431 | 134 | return Objects.hash(fRank, fLocation); |
ff4ed569 | 135 | } |
948b0607 | 136 | |
ff4ed569 | 137 | @Override |
5d837f9b | 138 | public boolean equals(final Object obj) { |
0316808c | 139 | if (this == obj) { |
948b0607 | 140 | return true; |
0316808c FC |
141 | } |
142 | if (obj == null) { | |
948b0607 | 143 | return false; |
0316808c FC |
144 | } |
145 | if (getClass() != obj.getClass()) { | |
cbdacf03 | 146 | return false; |
0316808c | 147 | } |
5d837f9b | 148 | final TmfContext other = (TmfContext) obj; |
38db0431 | 149 | if (fRank != other.fRank) { |
cbdacf03 | 150 | return false; |
0316808c | 151 | } |
38db0431 | 152 | if (!Objects.equals(fLocation, other.fLocation)) { |
cbdacf03 | 153 | return false; |
0316808c | 154 | } |
cbdacf03 | 155 | return true; |
ff4ed569 | 156 | } |
948b0607 FC |
157 | |
158 | @Override | |
3b38ea61 | 159 | @SuppressWarnings("nls") |
ff4ed569 | 160 | public String toString() { |
cbdacf03 | 161 | return "TmfContext [fLocation=" + fLocation + ", fRank=" + fRank + "]"; |
ff4ed569 | 162 | } |
8c8bf09f ASL |
163 | |
164 | } |