Class NonQueryJinqStream<T>

  • All Implemented Interfaces:
    java.lang.AutoCloseable, java.util.stream.BaseStream<T,​java.util.stream.Stream<T>>, java.util.stream.Stream<T>, JinqStream<T>
    Direct Known Subclasses:
    QueryJinqStream

    public class NonQueryJinqStream<T>
    extends LazyWrappedStream<T>
    implements JinqStream<T>
    • Field Detail

      • recordedExceptions

        protected java.util.Map<java.lang.Object,​java.lang.Throwable> recordedExceptions
    • Constructor Detail

      • NonQueryJinqStream

        public NonQueryJinqStream​(java.util.stream.Stream<T> wrapped)
      • NonQueryJinqStream

        public NonQueryJinqStream​(java.util.stream.Stream<T> wrapped,
                                  InQueryStreamSource inQueryStreamSource)
    • Method Detail

      • wrap

        protected <U> JinqStream<U> wrap​(java.util.stream.Stream<U> toWrap)
        Description copied from class: LazyWrappedStream
        Allows subclasses to wrap streams generated by this class with wrappers that provide additional functionality.
        Overrides:
        wrap in class LazyWrappedStream<T>
      • where

        public <E extends java.lang.Exception> JinqStream<T> where​(JinqStream.Where<T,​E> test)
        Description copied from interface: JinqStream
        Filters the elements of the stream.
         JinqStream<Customer> stream = ...;
         JinqStream<Customer> result = stream.where(c -> c.getName().equals("Alice"));
         
         
        Specified by:
        where in interface JinqStream<T>
        Parameters:
        test - function applied to the elements of the stream. When passed an element from the stream, the function should return true if the element should be kept. if the function returns false, the element is discarded.
        Returns:
        a new stream that returns only the elements satisfying the filter
      • where

        public <E extends java.lang.Exception> JinqStream<T> where​(JinqStream.WhereWithSource<T,​E> test)
        Description copied from interface: JinqStream
        Filters the elements of the stream. This version allows the filter function to take a second parameter with an InQueryStreamSource. This lets the function create new streams of elements that it can use in subqueries.
        Specified by:
        where in interface JinqStream<T>
        Parameters:
        test - function applied to each element of the stream. The function is passed an element from the stream as well as an InQueryStreamSource. The function should return true if the element should be kept. if the function returns false, the element is discarded.
        Returns:
        a new stream that returns only the elements satisfying the filter
        See Also:
        JinqStream.where(Where)
      • select

        public <U> JinqStream<U> select​(JinqStream.Select<T,​U> select)
        Description copied from interface: JinqStream
        Transforms the elements in the stream. The method allows you to rewrite each element from the stream, so that they contain only certain fields or to do some calculation based on the values of the fields.
         JinqStream<Customer> stream = ...;
         JinqStream<String> result = stream.select(c -> c.getName());
         
         
        Specified by:
        select in interface JinqStream<T>
        Parameters:
        select - function applied to the elements of the stream. When passed an element from the stream, the function should return a new value that should be used instead of the element in the stream.
        Returns:
        a new stream that uses only the new rewritten stream elements
      • selectAll

        public <U> JinqStream<U> selectAll​(JinqStream.Join<T,​U> select)
        Description copied from interface: JinqStream
        Transforms the elements in the stream. The method allows you to rewrite each element from the stream, so that they contain only certain fields or to do some calculation based on the values of the fields. Unlike a normal select(), this method allows you to return a stream of elements. The stream elements will all be added to the final stream.
         JinqStream<Country> stream = ...;
         JinqStream<City> result = stream.selectAll(c -> JinqStream.from(c.getCities()));
         
         
        Specified by:
        selectAll in interface JinqStream<T>
        Parameters:
        select - function applied to the elements of the stream. When passed an element from the stream, the function should return a stream of new values that will be flattened and placed in the new stream
        Returns:
        a new stream that uses only the new rewritten stream elements
        See Also:
        JinqStream.select(Select)
      • selectAllList

        public <U> JinqStream<U> selectAllList​(JinqStream.JoinToIterable<T,​U> select)
        Description copied from interface: JinqStream
        A variant of selectAll() that can be used if you want to join to a collection without the trouble of converting it to a JinqStream first.
         JinqStream<Country> stream = ...;
         JinqStream<City> result = stream.selectAll(c -> c.getCities());
         
         
        Specified by:
        selectAllList in interface JinqStream<T>
        Parameters:
        select - function applied to the elements of the stream. When passed an element from the stream, the function should return a collection of new values that will be flattened and placed in the new stream
        Returns:
        a new stream that uses only the new rewritten stream elements
        See Also:
        JinqStream.selectAll(Join)
      • join

        public <U> JinqStream<Pair<T,​U>> join​(JinqStream.Join<T,​U> join)
        Description copied from interface: JinqStream
        Pairs up each entry of the stream with a stream of related elements.
         JinqStream<Country> stream = ...;
         JinqStream<Pair<Country, City>> result = 
            stream.join(c -> JinqStream.from(c.getCities()));
         
         
        Specified by:
        join in interface JinqStream<T>
        Parameters:
        join - function applied to the elements of the stream. When passed an element from the stream, the function should return a stream of values that should be paired up with that stream element.
        Returns:
        a new stream with the paired up elements
      • leftOuterJoin

        public <U> JinqStream<Pair<T,​U>> leftOuterJoin​(JinqStream.Join<T,​U> join)
        Description copied from interface: JinqStream
        Pairs up each entry of the stream with a stream of related elements. Uses a left outer join during the pairing up process, so even if an element is not joined with anything, a pair will still be created in the output stream consisting of the element paired with null.
         JinqStream<Country> stream = ...;
         JinqStream<Pair<Country, Mountain>> result = 
            stream.leftOuterJoin(c -> JinqStream.from(c.getMountain()));
         JinqStream<Pair<Country, Mountain>> result = 
            stream.leftOuterJoin(c -> JinqStream.of(c.getHighestMountain()));
         
         
        Specified by:
        leftOuterJoin in interface JinqStream<T>
        Parameters:
        join - function applied to the elements of the stream. When passed an element from the stream, the function should return a stream of values that should be paired up with that stream element. The function must use a JPA association or navigational link as the base for the stream returned. Both singular or plural associations are allowed.
        Returns:
        a new stream with the paired up elements
        See Also:
        JinqStream.join(Join)
      • leftOuterJoin

        public <U> JinqStream<Pair<T,​U>> leftOuterJoin​(JinqStream.JoinWithSource<T,​U> join,
                                                             JinqStream.WhereForOn<T,​U> on)
        Description copied from interface: JinqStream
        Pairs up each entry of the stream with a stream of related elements. Uses a left outer join during the pairing up process, so even if an element is not joined with anything, a pair will still be created in the output stream consisting of the element paired with null. This version also passes an InQueryStreamSource to the join function so that the function can join elements with unrelated streams of entities from a database and an ON clause can be specified that will determine which elements from the two streams will be joined together.
         JinqStream<Country> stream = ...;
         JinqStream<Pair<Country, Mountain>> result = 
            stream.leftOuterJoin(
                    (c, source) -> source.stream(Mountain.class), 
                    (country, mountain) -> country.getName().equals(mountain.getCountry()));
         
         
        Specified by:
        leftOuterJoin in interface JinqStream<T>
        Parameters:
        join - function applied to the elements of the stream. When passed an element from the stream, the function should return a stream of values that should be paired up with that stream element. The function must use a JPA association or navigational link as the base for the stream returned. Both singular or plural associations are allowed.
        on - this is a comparison function that returns true if the elements from the two streams should be joined together. It is similar to a standard where() clause except that the elements from the two streams are passed in as separate parameters for convenience (as opposed to being passed in as a pair)
        Returns:
        a new stream with the paired up elements
        See Also:
        JinqStream.leftOuterJoin(Join)
      • crossJoin

        public <U> JinqStream<Pair<T,​U>> crossJoin​(JinqStream<U> join)
        Description copied from interface: JinqStream
        Performs a full cross-join of the elements of two streams.
        Specified by:
        crossJoin in interface JinqStream<T>
        Parameters:
        join - other stream to perform the cross join with
        Returns:
        a new stream where each element of the stream is paired up with each element of the join stream
      • group

        public <U,​V> JinqStream<Pair<U,​V>> group​(JinqStream.Select<T,​U> select,
                                                             JinqStream.AggregateGroup<U,​T,​V> aggregate)
        Description copied from interface: JinqStream
        Groups together elements from the stream that share a common key. Aggregates can then be calculated over the elements in each group.
         JinqStream<City> stream = ...;
         JinqStream<Pair<String, Long>> result = 
            stream.group(c -> c.getCountry(), (key, cities) -> cities.count());
         
         
        Specified by:
        group in interface JinqStream<T>
        Parameters:
        select - function applied to each element of the stream that returns the key to be used to group elements together
        aggregate - function applied to each group and calculates an aggregate value over the group. The function is passed the key for the group and a JinqStream of elements contained inside that group. It should return the aggregate value calculated for that group.
        Returns:
        a new stream containing a tuple for each group. The tuple contains the key for the group and any calculated aggregate values.
      • sumInteger

        public java.lang.Long sumInteger​(JinqStream.CollectInteger<T> aggregate)
        Description copied from interface: JinqStream
        Calculates a sum over the elements of a stream. Different sum methods are provided for calculating the sum of integer, long, double, BigDecimal, and BigInteger values.
         JinqStream<City> stream = ...;
         long totalPopulation = stream.sumInteger(c -> c.getPopulation()); 
         
         
        Specified by:
        sumInteger in interface JinqStream<T>
        Parameters:
        aggregate - function applied to each element of the stream. When passed an element of the stream, it should return the value that should be added to the sum.
        Returns:
        the sum of the values returned by the function
      • max

        public <V extends java.lang.Comparable<V>> V max​(JinqStream.CollectComparable<T,​V> aggregate)
        Description copied from interface: JinqStream
        Finds the largest or maximum element of a stream.
         JinqStream<Student> stream = ...;
         Date birthdayOfYoungest = stream.max(s -> s.getBirthday()); 
         
         
        Specified by:
        max in interface JinqStream<T>
        Parameters:
        aggregate - function applied to each element of the stream. When passed an element of the stream, it should return the value that should be compared.
        Returns:
        the maximum of the values returned by the function
      • min

        public <V extends java.lang.Comparable<V>> V min​(JinqStream.CollectComparable<T,​V> aggregate)
        Description copied from interface: JinqStream
        Finds the smallest or minimum element of a stream.
         JinqStream<Student> stream = ...;
         Date birthdayOfOldest = stream.min(s -> s.getBirthday()); 
         
         
        Specified by:
        min in interface JinqStream<T>
        Parameters:
        aggregate - function applied to each element of the stream. When passed an element of the stream, it should return the value that should be compared.
        Returns:
        the minimum of the values returned by the function
        See Also:
        JinqStream.max(CollectComparable)
      • avg

        public <V extends java.lang.Number & java.lang.Comparable<V>> java.lang.Double avg​(JinqStream.CollectNumber<T,​V> aggregate)
        Description copied from interface: JinqStream
        Finds the average of the elements of a stream.
         JinqStream<Student> stream = ...;
         double averageAge = stream.avg(s -> s.getage()); 
         
         
        Specified by:
        avg in interface JinqStream<T>
        Parameters:
        aggregate - function applied to each element of the stream. When passed an element of the stream, it should return the value that should be included in the average
        Returns:
        the average of the values returned by the function
      • sortedBy

        public <V extends java.lang.Comparable<V>> JinqStream<T> sortedBy​(JinqStream.CollectComparable<T,​V> sortField)
        Description copied from interface: JinqStream
        Sorts the elements of a stream in ascending order based on the value returned. The sort is stable, so it is possible to sort the stream multiple times in order to have multiple sort keys. The last sort becomes the primary sort key, and earlier sorts become lesser keys.
        Specified by:
        sortedBy in interface JinqStream<T>
        Parameters:
        sortField - function applied to each element of the stream. When passed an element of the stream, it should return the value that should be used as the sorting value of the element
        Returns:
        sorted stream
      • sortedDescendingBy

        public <V extends java.lang.Comparable<V>> JinqStream<T> sortedDescendingBy​(JinqStream.CollectComparable<T,​V> sortField)
        Description copied from interface: JinqStream
        Sorts the elements of a stream in descending order based on the value returned.
        Specified by:
        sortedDescendingBy in interface JinqStream<T>
        Parameters:
        sortField - function applied to each element of the stream. When passed an element of the stream, it should return the value that should be used as the sorting value of the element
        Returns:
        sorted stream
        See Also:
        JinqStream.sortedBy(CollectComparable)
      • findOne

        public java.util.Optional<T> findOne()
        Description copied from interface: JinqStream
        A convenience method for getting the contents of a stream when it contains 0 or 1 values. If the stream contains more than one value, an exception will be thrown. It cannot be used in subqueries.
        Specified by:
        findOne in interface JinqStream<T>
        Returns:
        an Optional with the single element contained in the stream or an empty Optional if the stream is empty
        See Also:
        Stream.findFirst()
      • findFirst

        public java.util.Optional<T> findFirst()
        A convenience method for limiting the stream to one element and then returning that element as an Optional. It cannot be used in subqueries.
        Specified by:
        findFirst in interface JinqStream<T>
        Specified by:
        findFirst in interface java.util.stream.Stream<T>
        Overrides:
        findFirst in class LazyWrappedStream<T>
        See Also:
        Stream.findFirst()
      • getOnlyValue

        public T getOnlyValue()
        Description copied from interface: JinqStream
        If the stream contains only a single value, this method will return that value. This method is convenient for getting the results of queries that contain only a single value. It is also useful in subqueries.
        Specified by:
        getOnlyValue in interface JinqStream<T>
        Returns:
        the single element contained in the stream
      • toList

        public java.util.List<T> toList()
        Description copied from interface: JinqStream
        Convenience method that collects the stream contents into a List.
        Specified by:
        toList in interface JinqStream<T>
        Returns:
        a list of all the elements from the stream
      • getDebugQueryString

        public java.lang.String getDebugQueryString()
        Description copied from interface: JinqStream
        Returns the query that Jinq will send to the database to generate the values of the stream.
        Specified by:
        getDebugQueryString in interface JinqStream<T>
        Returns:
        the database query string or null if Jinq cannot find a database query equivalent to the contents of the stream.
      • propagateException

        public void propagateException​(java.lang.Object source,
                                       java.lang.Throwable exception)
        Description copied from interface: JinqStream
        Used for recording an exception that occurred during processing somewhere in the stream chain.
        Specified by:
        propagateException in interface JinqStream<T>
        Parameters:
        source - lambda object that caused the exception (used so that if the same lambda causes multiple exceptions, only some of them need to be recorded in order to avoid memory issues)
        exception - actual exception object
      • getExceptions

        public java.util.Collection<java.lang.Throwable> getExceptions()
        Specified by:
        getExceptions in interface JinqStream<T>
      • aggregate

        public <U,​V> Pair<U,​V> aggregate​(JinqStream.AggregateSelect<T,​U> aggregate1,
                                                     JinqStream.AggregateSelect<T,​V> aggregate2)
        Description copied from interface: JinqStream
        Calculates more than one aggregate function over the elements of the stream.
         JinqStream<City> stream = ...;
         Pair<Long, Long> result = stream.aggregate(
            c -> c.sumInteger(c.getPopulation()),
            c -> c.count()); 
         
         
        Specified by:
        aggregate in interface JinqStream<T>
        Parameters:
        aggregate1 - a function that takes a stream and returns the first calculated aggregate value for the stream
        aggregate2 - a function that takes a stream and returns a second calculated aggregate value for the stream
        Returns:
        a tuple of the calculated aggregate values
      • setHint

        public JinqStream<T> setHint​(java.lang.String name,
                                     java.lang.Object value)
        Description copied from interface: JinqStream
        Sets a hint on the stream for how the query should be executed
        Specified by:
        setHint in interface JinqStream<T>
        Parameters:
        name - name of the hint to change
        value - value to assign to the hint
        Returns:
        a pointer to the stream, to make it easier to chain method calls on the stream