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

com.github.leeonky.dal.runtime.CollectionDALCollection Maven / Gradle / Ivy

package com.github.leeonky.dal.runtime;

import com.github.leeonky.dal.IndexedElement;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class CollectionDALCollection extends DALCollectionBase {
    private final List list;

    public CollectionDALCollection(Collection collection) {
        list = new ArrayList<>(collection);
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    protected E getByPosition(int position) {
        return list.get(position);
    }

    @Override
    public Iterator> iterator() {
        return new Iterator>() {
            int index = 0;

            @Override
            public boolean hasNext() {
                return index < list.size();
            }

            @Override
            public IndexedElement next() {
                return new IndexedElement<>(index + firstIndex(), list.get(index++));
            }
        };
    }

    @Override
    public List collect() {
        return list;
    }

    @Override
    public CollectionDALCollection filter(Predicate predicate) {
        return new CollectionDALCollection(list.stream().filter(predicate).collect(Collectors.toList())) {
            @Override
            public int firstIndex() {
                return CollectionDALCollection.this.firstIndex();
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy