/*
 * Copyright (c) 2018 Goldman Sachs and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.list.immutable.primitive;

import java.io.IOException;
import java.io.Serializable;

import org.eclipse.collections.api.ShortIterable;
import org.eclipse.collections.api.LazyShortIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.block.function.primitive.ShortToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectShortToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectShortIntToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.ShortPredicate;
import org.eclipse.collections.api.block.procedure.primitive.ShortProcedure;
import org.eclipse.collections.api.block.procedure.primitive.ShortIntProcedure;
import org.eclipse.collections.api.iterator.ShortIterator;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.primitive.ImmutableShortList;
import org.eclipse.collections.api.list.primitive.ShortList;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.set.primitive.MutableShortSet;
import org.eclipse.collections.api.tuple.primitive.ShortShortPair;
import org.eclipse.collections.api.tuple.primitive.ShortObjectPair;
import org.eclipse.collections.impl.bag.mutable.primitive.ShortHashBag;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.primitive.ShortLists;
import org.eclipse.collections.impl.iterator.UnmodifiableShortIterator;
import org.eclipse.collections.impl.lazy.primitive.LazyShortIterableAdapter;
import org.eclipse.collections.impl.lazy.primitive.ReverseShortIterable;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.list.mutable.primitive.ShortArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.ShortHashSet;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;

ImmutableShortSingletonList is an optimization for ImmutableShortList of size 1. This file was automatically generated from template file immutablePrimitiveSingletonList.stg.
/** * ImmutableShortSingletonList is an optimization for {@link ImmutableShortList} of size 1. * This file was automatically generated from template file immutablePrimitiveSingletonList.stg. */
final class ImmutableShortSingletonList implements ImmutableShortList, Serializable { private static final long serialVersionUID = 1L; private final short element1; ImmutableShortSingletonList(short element) { this.element1 = element; } @Override public short get(int index) { if (index == 0) { return this.element1; } throw new IndexOutOfBoundsException("Index: " + index + ", Size: 1"); } @Override public short getFirst() { return this.element1; } @Override public short getLast() { return this.element1; } @Override public int indexOf(short value) { return this.element1 == value ? 0 : -1; } @Override public int lastIndexOf(short value) { return this.element1 == value ? 0 : -1; } @Override public ShortIterator shortIterator() { return new UnmodifiableShortIterator(ShortArrayList.newListWith(this.element1).shortIterator()); } @Override public void forEach(ShortProcedure procedure) { this.each(procedure); }
Since:7.0.
/** * @since 7.0. */
@Override public void each(ShortProcedure procedure) { procedure.value(this.element1); } @Override public void forEachWithIndex(ShortIntProcedure procedure) { procedure.value(this.element1, 0); } @Override public int count(ShortPredicate predicate) { return predicate.accept(this.element1) ? 1 : 0; } @Override public boolean anySatisfy(ShortPredicate predicate) { return predicate.accept(this.element1); } @Override public boolean allSatisfy(ShortPredicate predicate) { return predicate.accept(this.element1); } @Override public boolean noneSatisfy(ShortPredicate predicate) { return !predicate.accept(this.element1); } @Override public ImmutableShortList select(ShortPredicate predicate) { return predicate.accept(this.element1) ? ShortArrayList.newListWith(this.element1).toImmutable() : new ShortArrayList().toImmutable(); } @Override public ImmutableShortList reject(ShortPredicate predicate) { return predicate.accept(this.element1) ? new ShortArrayList().toImmutable() : ShortArrayList.newListWith(this.element1).toImmutable(); } @Override public short detectIfNone(ShortPredicate predicate, short ifNone) { return predicate.accept(this.element1) ? this.element1 : ifNone; } @Override public <V> ImmutableList<V> collect(ShortToObjectFunction<? extends V> function) { return FastList.newListWith(function.valueOf(this.element1)).toImmutable(); } @Override public long sum() { return this.element1; } @Override public short max() { return this.element1; } @Override public short maxIfEmpty(short defaultValue) { return this.element1; } @Override public short min() { return this.element1; } @Override public short minIfEmpty(short defaultValue) { return this.element1; } @Override public double average() { return this.element1; } @Override public double median() { return this.element1; } @Override public short[] toSortedArray() { return new short[]{this.element1}; } @Override public MutableShortList toSortedList() { return ShortArrayList.newListWith(this.element1); } @Override public int binarySearch(short value) { if (this.element1 == value) { return 0; } if (this.element1 < value) { return -2; } return -1; } @Override public long dotProduct(ShortList list) { if (list.size() != 1) { throw new IllegalArgumentException("Lists used in dotProduct must be the same size"); } return (long) this.element1 * list.getFirst(); } @Override public short[] toArray() { return new short[]{this.element1}; } @Override public boolean contains(short value) { return this.element1 == value; } @Override public boolean containsAll(short... source) { for (short value : source) { if (this.element1 != value) { return false; } } return true; } @Override public boolean containsAll(ShortIterable source) { for (ShortIterator iterator = source.shortIterator(); iterator.hasNext(); ) { if (this.element1 != iterator.next()) { return false; } } return true; } @Override public LazyShortIterable asReversed() { return ReverseShortIterable.adapt(this); } @Override public MutableShortList toList() { return ShortArrayList.newListWith(this.element1); } @Override public MutableShortSet toSet() { return ShortHashSet.newSetWith(this.element1); } @Override public MutableShortBag toBag() { return ShortHashBag.newBagWith(this.element1); } @Override public LazyShortIterable asLazy() { return new LazyShortIterableAdapter(this); } @Override public ImmutableShortList toImmutable() { return this; } @Override public ImmutableShortSingletonList toReversed() { return this; } @Override public ImmutableShortList newWith(short element) { return ShortLists.immutable.with(this.element1, element); } @Override public ImmutableShortList newWithout(short element) { return this.element1 == element ? ShortLists.immutable.with() : this; } @Override public ImmutableShortList newWithAll(ShortIterable elements) { ShortArrayList arrayList = ShortArrayList.newListWith(this.element1); arrayList.addAll(elements); return arrayList.toImmutable(); } @Override public ImmutableShortList newWithoutAll(ShortIterable elements) { return elements.contains(this.element1) ? ShortLists.immutable.with() : this; } @Override public int size() { return 1; } @Override public boolean isEmpty() { return false; } @Override public boolean notEmpty() { return true; } @Override public <T> T injectInto(T injectedValue, ObjectShortToObjectFunction<? super T, ? extends T> function) { return function.valueOf(injectedValue, this.element1); } @Override public <T> T injectIntoWithIndex(T injectedValue, ObjectShortIntToObjectFunction<? super T, ? extends T> function) { return function.valueOf(injectedValue, this.element1, 0); } @Override public RichIterable<ShortIterable> chunk(int size) { if (size <= 0) { throw new IllegalArgumentException("Size for groups must be positive but was: " + size); } return Lists.immutable.with(this); } @Override public boolean equals(Object otherList) { if (otherList == this) { return true; } if (!(otherList instanceof ShortList)) { return false; } ShortList list = (ShortList) otherList; if (list.size() != 1) { return false; } return this.element1 == list.get(0); } @Override public int hashCode() { return 31 + (int) this.element1; } @Override public String toString() { return "[" + this.element1 + ']'; } @Override public String makeString() { return this.makeString(", "); } @Override public String makeString(String separator) { return this.makeString("", separator, ""); } @Override public String makeString(String start, String separator, String end) { Appendable stringBuilder = new StringBuilder(); this.appendString(stringBuilder, start, separator, end); return stringBuilder.toString(); } @Override public void appendString(Appendable appendable) { this.appendString(appendable, ", "); } @Override public void appendString(Appendable appendable, String separator) { this.appendString(appendable, "", separator, ""); } @Override public void appendString(Appendable appendable, String start, String separator, String end) { try { appendable.append(start); appendable.append(String.valueOf(this.element1)); appendable.append(end); } catch (IOException e) { throw new RuntimeException(e); } }
Since:6.0.
/** * @since 6.0. */
@Override public ImmutableShortList distinct() { return this; } @Override public ImmutableShortList subList(int fromIndex, int toIndex) { throw new UnsupportedOperationException("subList not yet implemented!"); }
Since:9.1.
/** * @since 9.1. */
@Override public ImmutableList<ShortShortPair> zipShort(ShortIterable iterable) { if (iterable.isEmpty()) { return Lists.immutable.empty(); } return Lists.immutable.with(PrimitiveTuples.pair(this.element1, iterable.shortIterator().next())); }
Since:9.1.
/** * @since 9.1. */
@Override public <T> ImmutableList<ShortObjectPair<T>> zip(Iterable<T> iterable) { if (Iterate.isEmpty(iterable)) { return Lists.immutable.empty(); } return Lists.immutable.with(PrimitiveTuples.pair(this.element1, Iterate.getFirst(iterable))); } }