package org.eclipse.collections.impl.primitive;
import java.util.Arrays;
import org.eclipse.collections.api.DoubleIterable;
import org.eclipse.collections.api.LazyDoubleIterable;
import org.eclipse.collections.api.bag.primitive.MutableDoubleBag;
import org.eclipse.collections.api.list.primitive.MutableDoubleList;
import org.eclipse.collections.api.set.primitive.MutableDoubleSet;
import org.eclipse.collections.impl.bag.mutable.primitive.DoubleHashBag;
import org.eclipse.collections.impl.lazy.primitive.LazyDoubleIterableAdapter;
import org.eclipse.collections.impl.list.mutable.primitive.DoubleArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.DoubleHashSet;
public abstract class AbstractDoubleIterable implements DoubleIterable
{
@Override
public String toString()
{
return this.makeString("[", ", ", "]");
}
@Override
public double minIfEmpty(double defaultValue)
{
if (this.isEmpty())
{
return defaultValue;
}
return this.min();
}
@Override
public double maxIfEmpty(double defaultValue)
{
if (this.isEmpty())
{
return defaultValue;
}
return this.max();
}
@Override
public double average()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
return (double) this.sum() / (double) this.size();
}
@Override
public double median()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
double[] sortedArray = this.toSortedArray();
int middleIndex = sortedArray.length >> 1;
if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
{
double first = sortedArray[middleIndex];
double second = sortedArray[middleIndex - 1];
return ((double) first + (double) second) / 2.0;
}
return (double) sortedArray[middleIndex];
}
@Override
public double[] toSortedArray()
{
double[] array = this.toArray();
Arrays.sort(array);
return array;
}
@Override
public MutableDoubleList toSortedList()
{
return this.toList().sortThis();
}
@Override
public LazyDoubleIterable asLazy()
{
return new LazyDoubleIterableAdapter(this);
}
@Override
public MutableDoubleList toList()
{
return DoubleArrayList.newList(this);
}
@Override
public MutableDoubleSet toSet()
{
return DoubleHashSet.newSet(this);
}
@Override
public MutableDoubleBag toBag()
{
return DoubleHashBag.newBag(this);
}
@Override
public boolean containsAll(double... source)
{
for (double item : source)
{
if (!this.contains(item))
{
return false;
}
}
return true;
}
@Override
public boolean containsAll(DoubleIterable source)
{
return source.allSatisfy(this::contains);
}
}