]> git.basschouten.com Git - openhab-addons.git/commitdiff
[inmemory] Add filterCritera ordering (#16185)
authorJ-N-K <github@klug.nrw>
Tue, 2 Jan 2024 19:22:37 +0000 (20:22 +0100)
committerGitHub <noreply@github.com>
Tue, 2 Jan 2024 19:22:37 +0000 (20:22 +0100)
Signed-off-by: Jan N. Klug <github@klug.nrw>
bundles/org.openhab.persistence.inmemory/src/main/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceService.java
bundles/org.openhab.persistence.inmemory/src/test/java/org/openhab/persistence/inmemory/internal/InMemoryPersistenceTests.java

index 74999cebd21bc1bb761f78d7b3d93d995383b3e1..3b4ae0bac7322fb1260ca963a9cdb98850d02585 100644 (file)
@@ -173,9 +173,14 @@ public class InMemoryPersistenceService implements ModifiablePersistenceService
 
         Lock lock = persistItem.lock();
         lock.lock();
+
+        Comparator<PersistEntry> comparator = filter.getOrdering() == FilterCriteria.Ordering.ASCENDING
+                ? Comparator.comparing(PersistEntry::timestamp)
+                : Comparator.comparing(PersistEntry::timestamp).reversed();
+
         try {
-            return persistItem.database().stream().filter(e -> applies(e, filter)).map(e -> toHistoricItem(itemName, e))
-                    .toList();
+            return persistItem.database().stream().filter(e -> applies(e, filter)).sorted(comparator)
+                    .map(e -> toHistoricItem(itemName, e)).toList();
         } finally {
             lock.unlock();
         }
index 0ffe083ff9dcdb05bce281a87870c07632025c1b..ce945a6b6daac108939ee5b221304a70da137c66 100644 (file)
 package org.openhab.persistence.inmemory.internal;
 
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.closeTo;
-import static org.hamcrest.Matchers.empty;
-import static org.hamcrest.Matchers.hasSize;
-import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.*;
 import static org.mockito.Mockito.when;
 
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
+import java.util.ArrayList;
 import java.util.Comparator;
+import java.util.List;
 import java.util.TreeSet;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -144,6 +143,38 @@ public class InMemoryPersistenceTests {
         assertThat(storedStates, is(empty()));
     }
 
+    @Test
+    public void querySupportsAscendingOrdering() {
+        ZonedDateTime start = ZonedDateTime.of(2020, 12, 1, 12, 0, 0, 0, ZoneId.systemDefault());
+        service.store(item, start, new DecimalType(1));
+        service.store(item, start.plusHours(1), new DecimalType(2));
+        service.store(item, start.plusHours(2), new DecimalType(3));
+
+        filterCriteria.setOrdering(FilterCriteria.Ordering.ASCENDING);
+        filterCriteria.setBeginDate(start);
+
+        List<Integer> resultSet = new ArrayList<>();
+        service.query(filterCriteria).forEach(h -> resultSet.add(((DecimalType) h.getState()).intValue()));
+
+        assertThat(resultSet, contains(1, 2, 3));
+    }
+
+    @Test
+    public void querySupportsDescendingOrdering() {
+        ZonedDateTime start = ZonedDateTime.of(2020, 12, 1, 12, 0, 0, 0, ZoneId.systemDefault());
+        service.store(item, start, new DecimalType(1));
+        service.store(item, start.plusHours(1), new DecimalType(2));
+        service.store(item, start.plusHours(2), new DecimalType(3));
+
+        filterCriteria.setOrdering(FilterCriteria.Ordering.DESCENDING);
+        filterCriteria.setBeginDate(start);
+
+        List<Integer> resultSet = new ArrayList<>();
+        service.query(filterCriteria).forEach(h -> resultSet.add(((DecimalType) h.getState()).intValue()));
+
+        assertThat(resultSet, contains(3, 2, 1));
+    }
+
     @Test
     public void removeBetweenTimes() {
         State historicState1 = new StringType("value1");