Ver Fonte

new: CQuery.from.finite(size, getter)
fix: tests

Ranides Atterwim há 2 anos atrás
pai
commit
e8ec7a8100

+ 59 - 13
assira.core/src/main/java/net/ranides/assira/collection/query/CQuery.java

@@ -54,7 +54,6 @@ public interface CQuery<T> extends Iterable<T> {
 
     /**
      * Returns BUILDER which allows construction Query by wrapping provided data source (for example: in memory collection).
-     * This is facade method: it allows creation from single data source.
      *
      * @param <T> T
      * @return builder
@@ -64,18 +63,6 @@ public interface CQuery<T> extends Iterable<T> {
         return WrapBuilder.INSTANCE;
     }
 
-    /**
-     * Returns new Query which compute results by creating new Stream in every terminate method.
-     * Supplier can create different stream on every call.|
-     *
-     * @param source source
-     * @param <T> T
-     * @return query
-     */
-    static  <T> CQuery<T> from(CheckedSupplier<Stream<T>,?> source) {
-        return from().stream(source);
-    }
-
     /**
      * Returns new Query which compute results by using provided collection.
      * Collection can be modified between call.
@@ -1484,6 +1471,65 @@ public interface CQuery<T> extends Iterable<T> {
             return iterable(() -> IteratorUtils.infinite(identity, source));
         }
 
+        /**
+         * Creates query with finite number of elements. Each item is created by subsequent calls to generator.
+         *
+         * Please note, that means generator should be statefull.
+         * Please note, that it is impossible to "rewind" generator, which means generated infinite stream probably
+         * will return different results on every call.
+         *
+         * @param source source
+         * @param <T> T
+         * @return query
+         */
+        public final <T extends F> CQuery<T> finite(CheckedSupplier<Optional<T>, ?> source) {
+            return iterable(() -> IteratorUtils.finite(source));
+        }
+
+        /**
+         * Creates query with finite number of elements. Each item is created by subsequent calls to generator.
+         *
+         * Please note, that generator does not need to be statefull.
+         * Generates receives "previously" computed result (or identity at start) and can make computation using it.
+         * For example, it can create all natural numbers by simple incrementation.
+         *
+         * @param identity starting value passed to generator
+         * @param source source
+         * @param <T> T
+         * @return query
+         */
+        public final <T extends F> CQuery<T> finite(T identity, CheckedFunction<T, Optional<T>, ?> source) {
+            return iterable(() -> IteratorUtils.finite(identity, source));
+        }
+
+        /**
+         * Creates query with virtual list made of generated elements
+         * Element as each index is computed by "function" at every access.
+         * Size of the list provided as first argument
+         *
+         * @param size size
+         * @param function function
+         * @param <T> T
+         * @return query
+         */
+        public final <T extends F> CQuery<T> finite(int size, IntFunction<? extends T> function) {
+            return CQuery.from().list(VirtualList.of(size, function));
+        }
+
+        /**
+         * Creates query with virtual list made of generated elements
+         * Element as each index is computed by "function" at every access.
+         * Size of the list computed by supplier provided as first argument.
+         *
+         * @param size size
+         * @param function function
+         * @param <T> T
+         * @return query
+         */
+        public final <T extends F> CQuery<T> finite(IntSupplier size, IntFunction<? extends T> function) {
+            return CQuery.from().list(VirtualList.of(size, function));
+        }
+
         /**
          * Creates stream with natural numbers from specified range.
          *

+ 1 - 1
assira.core/src/test/java/net/ranides/assira/collection/query/CQueryTest.java

@@ -160,7 +160,7 @@ public class CQueryTest {
             return v < 6;
         };
 
-        CQuery<Integer> query = CQuery.from(creator::get)
+        CQuery<Integer> query = CQuery.from().stream(creator::get)
             .filter(filter1)
             .cache(temp)
             .filter(filter2);

+ 23 - 5
assira.core/src/test/java/net/ranides/assira/reflection/impl/bean/RBeanModelTest.java

@@ -332,12 +332,12 @@ public class RBeanModelTest {
     }
 
     @Test
-    public void testOpenPrivateScope() {
+    public void testPrivateAccess_unsafe() {
+        Assume.assumeTrue(RuntimeConfiguration.FEATURE_BEAN_MODEL_FOR_PRIVATE());
+
         Object v = ForFluentBean.newHidden("hidden value");
         BeanModel.FluentMap f = BeanModel.typefor(v).fluent(v);
 
-        Assume.assumeTrue(RuntimeConfiguration.FEATURE_BEAN_MODEL_FOR_PRIVATE());
-
         assertEquals("hidden name", f.get("name"));
         assertEquals("hidden value", f.get("value"));
         assertEquals("hidden value", f.put("value", "other"));
@@ -345,7 +345,7 @@ public class RBeanModelTest {
     }
 
     @Test
-    public void testClosePrivateScope() {
+    public void testPrivateAccess_safe() {
         Assume.assumeFalse(RuntimeConfiguration.FEATURE_BEAN_MODEL_FOR_PRIVATE());
 
         Object v = ForFluentBean.newHidden("hidden value");
@@ -356,7 +356,9 @@ public class RBeanModelTest {
     }
 
     @Test
-    public void testPrivateAccess() {
+    public void testPrivateAccessParents_safe() {
+        Assume.assumeFalse(RuntimeConfiguration.FEATURE_BEAN_MODEL_FOR_PRIVATE());
+
         Object v = ForBeanModel2.newHidden();
         BeanModel.FluentMap f = BeanModel.typefor(v).fluent(v);
 
@@ -369,6 +371,22 @@ public class RBeanModelTest {
         assertEquals(new TreeSet<>(Arrays.asList("something", "age", "name")), new TreeSet<>(f.keySet()));
     }
 
+    @Test
+    public void testPrivateAccessParents_unsafe() {
+        Assume.assumeTrue(RuntimeConfiguration.FEATURE_BEAN_MODEL_FOR_PRIVATE());
+
+        Object v = ForBeanModel2.newHidden();
+        BeanModel.FluentMap f = BeanModel.typefor(v).fluent(v);
+
+        ForBeanModel2.PublicIntermediate z = new ForBeanModel2.PublicIntermediate();
+
+        assertNotNull(z.something());
+        assertNotNull(z.age());
+        assertNotNull(z.name());
+
+        assertEquals(new TreeSet<>(Arrays.asList("something", "age", "name", "mymethod")), new TreeSet<>(f.keySet()));
+    }
+
     public static class Point<N extends Number> {
         private N scale;
         private N x;