Skip to content

Commit 53a6b24

Browse files
committed
Fixing Utils.addTimeOrdered loop and loop terminator to allow adding a window element to the front of a list
1 parent 513d711 commit 53a6b24

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

  • src

src/main/java/com/scaleoutsoftware/streaming/timewindowing/Utils.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@ static <T> void addTimeOrdered(List<T> source, TimestampSelector<T> selector, T
3333
int index = source.size() - 1;
3434
long timeToAdd = selector.select(toAdd);
3535
long last = selector.select(source.get(index));
36+
3637
if(timeToAdd < last) {
37-
while(timeToAdd < last) {
38-
index--;
39-
last = selector.select(source.get(index));
38+
while(index > 0) {
39+
if(timeToAdd < last) {
40+
index--;
41+
last = selector.select(source.get(index));
42+
} else {
43+
index++;
44+
break;
45+
}
4046
}
41-
source.add(index++, toAdd);
47+
source.add(index, toAdd);
4248
} else {
43-
source.add(toAdd);
49+
source.add(++index, toAdd);
4450
}
45-
4651
}
4752

4853
/**

src/test/java/com/scaleoutsoftware/streaming/timewindowing/tests/WindowingTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ public void testUtilsAdd() {
143143
Assert.assertTrue(tumblingSource.size() == numElements);
144144
}
145145

146+
@Test
147+
public void TestUtilsAddToFront() {
148+
ArrayList<TestObject> list = new ArrayList<>(25);
149+
long start = System.currentTimeMillis();
150+
long every = 1000L;
151+
long duration = 2500L;
152+
SlidingWindowCollection<TestObject> swc = new SlidingWindowCollection<TestObject>(list,
153+
test -> test.getTimestamp(),
154+
duration,
155+
every,
156+
start);
157+
long addToFront = System.currentTimeMillis();
158+
swc.add(new TestObject(System.currentTimeMillis() + 1000L));
159+
swc.add(new TestObject(addToFront));
160+
Assert.assertTrue(list.size() == 2);
161+
Assert.assertTrue(list.get(0).getTimestamp() == addToFront);
162+
}
163+
146164
@Test
147165
public void testEviction() {
148166
int numElements = 100;

0 commit comments

Comments
 (0)