src/main/java/kweimann/mezzure/util/Collections.java
/**
     * slides a window over collection
     * @param iterable collection
     * @param size size of the window
     * @param padding number of null elements to prepend & append to the collection
     * @return window iterator
     */
slides a window over collection
@param iterable collection
@param size size of the window
@param padding number of null elements to prepend & append to the collection
@return window iterator
slides a window over collection @param iterable collection @param size size of the window @param padding number of null elements to prepend & append to the collection @return window iterator
public static <A> Iterable<Window<A>> slideWindow(Iterable<A> iterable, int size, int padding) {
        if (padding >= size) throw new IllegalArgumentException("padding may not exceed size");
        return () -> new Iterator<Window<A>>() {
            Iterator<A> it = iterable.iterator();
            _Window<A> window = new _Window<>(size);
            int trailingNulls = padding;
            {
                int _size = 0;
                while (_size < padding) {
                    window.add(null);
                    _size++;
                }
                while (it.hasNext() && ++_size < size)
                    window.add(it.next());
                while (trailingNulls > 0 && ++_size < size) {
                    window.add(null);
                    trailingNulls--;
                }
            }
            @Override
            public boolean hasNext() {
                return it.hasNext() || trailingNulls > 0;
            }
            @Override
            public Window<A> next() {
                A next;
                if (it.hasNext()) next = it.next();
                else if (trailingNulls-- > 0) next = null;
                else throw new NoSuchElementException();
                if (window.full()) window.slide(next);
                else window.add(next);
                return window;
            }
        };
    }
public static < A > Iterable < Window < A > > slideWindow ( Iterable < A > iterable , int size , int padding ) { if ( padding >= size ) throw new IllegalArgumentException ( "padding may not exceed size" ) ; return ( ) -> new Iterator < Window < A > > ( ) { Iterator < A > it = iterable . iterator ( ) ; _Window < A > window = new _Window < > ( size ) ; int trailingNulls = padding ; { int _size = 0 ; while ( _size < padding ) { window . add ( null ) ; _size ++ ; } while ( it . hasNext ( ) && ++ _size < size ) window . add ( it . next ( ) ) ; while ( trailingNulls > 0 && ++ _size < size ) { window . add ( null ) ; trailingNulls -- ; } } @ Override public boolean hasNext ( ) { return it . hasNext ( ) || trailingNulls > 0 ; } @ Override public Window < A > next ( ) { A next ; if ( it . hasNext ( ) ) next = it . next ( ) ; else if ( trailingNulls -- > 0 ) next = null ; else throw new NoSuchElementException ( ) ; if ( window . full ( ) ) window . slide ( next ) ; else window . add ( next ) ; return window ; } } ; }
slides a window over collection
 @param iterable collection
 @param size size of the window
 @param padding number of null elements to prepend & append to the collection
 @return window iterator
slides a window over collection @param iterable collection @param size size of the window @param padding number of null elements to prepend & append to the collection @return window iterator
// add size - 1 elements to the window