Package com.gs.collections.impl.lazy.primitive

Source Code of com.gs.collections.impl.lazy.primitive.SelectShortIterable$CountShortProcedure

/*
* 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.io.IOException;
import java.util.Arrays;
import java.util.NoSuchElementException;

import com.gs.collections.api.ShortIterable;
import com.gs.collections.api.LazyShortIterable;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.block.function.primitive.ShortToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectShortToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.ShortPredicate;
import com.gs.collections.api.block.procedure.primitive.ShortProcedure;
import com.gs.collections.api.iterator.ShortIterator;
import com.gs.collections.api.bag.primitive.MutableShortBag;
import com.gs.collections.api.list.primitive.MutableShortList;
import com.gs.collections.api.set.primitive.MutableShortSet;
import com.gs.collections.impl.bag.mutable.primitive.ShortHashBag;
import com.gs.collections.impl.set.mutable.primitive.ShortHashSet;
import com.gs.collections.impl.list.mutable.primitive.ShortArrayList;
import com.gs.collections.impl.block.factory.primitive.ShortPredicates;

/**
* This file was automatically generated from template file selectPrimitiveIterable.stg.
*/
public class SelectShortIterable
    extends AbstractLazyShortIterable
{
    private final ShortIterable delegate;
    private final ShortPredicate predicate;

    public SelectShortIterable(ShortIterable delegate, ShortPredicate predicate)
    {
        this.delegate = delegate;
        this.predicate = predicate;
    }

    public ShortIterator shortIterator()
    {
        return new SelectShortIterator(this.delegate, this.predicate);
    }

    public void forEach(ShortProcedure procedure)
    {
        this.delegate.forEach(new IfShortProcedure(procedure));
    }

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

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

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

    @Override
    public int count(ShortPredicate predicate)
    {
        CountShortProcedure countShortProcedure = new CountShortProcedure(predicate);
        this.forEach(countShortProcedure);
        return countShortProcedure.getCount();
    }

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

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

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

        public long sum()
        {
            long sum = 0L;
            for (ShortIterator shortIterator = this.shortIterator(); shortIterator.hasNext() ;)
            {
                sum += shortIterator.next();
            }
            return sum;
        }

        public short max()
        {
            ShortIterator shortIterator = this.shortIterator();
            short max = shortIterator.next();
            while (shortIterator.hasNext())
            {
                max = (short) Math.max(max, shortIterator.next());
            }
            return max;
        }

        public short min()
        {
            ShortIterator shortIterator = this.shortIterator();
            short min = shortIterator.next();
            while (shortIterator.hasNext())
            {
                min = (short) Math.min(min, shortIterator.next());
            }
            return min;
        }

        public short minIfEmpty(short defaultValue)
        {
            try
            {
                return this.min();
            }
            catch (NoSuchElementException ex)
            {
            }
            return defaultValue;
        }

        public short maxIfEmpty(short defaultValue)
        {
            try
            {
                return this.max();
            }
            catch (NoSuchElementException ex)
            {
            }
            return defaultValue;
        }

        public double average()
        {
            if (this.isEmpty())
            {
                throw new ArithmeticException();
            }
            return (double) this.sum() / (double) this.size();
        }

        public double median()
        {
            if (this.isEmpty())
            {
                throw new ArithmeticException();
            }
            short[] sortedArray = this.toSortedArray();
            int middleIndex = sortedArray.length >> 1;
            if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
            {
                short first = sortedArray[middleIndex];
                short second = sortedArray[middleIndex - 1];
                return ((double) first + (double) second) / 2.0;
            }
            return (double) sortedArray[middleIndex];
        }

        public short[] toSortedArray()
        {
            short[] array = this.toArray();
            Arrays.sort(array);
            return array;
        }

        public MutableShortList toSortedList()
        {
            return ShortArrayList.newList(this).sortThis();
        }

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

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

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

    @Override
    public MutableShortList toList()
    {
        return ShortArrayList.newList(this);
    }

    @Override
    public MutableShortSet toSet()
    {
        return ShortHashSet.newSet(this);
    }

    @Override
    public MutableShortBag toBag()
    {
        return ShortHashBag.newBag(this);
    }

    private static final class CountShortProcedure implements ShortProcedure
    {
        private static final long serialVersionUID = 1L;
        private final ShortPredicate predicate;
        private int counter = 0;

        private CountShortProcedure(ShortPredicate predicate)
        {
            this.predicate = predicate;
        }

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

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

    private final class IfShortProcedure implements ShortProcedure
    {
        private static final long serialVersionUID = 1L;
        private final ShortProcedure procedure;

        private IfShortProcedure(ShortProcedure procedure)
        {
            this.procedure = procedure;
        }

        public void value(short each)
        {
            if (SelectShortIterable.this.predicate.accept(each))
            {
                this.procedure.value(each);
            }
        }
    }

    private static final class SelectShortIterator
            implements ShortIterator
    {
        private final ShortIterator iterator;
        private final ShortPredicate predicate;
        private short next;
        private boolean verifiedHasNext = false;

        private SelectShortIterator(ShortIterable iterable, ShortPredicate predicate)
        {
            this(iterable.shortIterator(), predicate);
        }

        private SelectShortIterator(ShortIterator iterator, ShortPredicate predicate)
        {
            this.iterator = iterator;
            this.predicate = predicate;
        }

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

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

Related Classes of com.gs.collections.impl.lazy.primitive.SelectShortIterable$CountShortProcedure

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.