tmf: Make TmfEventField's equals() also check the sub-fields
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / model / TimeGraphEntry.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Move code to provide base classes for time graph view
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.concurrent.CopyOnWriteArrayList;
20
21 /**
22 * An entry for use in the time graph views
23 *
24 * @since 2.1
25 */
26 public class TimeGraphEntry implements ITimeGraphEntry {
27
28 /** Entry's parent */
29 private TimeGraphEntry fParent = null;
30
31 /** List of child entries */
32 private final List<TimeGraphEntry> fChildren = new CopyOnWriteArrayList<>();
33
34 /** Name of this entry (text to show) */
35 private String fName;
36 private long fStartTime = -1;
37 private long fEndTime = -1;
38 private List<ITimeEvent> fEventList = new ArrayList<>();
39 private List<ITimeEvent> fZoomedEventList = new ArrayList<>();
40
41 /**
42 * Constructor
43 *
44 * @param name
45 * The name of this entry
46 * @param startTime
47 * The start time of this entry
48 * @param endTime
49 * The end time of this entry
50 */
51 public TimeGraphEntry(String name, long startTime, long endTime) {
52 fName = name;
53 fStartTime = startTime;
54 fEndTime = endTime;
55 }
56
57 // ---------------------------------------------
58 // Getters and setters
59 // ---------------------------------------------
60
61 @Override
62 public ITimeGraphEntry getParent() {
63 return fParent;
64 }
65
66 /**
67 * Sets the entry's parent
68 *
69 * @param entry The new parent entry
70 */
71 protected void setParent(TimeGraphEntry entry) {
72 fParent = entry;
73 }
74
75 @Override
76 public boolean hasChildren() {
77 return fChildren.size() > 0;
78 }
79
80 @Override
81 public List<TimeGraphEntry> getChildren() {
82 return fChildren;
83 }
84
85 @Override
86 public String getName() {
87 return fName;
88 }
89
90 /**
91 * Update the entry name
92 *
93 * @param name
94 * the updated entry name
95 */
96 public void setName(String name) {
97 fName = name;
98 }
99
100 @Override
101 public long getStartTime() {
102 return fStartTime;
103 }
104
105 @Override
106 public long getEndTime() {
107 return fEndTime;
108 }
109
110 /**
111 * Updates the end time
112 *
113 * @param endTime
114 * the end time
115 *
116 * @since 3.0
117 */
118 public void updateEndTime(long endTime) {
119 fEndTime = Math.max(endTime, fEndTime);
120 }
121
122 @Override
123 public boolean hasTimeEvents() {
124 return true;
125 }
126
127 @Override
128 public Iterator<ITimeEvent> getTimeEventsIterator() {
129 if (hasTimeEvents()) {
130 return new EventIterator(fEventList, fZoomedEventList);
131 }
132 return null;
133 }
134
135 @Override
136 public Iterator<ITimeEvent> getTimeEventsIterator(long startTime, long stopTime, long visibleDuration) {
137 if (!hasTimeEvents()) {
138 return null;
139 }
140 return new EventIterator(fEventList, fZoomedEventList, startTime, stopTime);
141 }
142
143 /**
144 * Add an event to this entry's event list. If necessary, update the start
145 * and end time of the entry. If the event list's last event starts at the
146 * same time as the event to add, it is replaced by the new event.
147 *
148 * @param event
149 * The time event to add
150 */
151 public void addEvent(ITimeEvent event) {
152 long start = event.getTime();
153 long end = start + event.getDuration();
154 synchronized (fEventList) {
155 int lastIndex = fEventList.size() - 1;
156 if (lastIndex >= 0 && fEventList.get(lastIndex).getTime() == event.getTime()) {
157 fEventList.set(lastIndex, event);
158 } else {
159 fEventList.add(event);
160 }
161 if (fStartTime == -1 || start < fStartTime) {
162 fStartTime = start;
163 }
164 if (fEndTime == -1 || end > fEndTime) {
165 fEndTime = end;
166 }
167 }
168 }
169
170 /**
171 * Set the general event list of this entry.
172 *
173 * @param eventList
174 * The list of time events
175 */
176 public void setEventList(List<ITimeEvent> eventList) {
177 if (eventList != null) {
178 fEventList = new ArrayList<>(eventList);
179 } else {
180 fEventList = new ArrayList<>();
181 }
182 }
183
184 /**
185 * Set the zoomed event list of this entry.
186 *
187 * @param eventList
188 * The list of time events
189 */
190 public void setZoomedEventList(List<ITimeEvent> eventList) {
191 if (eventList != null) {
192 fZoomedEventList = new ArrayList<>(eventList);
193 } else {
194 fZoomedEventList = new ArrayList<>();
195 }
196 }
197
198 /**
199 * Add a child entry to this one
200 *
201 * @param child
202 * The child entry
203 */
204 public void addChild(TimeGraphEntry child) {
205 child.fParent = this;
206 fChildren.add(child);
207 }
208
209 @Override
210 public String toString() {
211 return getClass().getSimpleName() + '(' + fName + ')';
212 }
213
214 }
This page took 0.037068 seconds and 6 git commands to generate.