/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jena.util.iterator;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * An ExtendedIterator is a {@link ClosableIterator} on which other operations
 * are defined for convenience in iterator composition: composition, filtering
 * in, filtering out, and element mapping.<br>
 * NOTE that the result of these operations consumes the base iterator(s); they
 * do not make independent copies. <br>
 * The canonical implementation of ExtendedIterator is {@link NiceIterator},
 * which also defines static methods for these operations that will work on any
 * {@link ClosableIterator}s. <br>
 * 
 * @param <T>
 *            the type of element over which an instance of ExtendedIterator
 *            iterates
 */
public interface ExtendedIterator<T> extends ClosableIterator<T>
    {
    /**
         Answer the next object, and remove it. Equivalent to next(); remove().
    */
    public T removeNext();
    
    /**
         return a new iterator which delivers all the elements of this iterator and
         then all the elements of the other iterator. Does not copy either iterator;
         they are consumed as the result iterator is consumed.
     */
     public <X extends T> ExtendedIterator<T> andThen( Iterator<X> other );

     /**
         return a new iterator containing only the elements of _this_ which
         pass the filter _f_. The order of the elements is preserved. Does not
         copy _this_, which is consumed as the result is consumed.
     */
     public ExtendedIterator<T> filterKeep( Predicate<T> f );

     /**
         return a new iterator containing only the elements of _this_ which
         are rejected by the filter _f_. The order of the elements is preserved.
         Does not copy _this_, which is consumed as the result is consumed.
     */
     public ExtendedIterator<T> filterDrop( Predicate<T> f );

     /**
         return a new iterator where each element is the result of applying
         _map1_ to the corresponding element of _this_. _this_ is not
         copied; it is consumed as the result is consumed.
     */
     public <U> ExtendedIterator<U> mapWith( Function<T, U> map1 );

    /**
         Answer a list of the [remaining] elements of this iterator, in order,
         consuming this iterator.
    */
    public List<T> toList();

    /**
        Answer a set of the [remaining] elements of this iterator,
        consuming this iterator.
    */
    public Set<T> toSet();
    }

@@ -1642,20 +1642,16 @@
 ator%3CT%3E%0A
-    
 %7B%0A    /*
@@ -3306,10 +3306,758 @@
 ();%0A    
+%0A    %0A    /**%0A        Answer with an %7B@link Optional%7D.%0A        This operation assumes that the %7B@code ExtendedIterator%7D does not return null for %7B@code next()%7D. %0A        If it does,  %7B@code NullPointerException%7D is thrown.%0A        %3Cul%3E%0A        %3Cli%3EIf there is no next, return %7B@code Optional.empty()%7D%0A        %3Cli%3EIf the next object exists, and is not null, return that in the %7B@link Optional%7D.%0A        %3Cli%3EIf the next object exists, and is null, throw %7B@code NullPointerException%7D%0A        %3C/ul%3E%0A     */%0A    public default Optional%3CT%3E nextOptional() %7B%0A        if ( ! hasNext() )%0A            return Optional.empty();%0A        T obj = next();%0A        Objects.requireNonNull(obj, %22ExtendedIterator.next is null%22);%0A        return Optional.of(obj);%0A    %7D%0A
 %7D%0A

Merge commit 'refs/pull/323/head' of github.com:apache/jena