All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.davidmoten.odata.client.Paged Maven / Gradle / Ivy

The newest version!
package com.github.davidmoten.odata.client;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public interface Paged> extends Iterable {

    List currentPage();

    Optional nextPage();

    default List toList() {
        return Util.add(this, new ArrayList());
    }
    
    default Set toSet() {
        return Util.add(this, new HashSet());
    }

    default Stream stream() {
        return StreamSupport.stream(this.spliterator(), false);
    }
    
    default  S to(Function,? extends S> function) {
    	return function.apply(this);
    }

    @Override
    default Iterator iterator() {
        return new Iterator() {

            Paged page = Paged.this;
            int i = 0;

            @Override
            public boolean hasNext() {
                loadNext();
                return page != null;
            }

            @Override
            public T next() {
                loadNext();
                if (page == null) {
                    throw new NoSuchElementException();
                } else {
                    T v = page.currentPage().get(i);
                    i++;
                    return v;
                }
            }

            private void loadNext() {
                while (true) {
                    if (page != null && i == page.currentPage().size()) {
                        page = page.nextPage().orElse(null);
                        i = 0;
                    } else {
                        break;
                    }
                }
            }

        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy