MtasDataCollectorResult.java.html
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasDataCollectorResult.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.codec.util.collector</a> > <span class="el_source">MtasDataCollectorResult.java</span></div><h1>MtasDataCollectorResult.java</h1><pre class="source lang-java linenums">package mtas.codec.util.collector;
import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
import mtas.codec.util.CodecUtil;
import mtas.codec.util.DataCollector;
/**
* The Class MtasDataCollectorResult.
*
* @param <T1> the generic type
* @param <T2> the generic type
*/
public class MtasDataCollectorResult<T1 extends Number & Comparable<T1>, T2 extends Number & Comparable<T2>>
implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The list. */
private SortedMap<String, MtasDataItem<T1, T2>> list;
/** The item. */
private MtasDataItem<T1, T2> item;
/** The sort type. */
private String sortType;
/** The sort direction. */
private String sortDirection;
/** The collector type. */
private String collectorType;
/** The last sort value. */
private MtasDataItemNumberComparator lastSortValue;
/** The start key. */
String startKey;
/** The end key. */
String endKey;
/**
* Instantiates a new mtas data collector result.
*
* @param collectorType the collector type
* @param sortType the sort type
* @param sortDirection the sort direction
* @param basicList the basic list
* @param start the start
* @param number the number
* @throws IOException Signals that an I/O exception has occurred.
*/
public MtasDataCollectorResult(String collectorType, String sortType,
String sortDirection, NavigableMap<String, MtasDataItem<T1, T2>> basicList,
Integer start, Integer number) throws IOException {
<span class="fc" id="L66"> this(collectorType, sortType, sortDirection);</span>
<span class="pc bpc" id="L67" title="1 of 4 branches missed."> if (sortType == null || sortType.equals(CodecUtil.SORT_TERM)) {</span>
<span class="pc bpc" id="L68" title="1 of 4 branches missed."> if (sortDirection == null || sortDirection.equals(CodecUtil.SORT_ASC)) {</span>
<span class="fc" id="L69"> list = basicList;</span>
<span class="pc bpc" id="L70" title="1 of 2 branches missed."> } else if (sortDirection.equals(CodecUtil.SORT_DESC)) {</span>
<span class="fc" id="L71"> list = basicList.descendingMap();</span>
} else {
<span class="nc" id="L73"> throw new IOException("unknown sort direction " + sortDirection);</span>
}
<span class="pc bpc" id="L75" title="1 of 2 branches missed."> } else if (CodecUtil.isStatsType(sortType)) {</span>
// comperator
<span class="fc" id="L77"> Comparator<String> valueComparator = new Comparator<String>() {</span>
@Override
public int compare(String k1, String k2) {
<span class="fc" id="L80"> int compare = basicList.get(k1).compareTo(basicList.get(k2));</span>
<span class="fc bfc" id="L81" title="All 2 branches covered."> return compare == 0 ? k1.compareTo(k2) : compare;</span>
}
};
<span class="fc" id="L84"> SortedMap<String, MtasDataItem<T1, T2>> sortedByValues = new TreeMap<>(</span>
valueComparator);
<span class="fc" id="L86"> sortedByValues.putAll(basicList);</span>
<span class="fc" id="L87"> list = sortedByValues;</span>
<span class="fc" id="L88"> } else {</span>
<span class="nc" id="L89"> throw new IOException("unknown sort type " + sortType);</span>
}
<span class="pc bpc" id="L91" title="1 of 2 branches missed."> int listStart = start == null ? 0 : start;</span>
<span class="pc bpc" id="L92" title="2 of 6 branches missed."> if (number == null || (start == 0 && number >= list.size())) {</span>
// do nothing, full list is ok
<span class="pc bpc" id="L94" title="2 of 4 branches missed."> } else if (listStart < list.size() && number > 0) {</span>
// subset
<span class="fc" id="L96"> String boundaryEndKey = null;</span>
<span class="fc" id="L97"> int counter = 0;</span>
<span class="fc" id="L98"> MtasDataItem<T1, T2> previous = null;</span>
<span class="fc bfc" id="L99" title="All 2 branches covered."> for (Entry<String, MtasDataItem<T1, T2>> entry : list.entrySet()) {</span>
<span class="fc bfc" id="L100" title="All 2 branches covered."> if (listStart == counter) {</span>
<span class="fc" id="L101"> startKey = entry.getKey();</span>
<span class="fc bfc" id="L102" title="All 2 branches covered."> } else if (listStart + number <= counter) {</span>
<span class="pc bpc" id="L103" title="1 of 4 branches missed."> if (sortType==null || sortType.equals(CodecUtil.SORT_TERM)) {</span>
<span class="fc" id="L104"> endKey = entry.getKey();</span>
<span class="fc" id="L105"> boundaryEndKey = entry.getKey();</span>
<span class="fc" id="L106"> break;</span>
<span class="fc bfc" id="L107" title="All 2 branches covered."> } else if (previous != null) {</span>
<span class="fc bfc" id="L108" title="All 2 branches covered."> if (previous.compareTo(entry.getValue()) != 0) {</span>
<span class="fc" id="L109"> break;</span>
} else {
<span class="fc" id="L111"> boundaryEndKey = entry.getKey();</span>
}
} else {
<span class="fc" id="L114"> endKey = entry.getKey();</span>
<span class="fc" id="L115"> boundaryEndKey = entry.getKey();</span>
<span class="fc" id="L116"> previous = entry.getValue();</span>
}
} else {
<span class="fc" id="L119"> endKey = entry.getKey();</span>
}
<span class="fc" id="L121"> counter++;</span>
<span class="fc" id="L122"> }</span>
<span class="fc" id="L123"> list = list.subMap(startKey, boundaryEndKey);</span>
<span class="fc" id="L124"> } else {</span>
<span class="nc" id="L125"> list = new TreeMap<>();</span>
}
<span class="pc bpc" id="L127" title="1 of 4 branches missed."> if (list.size() > 0 && sortType != null) {</span>
<span class="fc" id="L128"> lastSortValue = list.get(list.lastKey()).getComparableValue();</span>
}
<span class="fc" id="L130"> }</span>
/**
* Instantiates a new mtas data collector result.
*
* @param collectorType the collector type
* @param item the item
*/
public MtasDataCollectorResult(String collectorType,
MtasDataItem<T1, T2> item) {
<span class="fc" id="L140"> this(collectorType, null, null);</span>
<span class="fc" id="L141"> this.item = item;</span>
<span class="fc" id="L142"> }</span>
/**
* Instantiates a new mtas data collector result.
*
* @param collectorType the collector type
* @param sortType the sort type
* @param sortDirection the sort direction
*/
public MtasDataCollectorResult(String collectorType, String sortType,
<span class="fc" id="L152"> String sortDirection) {</span>
<span class="fc" id="L153"> list = null;</span>
<span class="fc" id="L154"> item = null;</span>
<span class="fc" id="L155"> lastSortValue = null;</span>
<span class="fc" id="L156"> this.collectorType = collectorType;</span>
<span class="fc" id="L157"> this.sortType = sortType;</span>
<span class="fc" id="L158"> this.sortDirection = sortDirection;</span>
<span class="fc" id="L159"> }</span>
/**
* Gets the list.
*
* @return the list
* @throws IOException Signals that an I/O exception has occurred.
*/
public final SortedMap<String, MtasDataItem<T1, T2>> getList()
throws IOException {
<span class="fc" id="L169"> return getList(true);</span>
}
/**
* Gets the list.
*
* @param reduce the reduce
* @return the list
* @throws IOException Signals that an I/O exception has occurred.
*/
public final SortedMap<String, MtasDataItem<T1, T2>> getList(boolean reduce)
throws IOException {
<span class="pc bpc" id="L181" title="1 of 2 branches missed."> if (collectorType.equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="pc bpc" id="L182" title="2 of 6 branches missed."> if (reduce && startKey != null && endKey != null) {</span>
<span class="fc" id="L183"> return list.subMap(startKey, endKey);</span>
} else {
<span class="fc" id="L185"> return list;</span>
}
} else {
<span class="nc" id="L188"> throw new IOException("type " + collectorType + " not supported");</span>
}
}
/**
* Gets the comparator list.
*
* @return the comparator list
* @throws IOException Signals that an I/O exception has occurred.
*/
@SuppressWarnings("rawtypes")
public final Map<String, MtasDataItemNumberComparator> getComparatorList()
throws IOException {
<span class="pc bpc" id="L201" title="1 of 2 branches missed."> if (collectorType.equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="fc" id="L202"> LinkedHashMap<String, MtasDataItemNumberComparator> comparatorList = new LinkedHashMap<>();</span>
<span class="fc bfc" id="L203" title="All 2 branches covered."> for (Entry<String, MtasDataItem<T1,T2>> entry: list.entrySet()) {</span>
<span class="fc" id="L204"> comparatorList.put(entry.getKey(), entry.getValue().getComparableValue());</span>
<span class="fc" id="L205"> }</span>
<span class="fc" id="L206"> return comparatorList;</span>
} else {
<span class="nc" id="L208"> throw new IOException("type " + collectorType + " not supported");</span>
}
}
/**
* Gets the last sort value.
*
* @return the last sort value
*/
@SuppressWarnings("rawtypes")
public final MtasDataItemNumberComparator getLastSortValue() {
<span class="fc" id="L219"> return lastSortValue;</span>
}
/**
* Gets the data.
*
* @return the data
* @throws IOException Signals that an I/O exception has occurred.
*/
public final MtasDataItem<T1, T2> getData() throws IOException {
<span class="pc bpc" id="L229" title="1 of 2 branches missed."> if (collectorType.equals(DataCollector.COLLECTOR_TYPE_DATA)) {</span>
<span class="fc" id="L230"> return item;</span>
} else {
<span class="nc" id="L232"> throw new IOException("type " + collectorType + " not supported");</span>
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
<span class="nc" id="L241"> StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L242"> buffer.append(this.getClass().getSimpleName() + "(");</span>
<span class="nc" id="L243"> buffer.append(collectorType+","+sortType+","+sortDirection);</span>
<span class="nc" id="L244"> buffer.append(")");</span>
<span class="nc" id="L245"> return buffer.toString();</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>