Package com.gs.collections.impl.list.immutable.primitive

Source Code of com.gs.collections.impl.list.immutable.primitive.ImmutableDoubleArrayList

/*
* 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.list.immutable.primitive;

import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.NoSuchElementException;

import com.gs.collections.api.DoubleIterable;
import com.gs.collections.api.LazyDoubleIterable;
import com.gs.collections.api.bag.primitive.MutableDoubleBag;
import com.gs.collections.api.block.function.primitive.DoubleToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectDoubleToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectDoubleIntToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.DoublePredicate;
import com.gs.collections.api.block.procedure.primitive.DoubleProcedure;
import com.gs.collections.api.block.procedure.primitive.DoubleIntProcedure;
import com.gs.collections.api.iterator.DoubleIterator;
import com.gs.collections.api.list.ImmutableList;
import com.gs.collections.api.list.primitive.ImmutableDoubleList;
import com.gs.collections.api.list.primitive.DoubleList;
import com.gs.collections.api.list.primitive.MutableDoubleList;
import com.gs.collections.api.set.primitive.MutableDoubleSet;
import com.gs.collections.impl.bag.mutable.primitive.DoubleHashBag;
import com.gs.collections.impl.factory.primitive.DoubleLists;
import com.gs.collections.impl.lazy.primitive.LazyDoubleIterableAdapter;
import com.gs.collections.impl.lazy.primitive.ReverseDoubleIterable;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
import com.gs.collections.impl.set.mutable.primitive.DoubleHashSet;
import net.jcip.annotations.Immutable;

/**
* ImmutableDoubleArrayList is the non-modifiable equivalent of {@link DoubleArrayList}.
* It wraps a Java double array.
* This file was automatically generated from template file immutablePrimitiveArrayList.stg.
*
* @since 3.2.
*/
@Immutable
final class ImmutableDoubleArrayList
        implements ImmutableDoubleList, Serializable
{
    private static final long serialVersionUID = 1L;
    private final double[] items;

    private ImmutableDoubleArrayList(double[] newElements)
    {
        if (newElements.length <= 1)
        {
            throw new IllegalArgumentException("Use DoubleLists.immutable.with() to instantiate an optimized collection");
        }
        this.items = newElements;
    }

    public static ImmutableDoubleArrayList newList(DoubleIterable iterable)
    {
        return new ImmutableDoubleArrayList(iterable.toArray());
    }

    public static ImmutableDoubleArrayList newListWith(double... elements)
    {
        double[] newArray = new double[elements.length];
        System.arraycopy(elements, 0, newArray, 0, elements.length);
        return new ImmutableDoubleArrayList(newArray);
    }

    public double get(int index)
    {
        return this.items[index];
    }

    public double getFirst()
    {
        return this.items[0];
    }

    public double getLast()
    {
        return this.items[this.items.length - 1];
    }

    public int indexOf(double value)
    {
        for (int i = 0; i < this.items.length; i++)
        {
            if (Double.compare(this.items[i], value) == 0)
            {
                return i;
            }
        }
        return -1;
    }

    public int lastIndexOf(double value)
    {
        for (int i = this.items.length - 1; i >= 0; i--)
        {
            if (Double.compare(this.items[i], value) == 0)
            {
                return i;
            }
        }
        return -1;
    }

    public DoubleIterator doubleIterator()
    {
        return new InternalDoubleIterator();
    }

    public void forEach(DoubleProcedure procedure)
    {
        for (double item : this.items)
        {
            procedure.value(item);
        }
    }

    public void forEachWithIndex(DoubleIntProcedure procedure)
    {
        for (int i = 0; i < this.items.length; i++)
        {
            procedure.value(this.items[i], i);
        }
    }

    public int count(DoublePredicate predicate)
    {
        int count = 0;
        for (double item : this.items)
        {
            if (predicate.accept(item))
            {
                count++;
            }
        }
        return count;
    }

    public boolean anySatisfy(DoublePredicate predicate)
    {
        for (double item : this.items)
        {
            if (predicate.accept(item))
            {
                return true;
            }
        }
        return false;
    }

    public boolean allSatisfy(DoublePredicate predicate)
    {
        for (double item : this.items)
        {
            if (!predicate.accept(item))
            {
                return false;
            }
        }
        return true;
    }

    public boolean noneSatisfy(DoublePredicate predicate)
    {
        for (double item : this.items)
        {
            if (predicate.accept(item))
            {
                return false;
            }
        }
        return true;
    }

    public ImmutableDoubleList select(DoublePredicate predicate)
    {
        DoubleArrayList result = new DoubleArrayList();
        for (double item : this.items)
        {
            if (predicate.accept(item))
            {
                result.add(item);
            }
        }
        return result.toImmutable();
    }

    public ImmutableDoubleList reject(DoublePredicate predicate)
    {
        DoubleArrayList result = new DoubleArrayList();
        for (double item : this.items)
        {
            if (!predicate.accept(item))
            {
                result.add(item);
            }
        }
        return result.toImmutable();
    }

    public double detectIfNone(DoublePredicate predicate, double ifNone)
    {
        for (double item : this.items)
        {
            if (predicate.accept(item))
            {
                return item;
            }
        }
        return ifNone;
    }

    public <V> ImmutableList<V> collect(DoubleToObjectFunction<? extends V> function)
    {
        FastList<V> target = FastList.newList(this.items.length);
        for (double item : this.items)
        {
            target.add(function.valueOf(item));
        }
        return target.toImmutable();
    }

    public double sum()
    {
        double result = 0.0;
        for (double item : this.items)
        {
            result += item;
        }
        return result;
    }

    public double max()
    {
        double max = this.items[0];
        for (int i = 1; i < this.items.length; i++)
        {
            double value = this.items[i];
            if (Double.compare(max, value) < 0)
            {
                max = value;
            }
        }
        return max;
    }

    public double maxIfEmpty(double defaultValue)
    {
        return this.max();
    }

    public double min()
    {
        double min = this.items[0];
        for (int i = 1; i < this.items.length; i++)
        {
            double value = this.items[i];
            if (Double.compare(value, min) < 0)
            {
                min = value;
            }
        }
        return min;
    }

    public double minIfEmpty(double defaultValue)
    {
        return this.min();
    }

    public double average()
    {
        return this.sum() / (double) this.size();
    }

    public double median()
    {
        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 (first + second) / 2.0;
        }
        return sortedArray[middleIndex];
    }

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

    public double dotProduct(DoubleList list)
    {
        if (this.size() != list.size())
        {
            throw new IllegalArgumentException("Lists used in dotProduct must be the same size");
        }
        double sum = 0.0;
        for (int i = 0; i < this.size(); i++)
        {
            sum += this.items[i] * list.get(i);
        }
        return sum;
    }

    public LazyDoubleIterable asReversed()
    {
        return ReverseDoubleIterable.adapt(this);
    }

    public MutableDoubleList toSortedList()
    {
        return DoubleArrayList.newList(this).sortThis();
    }

    public double[] toArray()
    {
        double[] newItems = new double[this.items.length];
        System.arraycopy(this.items, 0, newItems, 0, this.items.length);
        return newItems;
    }

    public boolean contains(double value)
    {
        for (double item : this.items)
        {
            if (Double.compare(item, value) == 0)
            {
                return true;
            }
        }
        return false;
    }

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

    public boolean containsAll(DoubleIterable source)
    {
        for (DoubleIterator iterator = source.doubleIterator(); iterator.hasNext(); )
        {
            if (!this.contains(iterator.next()))
            {
                return false;
            }
        }
        return true;
    }

    public MutableDoubleList toList()
    {
        return DoubleArrayList.newList(this);
    }

    public MutableDoubleSet toSet()
    {
        return DoubleHashSet.newSet(this);
    }

    public MutableDoubleBag toBag()
    {
        return DoubleHashBag.newBag(this);
    }

    public LazyDoubleIterable asLazy()
    {
        return new LazyDoubleIterableAdapter(this);
    }

    public ImmutableDoubleList toImmutable()
    {
        return this;
    }

    public ImmutableDoubleArrayList toReversed()
    {
        return ImmutableDoubleArrayList.newList(this.asReversed());
    }

    public ImmutableDoubleList newWith(double element)
    {
        double[] newItems = new double[this.items.length + 1];
        System.arraycopy(this.items, 0, newItems, 0, this.items.length);
        newItems[this.items.length] = element;
        return new ImmutableDoubleArrayList(newItems);
    }

    public ImmutableDoubleList newWithout(double element)
    {
        int index = this.indexOf(element);
        if (index != -1)
        {
            double[] newItems = new double[this.items.length - 1];
            System.arraycopy(this.items, 0, newItems, 0, index);
            System.arraycopy(this.items, index + 1, newItems, index, this.items.length - index - 1);
            return DoubleLists.immutable.with(newItems);
        }
        return this;
    }

    public ImmutableDoubleList newWithAll(DoubleIterable elements)
    {
        double[] newItems = new double[this.items.length + elements.size()];
        System.arraycopy(this.items, 0, newItems, 0, this.items.length);
        int index = 0;
        for (DoubleIterator iterator = elements.doubleIterator(); iterator.hasNext(); index++)
        {
            newItems[this.items.length + index] = iterator.next();
        }
        return new ImmutableDoubleArrayList(newItems);
    }

    public ImmutableDoubleList newWithoutAll(DoubleIterable elements)
    {
        MutableDoubleList mutableDoubleList = this.toList();
        mutableDoubleList.removeAll(elements);
        return mutableDoubleList.toImmutable();
    }

    public int size()
    {
        return this.items.length;
    }

    public boolean isEmpty()
    {
        return false;
    }

    public boolean notEmpty()
    {
        return true;
    }

    public <T> T injectInto(T injectedValue, ObjectDoubleToObjectFunction<? super T, ? extends T> function)
    {
        T result = injectedValue;
        for (int i = 0; i < this.items.length; i++)
        {
            result = function.valueOf(result, this.items[i]);
        }
        return result;
    }

    public <T> T injectIntoWithIndex(T injectedValue, ObjectDoubleIntToObjectFunction<? super T, ? extends T> function)
    {
        T result = injectedValue;
        for (int i = 0; i < this.items.length; i++)
        {
            result = function.valueOf(result, this.items[i], i);
        }
        return result;
    }

    @Override
    public boolean equals(Object otherList)
    {
        if (otherList == this)
        {
            return true;
        }
        if (!(otherList instanceof DoubleList))
        {
            return false;
        }
        DoubleList list = (DoubleList) otherList;
        if (this.items.length != list.size())
        {
            return false;
        }
        for (int i = 0; i < this.items.length; i++)
        {
            if (Double.compare(this.items[i], list.get(i)) != 0)
            {
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        int hashCode = 1;
        for (double item : this.items)
        {
            hashCode = 31 * hashCode + (int) (Double.doubleToLongBits(item) ^ Double.doubleToLongBits(item) >>> 32);
        }
        return hashCode;
    }

    @Override
    public String toString()
    {
        return this.makeString("[", ", ", "]");
    }

    public String makeString()
    {
        return this.makeString(", ");
    }

    public String makeString(String separator)
    {
        return this.makeString("", separator, "");
    }

    public String makeString(String start, String separator, String end)
    {
        Appendable stringBuilder = new StringBuilder();
        this.appendString(stringBuilder, start, separator, end);
        return stringBuilder.toString();
    }

    public void appendString(Appendable appendable)
    {
        this.appendString(appendable, ", ");
    }

    public void appendString(Appendable appendable, String separator)
    {
        this.appendString(appendable, "", separator, "");
    }

    public void appendString(Appendable appendable, String start, String separator, String end)
    {
        try
        {
            appendable.append(start);
            for (int i = 0; i < this.items.length; i++)
            {
                if (i > 0)
                {
                    appendable.append(separator);
                }
                double value = this.items[i];
                appendable.append(String.valueOf(value));
            }
            appendable.append(end);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }

    public ImmutableDoubleList subList(int fromIndex, int toIndex)
    {
        throw new UnsupportedOperationException("subList not yet implemented!");
    }

    private class InternalDoubleIterator implements DoubleIterator
    {
        /**
         * Index of element to be returned by subsequent call to next.
         */
        private int currentIndex;

        public boolean hasNext()
        {
            return this.currentIndex != ImmutableDoubleArrayList.this.items.length;
        }

        public double next()
        {
            if (!this.hasNext())
            {
                throw new NoSuchElementException();
            }
            double next = ImmutableDoubleArrayList.this.items[this.currentIndex];
            this.currentIndex++;
            return next;
        }
    }
}
TOP

Related Classes of com.gs.collections.impl.list.immutable.primitive.ImmutableDoubleArrayList

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.