Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2012, 2014 Ericsson |
be222f56 PT |
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 | |
4999a196 | 11 | * Geneviève Bastien - Added the fValue parameter to avoid subclassing |
be222f56 PT |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model; |
be222f56 | 15 | |
6f8332c4 AM |
16 | import java.util.Objects; |
17 | ||
be222f56 PT |
18 | /** |
19 | * Generic TimeEvent implementation | |
20 | * | |
21 | * @version 1.0 | |
22 | * @author Patrick Tasse | |
23 | */ | |
2e7d9caa | 24 | public class TimeEvent implements ITimeEvent { |
a0a88f65 AM |
25 | |
26 | /** TimeGraphEntry matching this time event */ | |
be222f56 | 27 | protected ITimeGraphEntry fEntry; |
a0a88f65 AM |
28 | |
29 | /** Beginning timestamp of this time event */ | |
be222f56 | 30 | protected long fTime; |
a0a88f65 AM |
31 | |
32 | /** Duration of this time event */ | |
be222f56 PT |
33 | protected long fDuration; |
34 | ||
4999a196 GB |
35 | private final int fValue; |
36 | ||
37 | /** | |
38 | * Default value when no other value present | |
39 | */ | |
40 | private static final int NOVALUE = Integer.MIN_VALUE; | |
41 | ||
be222f56 PT |
42 | /** |
43 | * Standard constructor | |
44 | * | |
45 | * @param entry | |
46 | * The entry matching this event | |
47 | * @param time | |
48 | * The timestamp of this event | |
49 | * @param duration | |
50 | * The duration of the event | |
51 | */ | |
52 | public TimeEvent(ITimeGraphEntry entry, long time, long duration) { | |
4999a196 GB |
53 | this(entry, time, duration, NOVALUE); |
54 | ||
55 | } | |
56 | ||
57 | /** | |
58 | * Constructor | |
59 | * | |
60 | * @param entry | |
61 | * The entry to which this time event is assigned | |
62 | * @param time | |
63 | * The timestamp of this event | |
64 | * @param duration | |
65 | * The duration of this event | |
66 | * @param value | |
67 | * The status assigned to the event | |
4999a196 GB |
68 | */ |
69 | public TimeEvent(ITimeGraphEntry entry, long time, long duration, | |
70 | int value) { | |
be222f56 PT |
71 | fEntry = entry; |
72 | fTime = time; | |
73 | fDuration = duration; | |
4999a196 GB |
74 | fValue = value; |
75 | } | |
76 | ||
77 | /** | |
78 | * Get this event's status | |
79 | * | |
80 | * @return The integer matching this status | |
4999a196 GB |
81 | */ |
82 | public int getValue() { | |
83 | return fValue; | |
84 | } | |
85 | ||
86 | /** | |
87 | * Return whether an event has a value | |
88 | * | |
89 | * @return true if the event has a value | |
4999a196 GB |
90 | */ |
91 | public boolean hasValue() { | |
92 | return (fValue != NOVALUE); | |
be222f56 PT |
93 | } |
94 | ||
95 | @Override | |
96 | public ITimeGraphEntry getEntry() { | |
97 | return fEntry; | |
98 | } | |
99 | ||
100 | @Override | |
101 | public long getTime() { | |
102 | return fTime; | |
103 | } | |
104 | ||
105 | @Override | |
106 | public long getDuration() { | |
107 | return fDuration; | |
108 | } | |
ad128fd8 | 109 | |
1d46dc38 | 110 | @Override |
2e7d9caa AM |
111 | public ITimeEvent splitBefore(long splitTime) { |
112 | return (splitTime > fTime ? | |
113 | new TimeEvent(fEntry, fTime, Math.min(fDuration, splitTime - fTime), fValue) : | |
114 | null); | |
115 | } | |
116 | ||
117 | @Override | |
118 | public ITimeEvent splitAfter(long splitTime) { | |
119 | return (splitTime < fTime + fDuration ? | |
120 | new TimeEvent(fEntry, Math.max(fTime, splitTime), fDuration - Math.max(0, splitTime - fTime), | |
121 | fValue) : | |
122 | null); | |
1d46dc38 PT |
123 | } |
124 | ||
6f8332c4 AM |
125 | @Override |
126 | public int hashCode() { | |
127 | return Objects.hash(fEntry, fTime, fDuration, fValue); | |
128 | } | |
129 | ||
130 | @Override | |
131 | public boolean equals(Object obj) { | |
132 | if (this == obj) { | |
133 | return true; | |
134 | } | |
135 | if (obj == null) { | |
136 | return false; | |
137 | } | |
138 | if (getClass() != obj.getClass()) { | |
139 | return false; | |
140 | } | |
141 | TimeEvent other = (TimeEvent) obj; | |
142 | return Objects.equals(fEntry, other.fEntry) && | |
143 | Objects.equals(fTime, other.fTime) && | |
144 | Objects.equals(fDuration, other.fDuration) && | |
145 | Objects.equals(fValue, other.fValue); | |
146 | } | |
147 | ||
ad128fd8 PT |
148 | @Override |
149 | public String toString() { | |
b1b156f3 | 150 | return getClass().getSimpleName() + " start=" + fTime + " end=" + (fTime + fDuration) + " duration=" + fDuration + (hasValue() ? (" value=" + fValue) : ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ |
ad128fd8 | 151 | } |
be222f56 | 152 | } |