com.gs.collections.impl.lazy.primitive.SelectFloatIterable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gs-collections Show documentation
Show all versions of gs-collections Show documentation
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.
/*
* 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.FloatIterable;
import com.gs.collections.api.LazyFloatIterable;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.block.function.primitive.FloatToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectFloatToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.FloatPredicate;
import com.gs.collections.api.block.procedure.primitive.FloatProcedure;
import com.gs.collections.api.iterator.FloatIterator;
import com.gs.collections.api.bag.primitive.MutableFloatBag;
import com.gs.collections.api.list.primitive.MutableFloatList;
import com.gs.collections.api.set.primitive.MutableFloatSet;
import com.gs.collections.impl.bag.mutable.primitive.FloatHashBag;
import com.gs.collections.impl.set.mutable.primitive.FloatHashSet;
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
import com.gs.collections.impl.block.factory.primitive.FloatPredicates;
/**
* This file was automatically generated from template file selectPrimitiveIterable.stg.
*/
public class SelectFloatIterable
extends AbstractLazyFloatIterable
{
private final FloatIterable delegate;
private final FloatPredicate predicate;
public SelectFloatIterable(FloatIterable delegate, FloatPredicate predicate)
{
this.delegate = delegate;
this.predicate = predicate;
}
public FloatIterator floatIterator()
{
return new SelectFloatIterator(this.delegate, this.predicate);
}
public void forEach(FloatProcedure procedure)
{
this.delegate.forEach(new IfFloatProcedure(procedure));
}
@Override
public int size()
{
return this.delegate.count(this.predicate);
}
@Override
public boolean isEmpty()
{
return !this.floatIterator().hasNext();
}
@Override
public boolean notEmpty()
{
return this.floatIterator().hasNext();
}
@Override
public int count(FloatPredicate predicate)
{
CountFloatProcedure countFloatProcedure = new CountFloatProcedure(predicate);
this.forEach(countFloatProcedure);
return countFloatProcedure.getCount();
}
@Override
public boolean anySatisfy(FloatPredicate predicate)
{
return this.delegate.anySatisfy(FloatPredicates.and(this.predicate, predicate));
}
@Override
public boolean allSatisfy(FloatPredicate predicate)
{
return this.noneSatisfy(FloatPredicates.not(predicate));
}
@Override
public boolean noneSatisfy(FloatPredicate predicate)
{
return !this.anySatisfy(predicate);
}
public double sum()
{
double sum = 0.0;
for (FloatIterator floatIterator = this.floatIterator(); floatIterator.hasNext() ;)
{
sum += floatIterator.next();
}
return sum;
}
public float max()
{
FloatIterator floatIterator = this.floatIterator();
float max = floatIterator.next();
while (floatIterator.hasNext())
{
max = (float) Math.max(max, floatIterator.next());
}
return max;
}
public float min()
{
FloatIterator floatIterator = this.floatIterator();
float min = floatIterator.next();
while (floatIterator.hasNext())
{
min = (float) Math.min(min, floatIterator.next());
}
return min;
}
public float minIfEmpty(float defaultValue)
{
try
{
return this.min();
}
catch (NoSuchElementException ex)
{
}
return defaultValue;
}
public float maxIfEmpty(float defaultValue)
{
try
{
return this.max();
}
catch (NoSuchElementException ex)
{
}
return defaultValue;
}
public double average()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
return this.sum() / (double) this.size();
}
public double median()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
float[] sortedArray = this.toSortedArray();
int middleIndex = sortedArray.length >> 1;
if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
{
float first = sortedArray[middleIndex];
float second = sortedArray[middleIndex - 1];
return ((double) first + (double) second) / 2.0;
}
return (double) sortedArray[middleIndex];
}
public float[] toSortedArray()
{
float[] array = this.toArray();
Arrays.sort(array);
return array;
}
public MutableFloatList toSortedList()
{
return FloatArrayList.newList(this).sortThis();
}
@Override
public float[] toArray()
{
final float[] array = new float[this.size()];
this.forEach(new FloatProcedure()
{
@SuppressWarnings("FieldMayBeFinal")
private int index = 0;
public void value(float each)
{
array[this.index++] = each;
}
});
return array;
}
@Override
public boolean containsAll(float... source)
{
for (float value : source)
{
if (!this.contains(value))
{
return false;
}
}
return true;
}
@Override
public boolean containsAll(FloatIterable source)
{
for (FloatIterator iterator = source.floatIterator(); iterator.hasNext(); )
{
if (!this.contains(iterator.next()))
{
return false;
}
}
return true;
}
@Override
public MutableFloatList toList()
{
return FloatArrayList.newList(this);
}
@Override
public MutableFloatSet toSet()
{
return FloatHashSet.newSet(this);
}
@Override
public MutableFloatBag toBag()
{
return FloatHashBag.newBag(this);
}
private static final class CountFloatProcedure implements FloatProcedure
{
private static final long serialVersionUID = 1L;
private final FloatPredicate predicate;
private int counter = 0;
private CountFloatProcedure(FloatPredicate predicate)
{
this.predicate = predicate;
}
public void value(float each)
{
if (this.predicate.accept(each))
{
this.counter++;
}
}
public int getCount()
{
return this.counter;
}
}
private final class IfFloatProcedure implements FloatProcedure
{
private static final long serialVersionUID = 1L;
private final FloatProcedure procedure;
private IfFloatProcedure(FloatProcedure procedure)
{
this.procedure = procedure;
}
public void value(float each)
{
if (SelectFloatIterable.this.predicate.accept(each))
{
this.procedure.value(each);
}
}
}
private static final class SelectFloatIterator
implements FloatIterator
{
private final FloatIterator iterator;
private final FloatPredicate predicate;
private float next;
private boolean verifiedHasNext = false;
private SelectFloatIterator(FloatIterable iterable, FloatPredicate predicate)
{
this(iterable.floatIterator(), predicate);
}
private SelectFloatIterator(FloatIterator iterator, FloatPredicate predicate)
{
this.iterator = iterator;
this.predicate = predicate;
}
public boolean hasNext()
{
if (this.verifiedHasNext)
{
return true;
}
while (this.iterator.hasNext())
{
float temp = this.iterator.next();
if (this.predicate.accept(temp))
{
this.next = temp;
this.verifiedHasNext = true;
return true;
}
}
return false;
}
public float next()
{
if (this.verifiedHasNext || this.hasNext())
{
this.verifiedHasNext = false;
return this.next;
}
throw new NoSuchElementException();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy