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

com.gs.collections.impl.lazy.primitive.SelectLongIterable Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2014 Goldman Sachs.
 *
 * Licensed 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 com.gs.collections.impl.lazy.primitive;

import java.util.NoSuchElementException;

import com.gs.collections.api.LongIterable;
import com.gs.collections.api.bag.primitive.MutableLongBag;
import com.gs.collections.api.block.predicate.primitive.LongPredicate;
import com.gs.collections.api.block.procedure.primitive.LongProcedure;
import com.gs.collections.api.iterator.LongIterator;
import com.gs.collections.api.list.primitive.MutableLongList;
import com.gs.collections.api.set.primitive.MutableLongSet;
import com.gs.collections.impl.bag.mutable.primitive.LongHashBag;
import com.gs.collections.impl.block.factory.primitive.LongPredicates;
import com.gs.collections.impl.list.mutable.primitive.LongArrayList;
import com.gs.collections.impl.set.mutable.primitive.LongHashSet;

/**
 * This file was automatically generated from template file selectPrimitiveIterable.stg.
 */
public class SelectLongIterable
        extends AbstractLazyLongIterable
{
    private final LongIterable delegate;
    private final LongPredicate predicate;

    public SelectLongIterable(LongIterable delegate, LongPredicate predicate)
    {
        this.delegate = delegate;
        this.predicate = predicate;
    }

    public LongIterator longIterator()
    {
        return new SelectLongIterator(this.delegate, this.predicate);
    }

    /**
     * @since 7.0.
     */
    public void each(LongProcedure procedure)
    {
        this.delegate.forEach(new IfLongProcedure(procedure));
    }

    @Override
    public int size()
    {
        return this.delegate.count(this.predicate);
    }

    @Override
    public boolean isEmpty()
    {
        return !this.longIterator().hasNext();
    }

    @Override
    public boolean notEmpty()
    {
        return this.longIterator().hasNext();
    }

    @Override
    public int count(LongPredicate predicate)
    {
        CountLongProcedure countLongProcedure = new CountLongProcedure(predicate);
        this.forEach(countLongProcedure);
        return countLongProcedure.getCount();
    }

    @Override
    public boolean anySatisfy(LongPredicate predicate)
    {
        return this.delegate.anySatisfy(LongPredicates.and(this.predicate, predicate));
    }

    @Override
    public boolean allSatisfy(LongPredicate predicate)
    {
        return this.noneSatisfy(LongPredicates.not(predicate));
    }

    @Override
    public boolean noneSatisfy(LongPredicate predicate)
    {
        return !this.anySatisfy(predicate);
    }

    @Override
    public long[] toArray()
    {
        final long[] array = new long[this.size()];
        this.forEach(new LongProcedure()
        {
            @SuppressWarnings("FieldMayBeFinal")
            private int index = 0;
            public void value(long each)
            {
                array[this.index++] = each;
            }
        });
        return array;
    }

    @Override
    public boolean containsAll(long... source)
    {
        for (long value : source)
        {
            if (!this.contains(value))
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean containsAll(LongIterable source)
    {
        for (LongIterator iterator = source.longIterator(); iterator.hasNext(); )
        {
            if (!this.contains(iterator.next()))
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public MutableLongList toList()
    {
        return LongArrayList.newList(this);
    }

    @Override
    public MutableLongSet toSet()
    {
        return LongHashSet.newSet(this);
    }

    @Override
    public MutableLongBag toBag()
    {
        return LongHashBag.newBag(this);
    }

    private static final class CountLongProcedure implements LongProcedure
    {
        private static final long serialVersionUID = 1L;
        private final LongPredicate predicate;
        private int counter = 0;

        private CountLongProcedure(LongPredicate predicate)
        {
            this.predicate = predicate;
        }

        public void value(long each)
        {
            if (this.predicate.accept(each))
            {
                this.counter++;
            }
        }

        public int getCount()
        {
            return this.counter;
        }
    }

    private final class IfLongProcedure implements LongProcedure
    {
        private static final long serialVersionUID = 1L;
        private final LongProcedure procedure;

        private IfLongProcedure(LongProcedure procedure)
        {
            this.procedure = procedure;
        }

        public void value(long each)
        {
            if (SelectLongIterable.this.predicate.accept(each))
            {
                this.procedure.value(each);
            }
        }
    }

    private static final class SelectLongIterator
            implements LongIterator
    {
        private final LongIterator iterator;
        private final LongPredicate predicate;
        private long next;
        private boolean verifiedHasNext = false;

        private SelectLongIterator(LongIterable iterable, LongPredicate predicate)
        {
            this(iterable.longIterator(), predicate);
        }

        private SelectLongIterator(LongIterator iterator, LongPredicate predicate)
        {
            this.iterator = iterator;
            this.predicate = predicate;
        }

        public boolean hasNext()
        {
            if (this.verifiedHasNext)
            {
                return true;
            }
            while (this.iterator.hasNext())
            {
                long temp = this.iterator.next();
                if (this.predicate.accept(temp))
                {
                    this.next = temp;
                    this.verifiedHasNext = true;
                    return true;
                }
            }
            return false;
        }

        public long next()
        {
            if (this.verifiedHasNext || this.hasNext())
            {
                this.verifiedHasNext = false;
                return this.next;
            }
            throw new NoSuchElementException();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy