Commit | Line | Data |
---|---|---|
5419a136 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2009, 2013 Ericsson |
5419a136 AM |
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 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.internal.tmf.core.request; | |
14 | ||
15 | import org.eclipse.linuxtools.internal.tmf.core.TmfCoreTracer; | |
16 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
5419a136 AM |
17 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
18 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; | |
19 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; | |
3bd46eef AM |
20 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
21 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
5419a136 AM |
22 | |
23 | /** | |
24 | * The TMF coalesced event request | |
25 | * | |
26 | * @version 1.0 | |
27 | * @author Francois Chouinard | |
28 | */ | |
29 | public class TmfCoalescedEventRequest extends TmfCoalescedDataRequest implements ITmfEventRequest { | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
cf5bad96 | 35 | private TmfTimeRange fRange; // The requested events time range |
5419a136 AM |
36 | |
37 | // ------------------------------------------------------------------------ | |
38 | // Constructor | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | /** | |
cf5bad96 AM |
42 | * Request 'n' events of a given type for the given time range (given |
43 | * priority). Events are returned in blocks of the given size. | |
5419a136 | 44 | * |
cf5bad96 AM |
45 | * @param dataType |
46 | * The requested data type | |
47 | * @param range | |
48 | * The range of the request. You can use | |
49 | * {@link TmfTimeRange#ETERNITY} to request all events. | |
50 | * @param index | |
51 | * The index of the first event to retrieve. Use '0' to start at | |
52 | * the beginning. | |
53 | * @param nbRequested | |
54 | * The number of events requested. You can use | |
55 | * {@link TmfDataRequest#ALL_DATA} to request all events. | |
cf5bad96 AM |
56 | * @param priority |
57 | * The requested execution priority | |
5419a136 | 58 | */ |
cf5bad96 AM |
59 | public TmfCoalescedEventRequest(Class<? extends ITmfEvent> dataType, |
60 | TmfTimeRange range, | |
61 | long index, | |
62 | int nbRequested, | |
cf5bad96 | 63 | ExecutionType priority) { |
672a642a | 64 | super(ITmfEvent.class, index, nbRequested, priority); |
5419a136 AM |
65 | fRange = range; |
66 | ||
67 | if (TmfCoreTracer.isRequestTraced()) { | |
68 | String type = getClass().getName(); | |
69 | type = type.substring(type.lastIndexOf('.') + 1); | |
70 | @SuppressWarnings("nls") | |
71 | String message = "CREATED " | |
72 | + (getExecType() == ITmfDataRequest.ExecutionType.BACKGROUND ? "(BG)" : "(FG)") | |
73 | + " Type=" + type + " Index=" + getIndex() + " NbReq=" + getNbRequested() | |
74 | + " Range=" + getRange() | |
75 | + " DataType=" + getDataType().getSimpleName(); | |
76 | TmfCoreTracer.traceRequest(this, message); | |
77 | } | |
78 | } | |
79 | ||
80 | // ------------------------------------------------------------------------ | |
81 | // Management | |
82 | // ------------------------------------------------------------------------ | |
83 | ||
84 | @Override | |
85 | public void addRequest(ITmfDataRequest request) { | |
86 | super.addRequest(request); | |
87 | if (request instanceof ITmfEventRequest) { | |
88 | merge((ITmfEventRequest) request); | |
89 | } | |
90 | } | |
91 | ||
cf5bad96 AM |
92 | @Override |
93 | public boolean isCompatible(ITmfDataRequest request) { | |
94 | if (request instanceof ITmfEventRequest) { | |
95 | if (super.isCompatible(request)) { | |
96 | return overlaps((ITmfEventRequest) request); | |
97 | } | |
98 | } | |
99 | return false; | |
100 | } | |
5419a136 AM |
101 | |
102 | private boolean overlaps(ITmfEventRequest request) { | |
103 | ITmfTimestamp startTime = request.getRange().getStartTime(); | |
cf5bad96 | 104 | ITmfTimestamp endTime = request.getRange().getEndTime(); |
5419a136 AM |
105 | return (startTime.compareTo(endTime) <= 0) && (fRange.getStartTime().compareTo(fRange.getEndTime()) <= 0); |
106 | } | |
107 | ||
108 | private void merge(ITmfEventRequest request) { | |
109 | ITmfTimestamp startTime = request.getRange().getStartTime(); | |
cf5bad96 | 110 | ITmfTimestamp endTime = request.getRange().getEndTime(); |
5419a136 AM |
111 | if (!fRange.contains(startTime) && fRange.getStartTime().compareTo(startTime) > 0) { |
112 | fRange = new TmfTimeRange(startTime, fRange.getEndTime()); | |
113 | } | |
114 | if (!fRange.contains(endTime) && fRange.getEndTime().compareTo(endTime) < 0) { | |
115 | fRange = new TmfTimeRange(fRange.getStartTime(), endTime); | |
116 | } | |
117 | } | |
118 | ||
119 | // ------------------------------------------------------------------------ | |
120 | // ITmfDataRequest | |
121 | // ------------------------------------------------------------------------ | |
122 | ||
123 | @Override | |
124 | public void handleData(ITmfEvent data) { | |
125 | super.handleData(data); | |
126 | long index = getIndex() + getNbRead() - 1; | |
127 | for (ITmfDataRequest request : fRequests) { | |
128 | if (data == null) { | |
129 | request.handleData(null); | |
130 | } else { | |
131 | long start = request.getIndex(); | |
132 | long end = start + request.getNbRequested(); | |
133 | if (request instanceof ITmfEventRequest) { | |
134 | ITmfEventRequest req = (ITmfEventRequest) request; | |
135 | if (!req.isCompleted() && index >= start && index < end) { | |
136 | ITmfTimestamp ts = data.getTimestamp(); | |
137 | if (req.getRange().contains(ts)) { | |
138 | if (req.getDataType().isInstance(data)) { | |
139 | req.handleData(data); | |
140 | } | |
141 | } | |
142 | } | |
143 | } | |
144 | else { | |
145 | TmfDataRequest req = (TmfDataRequest) request; | |
146 | if (!req.isCompleted() && index >= start && index < end) { | |
147 | if (req.getDataType().isInstance(data)) { | |
148 | req.handleData(data); | |
149 | } | |
150 | } | |
151 | } | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
156 | // ------------------------------------------------------------------------ | |
157 | // ITmfEventRequest | |
158 | // ------------------------------------------------------------------------ | |
159 | ||
cf5bad96 AM |
160 | @Override |
161 | public TmfTimeRange getRange() { | |
162 | return fRange; | |
163 | } | |
5419a136 | 164 | |
cf5bad96 AM |
165 | @Override |
166 | public void setStartIndex(int index) { | |
167 | setIndex(index); | |
168 | } | |
5419a136 AM |
169 | |
170 | // ------------------------------------------------------------------------ | |
171 | // Object | |
172 | // ------------------------------------------------------------------------ | |
173 | ||
174 | @Override | |
175 | // All requests have a unique id | |
176 | public int hashCode() { | |
cf5bad96 | 177 | return super.hashCode(); |
5419a136 AM |
178 | } |
179 | ||
180 | @Override | |
181 | public boolean equals(Object other) { | |
cf5bad96 AM |
182 | if (other instanceof TmfCoalescedEventRequest) { |
183 | TmfCoalescedEventRequest request = (TmfCoalescedEventRequest) other; | |
184 | return (request.getDataType() == getDataType()) && | |
185 | (request.getIndex() == getIndex()) && | |
186 | (request.getNbRequested() == getNbRequested()) && | |
187 | (request.getRange().equals(getRange())); | |
188 | } | |
189 | if (other instanceof TmfCoalescedDataRequest) { | |
190 | return super.equals(other); | |
191 | } | |
192 | return false; | |
5419a136 AM |
193 | } |
194 | ||
195 | @Override | |
196 | @SuppressWarnings("nls") | |
197 | public String toString() { | |
cf5bad96 | 198 | return "[TmfCoalescedEventRequest(" + getRequestId() + "," + getDataType().getSimpleName() |
672a642a | 199 | + "," + getRange() + "," + getIndex() + "," + getNbRequested() + ")]"; |
5419a136 AM |
200 | } |
201 | ||
202 | } |