Commit | Line | Data |
---|---|---|
8c8bf09f ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 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 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.request; | |
14 | ||
15 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
16 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
550d787e | 17 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; |
8c8bf09f ASL |
18 | |
19 | /** | |
20 | * <b><u>TmfCoalescedEventRequest</u></b> | |
21 | * <p> | |
22 | * TODO: Implement me. Please. | |
23 | */ | |
24 | public class TmfCoalescedEventRequest<T extends TmfEvent> extends TmfCoalescedDataRequest<T> implements ITmfEventRequest<T> { | |
25 | ||
26 | // ------------------------------------------------------------------------ | |
27 | // Attributes | |
28 | // ------------------------------------------------------------------------ | |
29 | ||
550d787e | 30 | private TmfTimeRange fRange; // The requested events time range |
8c8bf09f ASL |
31 | |
32 | // ------------------------------------------------------------------------ | |
33 | // Constructor | |
34 | // ------------------------------------------------------------------------ | |
35 | ||
36 | /** | |
37 | * @param range | |
38 | */ | |
39 | public TmfCoalescedEventRequest(Class<T> dataType) { | |
f6b14ce2 | 40 | this(dataType, TmfTimeRange.Eternity, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND); |
cb866e08 FC |
41 | } |
42 | ||
43 | public TmfCoalescedEventRequest(Class<T> dataType, ExecutionType execType) { | |
44 | this(dataType, TmfTimeRange.Eternity, ALL_DATA, DEFAULT_BLOCK_SIZE, execType); | |
8c8bf09f ASL |
45 | } |
46 | ||
47 | /** | |
48 | * @param range | |
49 | */ | |
50 | public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range) { | |
f6b14ce2 | 51 | this(dataType, range, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND); |
cb866e08 FC |
52 | } |
53 | ||
54 | public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, ExecutionType execType) { | |
55 | this(dataType, range, ALL_DATA, DEFAULT_BLOCK_SIZE, execType); | |
8c8bf09f ASL |
56 | } |
57 | ||
58 | /** | |
59 | * @param range | |
60 | * @param nbRequested | |
61 | */ | |
62 | public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested) { | |
f6b14ce2 | 63 | this(dataType, range, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND); |
cb866e08 FC |
64 | } |
65 | ||
66 | public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, ExecutionType execType) { | |
67 | this(dataType, range, nbRequested, DEFAULT_BLOCK_SIZE, execType); | |
8c8bf09f ASL |
68 | } |
69 | ||
70 | /** | |
71 | * @param range | |
72 | * @param nbRequested | |
73 | * @param blockSize Size of the largest blocks expected | |
74 | */ | |
75 | public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, int blockSize) { | |
f6b14ce2 | 76 | super(dataType, 0, nbRequested, blockSize, ExecutionType.FOREGROUND); |
cb866e08 FC |
77 | fRange = range; |
78 | } | |
79 | ||
80 | public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, int blockSize, ExecutionType execType) { | |
81 | super(dataType, 0, nbRequested, blockSize, execType); | |
8c8bf09f ASL |
82 | fRange = range; |
83 | } | |
84 | ||
85 | // ------------------------------------------------------------------------ | |
86 | // Management | |
87 | // ------------------------------------------------------------------------ | |
88 | ||
89 | @Override | |
2fb2eb37 FC |
90 | public boolean isCompatible(ITmfDataRequest<T> request) { |
91 | if (request instanceof ITmfEventRequest<?>) { | |
8c8bf09f ASL |
92 | boolean ok = getNbRequested() == request.getNbRequested(); |
93 | ok &= getBlockize() == request.getBlockize(); | |
9b635e61 | 94 | ok &= getExecType() == request.getExecType(); |
550d787e FC |
95 | if (ok) { |
96 | TmfTimestamp startTime = ((ITmfEventRequest<T>) request).getRange().getStartTime(); | |
97 | TmfTimestamp endTime = ((ITmfEventRequest<T>) request).getRange().getEndTime(); | |
98 | if (!fRange.contains(startTime)) | |
99 | fRange = new TmfTimeRange(startTime, fRange.getEndTime()); | |
100 | if (!fRange.contains(endTime)) | |
101 | fRange = new TmfTimeRange(fRange.getStartTime(), endTime); | |
102 | } | |
8c8bf09f ASL |
103 | return ok; |
104 | } | |
105 | return false; | |
106 | } | |
107 | ||
550d787e FC |
108 | // ------------------------------------------------------------------------ |
109 | // ITmfDataRequest | |
110 | // ------------------------------------------------------------------------ | |
111 | ||
112 | @Override | |
113 | public void handleData() { | |
114 | for (ITmfDataRequest<T> request : fRequests) { | |
115 | if (request instanceof TmfEventRequest<?>) { | |
116 | TmfEventRequest<T> req = (TmfEventRequest<T>) request; | |
9b635e61 FC |
117 | T[] data = getData(); |
118 | TmfTimestamp ts = data[0].getTimestamp(); | |
119 | if (data.length > 0 && req.getRange().contains(ts)) { | |
550d787e FC |
120 | req.setData(data); |
121 | req.handleData(); | |
122 | } | |
123 | } | |
124 | else { | |
82e04272 FC |
125 | TmfDataRequest<T> req = (TmfDataRequest<T>) request; |
126 | T[] data = getData(); | |
127 | req.setData(data); | |
128 | req.handleData(); | |
550d787e FC |
129 | } |
130 | } | |
131 | } | |
132 | ||
8c8bf09f ASL |
133 | // ------------------------------------------------------------------------ |
134 | // ITmfEventRequest | |
135 | // ------------------------------------------------------------------------ | |
136 | ||
137 | public TmfTimeRange getRange() { | |
138 | return fRange; | |
139 | } | |
140 | ||
2fb2eb37 FC |
141 | // ------------------------------------------------------------------------ |
142 | // Object | |
143 | // ------------------------------------------------------------------------ | |
144 | ||
145 | @Override | |
146 | // All requests have a unique id | |
147 | public int hashCode() { | |
148 | return super.hashCode(); | |
149 | } | |
150 | ||
151 | @Override | |
152 | public boolean equals(Object other) { | |
153 | if (other instanceof TmfCoalescedEventRequest<?>) { | |
154 | TmfCoalescedEventRequest<?> request = (TmfCoalescedEventRequest<?>) other; | |
155 | return (request.getDataType() == getDataType()) && | |
156 | (request.getIndex() == getIndex()) && | |
157 | (request.getNbRequested() == getNbRequested()) && | |
158 | (request.getRange().equals(getRange())); | |
159 | } | |
160 | if (other instanceof TmfCoalescedDataRequest<?>) { | |
161 | return super.equals(other); | |
162 | } | |
163 | return false; | |
164 | } | |
165 | ||
166 | @Override | |
167 | public String toString() { | |
168 | return "[TmfCoalescedEventRequest(" + getRequestId() + "," + getDataType().getSimpleName() | |
169 | + "," + getRange() + "," + getNbRequested() + "," + getBlockize() + ")]"; | |
170 | } | |
171 | ||
8c8bf09f | 172 | } |