package org.eclipse.collections.impl.primitive;
import java.util.Arrays;
import org.eclipse.collections.api.ShortIterable;
import org.eclipse.collections.api.LazyShortIterable;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.set.primitive.MutableShortSet;
import org.eclipse.collections.impl.bag.mutable.primitive.ShortHashBag;
import org.eclipse.collections.impl.lazy.primitive.LazyShortIterableAdapter;
import org.eclipse.collections.impl.list.mutable.primitive.ShortArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.ShortHashSet;
public abstract class AbstractShortIterable implements ShortIterable
{
@Override
public String toString()
{
return this.makeString("[", ", ", "]");
}
@Override
public short minIfEmpty(short defaultValue)
{
if (this.isEmpty())
{
return defaultValue;
}
return this.min();
}
@Override
public short maxIfEmpty(short 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();
}
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];
}
@Override
public short[] toSortedArray()
{
short[] array = this.toArray();
Arrays.sort(array);
return array;
}
@Override
public MutableShortList toSortedList()
{
return this.toList().sortThis();
}
@Override
public LazyShortIterable asLazy()
{
return new LazyShortIterableAdapter(this);
}
@Override
public MutableShortList toList()
{
return ShortArrayList.newList(this);
}
@Override
public MutableShortSet toSet()
{
return ShortHashSet.newSet(this);
}
@Override
public MutableShortBag toBag()
{
return ShortHashBag.newBag(this);
}
}