• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gephi / graphstore / #641

24 Oct 2026 03:34PM UTC coverage: 91.778% (+0.002%) from 91.776%
#641

push

web-flow
Include intervals ending at the query start in the IntervalMap estimators (#348)

When the query start matched an endpoint, the estimators included an
interval ending at the query start only if the binary search landed on
its end. With {[0,2],[2,4]} the FIRST estimator over [2,3] saw [0,2], but
not once [4,6] was added. Walk back over all intervals ending at or after
the query start, so an interval sharing an endpoint with the query always
overlaps it. This matches the closed intervals of Interval.compareTo, the
interval index and the existing estimator tests, and the touching intervals
already included at the query end.

The average skips intervals of weight zero, which only touch the query, so a
NaN or infinite value of a touching interval no longer fails the average.

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>

4 of 5 new or added lines in 1 file covered. (80.0%)

12268 of 13367 relevant lines covered (91.78%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.51
/src/main/java/org/gephi/graph/api/types/IntervalMap.java
1
/*
2
 * Copyright 2012-2013 Gephi Consortium
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
package org.gephi.graph.api.types;
17

18
import java.lang.reflect.Array;
19
import java.math.BigDecimal;
20
import java.math.RoundingMode;
21
import java.time.ZoneId;
22
import java.util.Arrays;
23
import org.gephi.graph.api.AttributeUtils;
24
import org.gephi.graph.api.Estimator;
25
import org.gephi.graph.api.Interval;
26
import org.gephi.graph.api.TimeFormat;
27
import org.gephi.graph.impl.FormattingAndParsingUtils;
28

29
/**
30
 * Abstract class that implement a sorted map between intervals and attribute values.
31
 * <p>
32
 * Implementations which extend this class customize the map for a unique type, which is represented by the
33
 * <code>T</code> parameter.
34
 *
35
 * @param <T> Value type
36
 */
37
public abstract class IntervalMap<T> implements TimeMap<Interval, T> {
38

39
    protected double[] array;
40
    protected int size = 0;
1 ✔
41

42
    /**
43
     * Default constructor.
44
     * <p>
45
     * The map is empty with zero capacity.
46
     */
47
    public IntervalMap() {
1 ✔
48
        array = new double[0];
1 ✔
49
    }
1 ✔
50

51
    /**
52
     * Constructor with capacity.
53
     * <p>
54
     * Using this constructor can improve performances if the number of timestamps is known in advance as it minimizes
55
     * array resizes.
56
     *
57
     * @param capacity timestamp capacity
58
     */
59
    public IntervalMap(int capacity) {
1 ✔
60
        array = new double[capacity];
1 ✔
61
        Arrays.fill(array, Double.MAX_VALUE);
1 ✔
62
    }
1 ✔
63

64
    /**
65
     * Constructor with an initial interval map.
66
     * <p>
67
     * The given array must be sorted and contain no duplicates.
68
     *
69
     * @param keys initial set content
70
     */
71
    protected IntervalMap(double[] keys) {
1 ✔
72
        array = new double[keys.length];
1 ✔
73
        System.arraycopy(keys, 0, array, 0, keys.length);
1 ✔
74
        size = keys.length / 2;
1 ✔
75
    }
1 ✔
76

77
    @Override
78
    public boolean put(Interval interval, T value) {
79
        if (value == null) {
1 ✔
80
            throw new NullPointerException();
1 ✔
81
        }
82
        Object values = getValuesArray();
1 ✔
83
        int valuesLength = Array.getLength(values);
1 ✔
84

85
        final int index = putInner(interval.getLow(), interval.getHigh());
1 ✔
86
        if (index < 0) {
1 ✔
87
            int insertIndex = -index - 1;
1 ✔
88

89
            if (size - 1 < valuesLength) {
1 ✔
90
                if (insertIndex < size - 1) {
1 ✔
91
                    System.arraycopy(values, insertIndex, values, insertIndex + 1, size - insertIndex - 1);
1 ✔
92
                }
93
                Array.set(values, insertIndex, value);
1 ✔
94
            } else {
95
                Object newArray = Array.newInstance(values.getClass().getComponentType(), valuesLength + 1);
1 ✔
96
                System.arraycopy(values, 0, newArray, 0, insertIndex);
1 ✔
97
                System.arraycopy(values, insertIndex, newArray, insertIndex + 1, valuesLength - insertIndex);
1 ✔
98
                Array.set(newArray, insertIndex, value);
1 ✔
99
                setValuesArray(newArray);
1 ✔
100
            }
101
            return true;
1 ✔
102
        } else {
103
            Array.set(values, index, value);
1 ✔
104
        }
105
        return false;
1 ✔
106
    }
107

108
    @Override
109
    public boolean remove(Interval interval) {
110
        Object values = getValuesArray();
1 ✔
111

112
        final int removeIndex = removeInner(interval.getLow(), interval.getHigh());
1 ✔
113
        if (removeIndex >= 0) {
1 ✔
114
            if (removeIndex != size) {
1 ✔
115
                System.arraycopy(values, removeIndex + 1, values, removeIndex, size - removeIndex);
1 ✔
116
            }
117
            return true;
1 ✔
118
        }
119
        return false;
1 ✔
120
    }
121

122
    @Override
123
    public Object get(Interval interval, Estimator estimator) {
124
        if (estimator == null) {
1 ✔
125
            return get(interval, (T) null);
×
126
        }
127

128
        if (!isSupported(estimator)) {
1 ✔
129
            throw new UnsupportedOperationException("Not supported estimator.");
×
130
        }
131
        switch (estimator) {
1 ✔
132
            case AVERAGE:
133
                return getAverage(interval);
1 ✔
134
            case MIN:
135
                return getMin(interval);
1 ✔
136
            case MAX:
137
                return getMax(interval);
1 ✔
138
            case FIRST:
139
                return getFirst(interval);
1 ✔
140
            case LAST:
141
                return getLast(interval);
1 ✔
142
            default:
143
                throw new UnsupportedOperationException("Not supported estimator.");
×
144
        }
145
    }
146

147
    @Override
148
    public T get(Interval interval, T defaultValue) {
149
        final int index = getIndex(interval.getLow(), interval.getHigh());
1 ✔
150
        if (index >= 0) {
1 ✔
151
            return getValue(index / 2);
1 ✔
152
        }
153
        return defaultValue;
1 ✔
154
    }
155

156
    @Override
157
    public T[] toValuesArray() {
158
        Object values = getValuesArray();
1 ✔
159
        int length = Array.getLength(values);
1 ✔
160
        if (values.getClass().getComponentType().isPrimitive() || size < length) {
1 ✔
161
            T[] res = (T[]) Array.newInstance(getTypeClass(), size);
1 ✔
162
            for (int i = 0; i < size; i++) {
1 ✔
163
                res[i] = (T) Array.get(values, i);
1 ✔
164
            }
165
            return res;
1 ✔
166
        } else {
167
            return (T[]) values;
1 ✔
168
        }
169
    }
170

171
    protected Object toNativeArray() {
172
        Object values = getValuesArray();
1 ✔
173
        int length = Array.getLength(values);
1 ✔
174
        if (size < length - 1) {
1 ✔
175
            Object res = Array.newInstance(values.getClass().getComponentType(), size);
1 ✔
176
            System.arraycopy(values, 0, res, 0, size);
1 ✔
177
            return res;
1 ✔
178
        }
179
        return values;
1 ✔
180
    }
181

182
    protected abstract T getValue(int index);
183

184
    protected abstract Object getValuesArray();
185

186
    protected abstract void setValuesArray(Object array);
187

188
    protected int putInner(double intervalStart, double intervalEnd) {
189
        int realSize = size * 2;
1 ✔
190
        int index = Arrays.binarySearch(array, 0, realSize, intervalStart);
1 ✔
191
        if (index < 0) {
1 ✔
192
            int insertIndex = -index - 1;
1 ✔
193

194
            if (insertIndex % 2 == 1) {
1 ✔
195
                overlappingIntervallException();
×
196
            }
197

198
            if (insertIndex < realSize) {
1 ✔
199
                double currentValue = array[insertIndex];
1 ✔
200
                if (intervalEnd > currentValue) {
1 ✔
201
                    overlappingIntervallException();
×
202
                }
203
            }
204

205
            if (realSize < array.length - 2) {
1 ✔
206
                if (insertIndex < realSize) {
1 ✔
207
                    System.arraycopy(array, insertIndex, array, insertIndex + 2, realSize - insertIndex);
×
208
                }
209
                array[insertIndex] = intervalStart;
1 ✔
210
                array[insertIndex + 1] = intervalEnd;
1 ✔
211
            } else {
212
                double[] newArray = new double[array.length + 2];
1 ✔
213
                System.arraycopy(array, 0, newArray, 0, insertIndex);
1 ✔
214
                System.arraycopy(array, insertIndex, newArray, insertIndex + 2, array.length - insertIndex);
1 ✔
215
                newArray[insertIndex] = intervalStart;
1 ✔
216
                newArray[insertIndex + 1] = intervalEnd;
1 ✔
217
                array = newArray;
1 ✔
218
            }
219

220
            size++;
1 ✔
221
            return -(insertIndex / 2) - 1;
1 ✔
222
        } else {
223
            int insertIndex = index % 2 == 0 ? index : index - 1;
1 ✔
224
            while (insertIndex - 2 >= 0 && array[insertIndex - 2] == intervalStart) {
1 ✔
225
                insertIndex -= 2;
1 ✔
226
            }
227
            int shift;
228
            for (; insertIndex < realSize && insertIndex >= 0; insertIndex += shift) {
1 ✔
229
                double startValue = array[insertIndex];
1 ✔
230
                double endValue = array[insertIndex + 1];
1 ✔
231
                if (startValue > intervalStart) {
1 ✔
232
                    if (intervalEnd > startValue) {
1 ✔
233
                        overlappingIntervallException();
×
234
                    }
235
                    break;
236
                }
237
                if (startValue == intervalStart && endValue > intervalEnd) {
1 ✔
238
                    if (intervalEnd <= startValue) {
1 ✔
239
                        break;
1 ✔
240
                    } else {
241
                        overlappingIntervallException();
×
242
                    }
243
                }
244
                shift = intervalEnd > endValue ? 2 : intervalEnd < endValue ? -2 : intervalStart > startValue ? 2 : 0;
1 ✔
245
                if (shift == 0) {
1 ✔
246
                    return insertIndex / 2;
1 ✔
247
                }
248
                if (startValue == intervalStart && endValue < intervalEnd && startValue != endValue) {
1 ✔
249
                    overlappingIntervallException();
×
250
                }
251
            }
252

253
            if (realSize < array.length - 2) {
1 ✔
254
                if (insertIndex < realSize) {
×
255
                    System.arraycopy(array, insertIndex, array, insertIndex + 2, realSize - insertIndex);
×
256
                }
257
                array[insertIndex] = intervalStart;
×
258
                array[insertIndex + 1] = intervalEnd;
×
259
            } else {
260
                double[] newArray = new double[array.length + 2];
1 ✔
261
                System.arraycopy(array, 0, newArray, 0, insertIndex);
1 ✔
262
                System.arraycopy(array, insertIndex, newArray, insertIndex + 2, array.length - insertIndex);
1 ✔
263
                newArray[insertIndex] = intervalStart;
1 ✔
264
                newArray[insertIndex + 1] = intervalEnd;
1 ✔
265
                array = newArray;
1 ✔
266
            }
267

268
            size++;
1 ✔
269
            return -(insertIndex / 2) - 1;
1 ✔
270
        }
271
    }
272

273
    protected int removeInner(double intervalStart, double intervalEnd) {
274
        int realSize = size * 2;
1 ✔
275
        int index = Arrays.binarySearch(array, 0, realSize, intervalStart);
1 ✔
276
        if (index >= 0) {
1 ✔
277
            int removeIndex = index % 2 == 0 ? index : index - 1;
1 ✔
278
            while (removeIndex - 2 >= 0 && array[removeIndex - 2] == intervalStart) {
1 ✔
279
                removeIndex -= 2;
1 ✔
280
            }
281
            int shift;
282
            for (; removeIndex < realSize && removeIndex >= 0; removeIndex += shift) {
1 ✔
283
                double startValue = array[removeIndex];
1 ✔
284
                if (startValue > intervalStart) {
1 ✔
285
                    return -1;
1 ✔
286
                }
287
                double endValue = array[removeIndex + 1];
1 ✔
288
                if (startValue == intervalStart && endValue > intervalEnd) {
1 ✔
289
                    return -1;
×
290
                }
291
                if ((shift = (intervalEnd > endValue ? 2
1 ✔
292
                        : intervalEnd < endValue ? -2 : intervalStart > startValue ? 2 : 0)) == 0) {
1 ✔
293
                    if (removeIndex == realSize - 2) {
1 ✔
294
                        size--;
1 ✔
295
                    } else {
296
                        System.arraycopy(array, removeIndex + 2, array, removeIndex, realSize - removeIndex - 2);
1 ✔
297
                        size--;
1 ✔
298
                    }
299
                    return removeIndex / 2;
1 ✔
300
                }
301

302
            }
303
        }
304
        return -1;
1 ✔
305
    }
306

307
    @Override
308
    public int size() {
309
        return size;
1 ✔
310
    }
311

312
    @Override
313
    public boolean isEmpty() {
314
        return size == 0;
1 ✔
315
    }
316

317
    /**
318
     * Returns true if this map contains an interval that starts or ends at <code>timestamp</code>.
319
     *
320
     * @param timestamp timestamp
321
     * @return true if contains, false otherwise
322
     */
323
    public boolean contains(double timestamp) {
324
        int realSize = size * 2;
×
325
        int index = Arrays.binarySearch(array, 0, realSize, timestamp);
×
326
        return index >= 0 && index < realSize;
×
327
    }
328

329
    protected int getIndex(double intervalStart, double intervalEnd) {
330
        int realSize = size * 2;
1 ✔
331
        int index = Arrays.binarySearch(array, 0, realSize, intervalStart);
1 ✔
332
        if (index >= 0) {
1 ✔
333
            int foundIndex = index % 2 == 0 ? index : index - 1;
1 ✔
334
            while (foundIndex - 2 >= 0 && array[foundIndex - 2] == intervalStart) {
1 ✔
335
                foundIndex -= 2;
1 ✔
336
            }
337
            int shift;
338
            for (; foundIndex < realSize && foundIndex >= 0; foundIndex += shift) {
1 ✔
339
                double startValue = array[foundIndex];
1 ✔
340
                if (startValue > intervalStart) {
1 ✔
341
                    return -1;
×
342
                }
343
                double endValue = array[foundIndex + 1];
1 ✔
344
                if (startValue == intervalStart && endValue > intervalEnd) {
1 ✔
345
                    return -1;
1 ✔
346
                }
347
                if ((shift = (intervalEnd > endValue ? 2
1 ✔
348
                        : intervalEnd < endValue ? -2 : intervalStart > startValue ? 2 : 0)) == 0) {
1 ✔
349
                    return foundIndex;
1 ✔
350
                }
351
            }
352
        }
353
        return -1;
1 ✔
354
    }
355

356
    protected int[] getOverlappingIntervals(double intervalStart, double intervalEnd) {
357
        int realSize = size * 2;
1 ✔
358
        int index = Arrays.binarySearch(array, 0, realSize, intervalStart);
1 ✔
359
        if (index >= 0) {
1 ✔
360
            int startIndex = index % 2 == 0 ? index : index - 1;
1 ✔
361
            for (; startIndex - 2 >= 0;) {
1 ✔
362
                if (array[startIndex - 1] >= intervalStart) {
1 ✔
363
                    startIndex -= 2;
1 ✔
364
                } else {
365
                    break;
366
                }
367
            }
368

369
            int[] res = new int[size - (startIndex / 2)];
1 ✔
370
            int i = 0;
1 ✔
371
            for (; startIndex < realSize && array[startIndex] <= intervalEnd; startIndex += 2) {
1 ✔
372
                res[i++] = startIndex / 2;
1 ✔
373
            }
374
            if (res.length != i) {
1 ✔
375
                return Arrays.copyOf(res, i);
1 ✔
376
            }
377
            return res;
1 ✔
378
        } else {
379
            int startIndex = (-index - 1) % 2 == 0 ? (-index - 1) : -index - 2;
1 ✔
380
            if (startIndex < realSize && array[startIndex] <= intervalEnd) {
1 ✔
381
                int[] res = new int[size - (startIndex / 2)];
1 ✔
382
                int i = 0;
1 ✔
383
                for (; startIndex < realSize && array[startIndex] <= intervalEnd; startIndex += 2) {
1 ✔
384
                    res[i++] = startIndex / 2;
1 ✔
385
                }
386
                if (i != 0) {
1 ✔
387
                    if (res.length != i) {
1 ✔
388
                        return Arrays.copyOf(res, i);
1 ✔
389
                    }
390
                    return res;
1 ✔
391
                }
392
            }
393
        }
394
        return new int[0];
1 ✔
395
    }
396

397
    protected double[] getIntervalsWeight(double intervalStart, double intervalEnd, int[] intervals) {
398
        double[] res = new double[intervals.length];
1 ✔
399
        for (int i = 0; i < intervals.length; i++) {
1 ✔
400
            double start = array[intervals[i] * 2];
1 ✔
401
            double end = array[intervals[i] * 2 + 1];
1 ✔
402
            if (start != end) {
1 ✔
403
                start = Math.max(intervalStart, start);
1 ✔
404
                end = Math.min(intervalEnd, end);
1 ✔
405
                res[i] = end - start;
1 ✔
406
            }
407
        }
408
        return res;
1 ✔
409
    }
410

411
    @Override
412
    public boolean contains(Interval interval) {
413
        return getIndex(interval.getLow(), interval.getHigh()) >= 0;
1 ✔
414
    }
415

416
    @Override
417
    public Interval[] toKeysArray() {
418
        Interval[] res = new Interval[size];
1 ✔
419
        for (int i = 0; i < size; i++) {
1 ✔
420
            res[i] = new Interval(array[i * 2], array[i * 2 + 1]);
1 ✔
421
        }
422
        return res;
1 ✔
423
    }
424

425
    /**
426
     * Returns an array of all intervals in this set.
427
     * <p>
428
     * The intervals are represented in a flat and sorted array (e.g. {[1.0,2.0], [5.0,6.0]}) returns
429
     * [1.0,2.0,5.0,6.0]).
430
     * <p>
431
     * This method may return a reference to the underlying array so clients should make a copy if the array is written
432
     * to.
433
     *
434
     * @return array of all intervals
435
     */
436
    public double[] getIntervals() {
437
        int realSize = size * 2;
1 ✔
438
        if (realSize < array.length) {
1 ✔
439
            double[] res = new double[realSize];
1 ✔
440
            System.arraycopy(array, 0, res, 0, realSize);
1 ✔
441
            return res;
1 ✔
442
        } else {
443
            return array;
1 ✔
444
        }
445
    }
446

447
    @Override
448
    public void clear() {
449
        size = 0;
1 ✔
450
        array = new double[0];
1 ✔
451
    }
1 ✔
452

453
    private void overlappingIntervallException() {
454
        throw new IllegalArgumentException("Overlapping intervals aren't allowed");
1 ✔
455
    }
456

457
    @Override
458
    public int hashCode() {
459
        int hash = 7;
1 ✔
460
        hash = 29 * hash + this.size;
1 ✔
461
        int realSize = size * 2;
1 ✔
462
        for (int i = 0; i < realSize; i++) {
1 ✔
463
            double t = this.array[i];
1 ✔
464
            hash = 29 * hash + (int) (Double.doubleToLongBits(t) ^ (Double.doubleToLongBits(t) >>> 32));
1 ✔
465
            if (i % 2 == 0) {
1 ✔
466
                Object obj = this.getValue(i / 2);
1 ✔
467
                hash = 29 * hash + obj.hashCode();
1 ✔
468
            }
469
        }
470
        return hash;
1 ✔
471
    }
472

473
    @Override
474
    public boolean equals(Object obj) {
475
        if (obj == null) {
1 ✔
476
            return false;
×
477
        }
478
        if (getClass() != obj.getClass()) {
1 ✔
479
            return false;
×
480
        }
481
        final IntervalMap<?> other = (IntervalMap<?>) obj;
1 ✔
482
        if (this.size != other.size) {
1 ✔
483
            return false;
1 ✔
484
        }
485
        int realSize = size * 2;
1 ✔
486
        for (int i = 0; i < realSize; i++) {
1 ✔
487
            double i1 = this.array[i];
1 ✔
488
            double i2 = other.array[i];
1 ✔
489
            if (i1 != i2) {
1 ✔
490
                return false;
×
491
            }
492
            if (i % 2 == 0) {
1 ✔
493
                Object o1 = this.getValue(i / 2);
1 ✔
494
                Object o2 = other.getValue(i / 2);
1 ✔
495
                if ((o1 == null && o2 != null) || (o1 != null && o2 == null) || (o1 != null && o2 != null && !o1
1 ✔
496
                        .equals(o2))) {
1 ✔
497
                    return false;
1 ✔
498
                }
499
            }
500
        }
501
        return true;
1 ✔
502
    }
503

504
    // Estimators
505
    protected Object getFirst(final Interval interval) {
506
        if (size == 0) {
1 ✔
507
            return null;
1 ✔
508
        }
509
        int[] intervals = getOverlappingIntervals(interval.getLow(), interval.getHigh());
1 ✔
510
        if (intervals.length == 0) {
1 ✔
511
            return null;
1 ✔
512
        }
513
        return getValue(intervals[0]);
1 ✔
514
    }
515

516
    protected Object getLast(final Interval interval) {
517
        if (size == 0) {
1 ✔
518
            return null;
1 ✔
519
        }
520
        int[] intervals = getOverlappingIntervals(interval.getLow(), interval.getHigh());
1 ✔
521
        if (intervals.length == 0) {
1 ✔
522
            return null;
1 ✔
523
        }
524
        return getValue(intervals[intervals.length - 1]);
1 ✔
525
    }
526

527
    protected Object getMin(final Interval interval) {
528
        Double min = getMinDouble(interval);
1 ✔
529
        return min != null ? min.doubleValue() : null;
1 ✔
530
    }
531

532
    protected Double getMinDouble(final Interval interval) {
533
        if (size == 0) {
1 ✔
534
            return null;
1 ✔
535
        }
536
        int[] intervals = getOverlappingIntervals(interval.getLow(), interval.getHigh());
1 ✔
537
        if (intervals.length == 0) {
1 ✔
538
            return null;
1 ✔
539
        }
540
        double min = Double.POSITIVE_INFINITY;
1 ✔
541
        for (int i = 0; i < intervals.length; i++) {
1 ✔
542
            double val = ((Number) getValue(intervals[i])).doubleValue();
1 ✔
543
            min = Math.min(val, min);
1 ✔
544
        }
545
        return min;
1 ✔
546
    }
547

548
    protected Object getMax(final Interval interval) {
549
        Double max = getMaxDouble(interval);
1 ✔
550
        return max != null ? max.doubleValue() : null;
1 ✔
551
    }
552

553
    protected Double getMaxDouble(final Interval interval) {
554
        if (size == 0) {
1 ✔
555
            return null;
1 ✔
556
        }
557
        int[] intervals = getOverlappingIntervals(interval.getLow(), interval.getHigh());
1 ✔
558
        if (intervals.length == 0) {
1 ✔
559
            return null;
1 ✔
560
        }
561
        double max = Double.NEGATIVE_INFINITY;
1 ✔
562
        for (int i = 0; i < intervals.length; i++) {
1 ✔
563
            double val = ((Number) getValue(intervals[i])).doubleValue();
1 ✔
564
            max = Math.max(val, max);
1 ✔
565
        }
566
        return max;
1 ✔
567
    }
568

569
    protected Object getAverage(final Interval interval) {
570
        BigDecimal average = getAverageBigDecimal(interval);
1 ✔
571
        return average != null ? average.doubleValue() : null;
1 ✔
572
    }
573

574
    protected BigDecimal getAverageBigDecimal(final Interval interval) {
575
        if (size == 0) {
1 ✔
576
            return null;
1 ✔
577
        }
578
        int[] intervals = getOverlappingIntervals(interval.getLow(), interval.getHigh());
1 ✔
579
        if (intervals.length == 0) {
1 ✔
580
            return null;
1 ✔
581
        }
582
        double[] weights = getIntervalsWeight(interval.getLow(), interval.getHigh(), intervals);
1 ✔
583
        BigDecimal result = new BigDecimal(0.0);
1 ✔
584
        BigDecimal period = new BigDecimal(0.0);
1 ✔
585
        for (int i = 0; i < intervals.length; i++) {
1 ✔
586
            if (weights[i] == 0.0) {
1 ✔
587
                // Intervals touching the query don't count
588
                continue;
1 ✔
589
            }
590
            BigDecimal w = new BigDecimal(weights[i]);
1 ✔
591
            period = period.add(w);
1 ✔
592
            w = w.multiply(new BigDecimal(((Number) getValue(intervals[i])).doubleValue()));
1 ✔
593
            result = result.add(w);
1 ✔
594
        }
595
        return result.divide(period, 10, RoundingMode.HALF_EVEN);
1 ✔
596
    }
597

598
    protected Double getAverageDouble(final Interval interval) {
599
        if (size == 0) {
1 ✔
600
            return null;
1 ✔
601
        }
602
        int[] intervals = getOverlappingIntervals(interval.getLow(), interval.getHigh());
1 ✔
603
        if (intervals.length == 0) {
1 ✔
604
            return null;
1 ✔
605
        }
606
        double[] weights = getIntervalsWeight(interval.getLow(), interval.getHigh(), intervals);
1 ✔
607
        double result = 0.0, period = 0.0;
1 ✔
608
        for (int i = 0; i < intervals.length; i++) {
1 ✔
609
            double w = weights[i];
1 ✔
610
            if (w == 0.0) {
1 ✔
611
                // Intervals touching the query don't count
NEW
612
                continue;
×
613
            }
614
            period += w;
1 ✔
615
            w *= ((Number) getValue(intervals[i])).doubleValue();
1 ✔
616
            result += w;
1 ✔
617
        }
618
        return result / period;
1 ✔
619
    }
620

621
    @Override
622
    public String toString(TimeFormat timeFormat, ZoneId zoneId) {
623
        if (size == 0) {
1 ✔
624
            return FormattingAndParsingUtils.EMPTY_VALUE;
1 ✔
625
        }
626

627
        T[] values = toValuesArray();
1 ✔
628

629
        StringBuilder sb = new StringBuilder();
1 ✔
630
        sb.append('<');
1 ✔
631
        for (int i = 0; i < size; i++) {
1 ✔
632
            sb.append('[');
1 ✔
633
            sb.append(AttributeUtils.printTimestampInFormat(array[i * 2], timeFormat, zoneId));
1 ✔
634
            sb.append(", ");
1 ✔
635
            sb.append(AttributeUtils.printTimestampInFormat(array[i * 2 + 1], timeFormat, zoneId));
1 ✔
636

637
            sb.append(", ");
1 ✔
638
            String stringValue = values[i].toString();
1 ✔
639
            if (FormattingAndParsingUtils.containsDynamicSpecialCharacters(stringValue) || stringValue.trim()
1 ✔
640
                    .isEmpty()) {
1 ✔
641
                sb.append('"');
1 ✔
642
                sb.append(stringValue.replace("\\", "\\\\").replace("\"", "\\\""));
1 ✔
643
                sb.append('"');
1 ✔
644
            } else {
645
                sb.append(stringValue);
1 ✔
646
            }
647

648
            sb.append(']');
1 ✔
649

650
            if (i < size - 1) {
1 ✔
651
                sb.append("; ");
1 ✔
652
            }
653
        }
654
        sb.append('>');
1 ✔
655

656
        return sb.toString();
1 ✔
657
    }
658

659
    @Override
660
    public String toString(TimeFormat timeFormat) {
661
        return toString(timeFormat, null);
1 ✔
662
    }
663

664
    @Override
665
    public String toString() {
666
        return toString(TimeFormat.DOUBLE, null);
1 ✔
667
    }
668
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc