Top Description Fields Constructors Methods
org.pcollections

public final Class ConsPStack<E>

extends AbstractUnmodifiableSequentialList<E>
implements PStack<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.pcollections.PStack, org.pcollections.PSequence, java.util.List, java.util.SequencedCollection, java.util.Collection, java.lang.Iterable, org.pcollections.PCollection
Static Imports
java.util.Objects.requireNonNull

A simple persistent stack.

This implementation is thread-safe (assuming Java's AbstractSequentialList is thread-safe), although its iterators may not be.

Author
harold

Field Summary

Modifier and TypeField and Description
private static final ConsPStack<Object>
private final E
private final ConsPStack<E>
private static final long
private final int

Constructor Summary

AccessConstructor and Description
private
private
ConsPStack(final E first, final ConsPStack<E> rest)

Method Summary

Modifier and TypeMethod and Description
public static <E> ConsPStack<E>

Returns:

an empty stack
empty
()

public static <E> ConsPStack<E>

Returns:

a stack consisting of the elements of list in the order of list.iterator()
from
(final Collection<? extends E> list)

private static <E> ConsPStack<E>
from(final Iterator<? extends E> i)

public ListIterator<E>
listIterator(final int
index of the first element to be returned from the list iterator (by a call to next)
index
)

Implements abstract java.util.AbstractSequentialList.listIterator.

Implements java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

public ConsPStack<E>
minus(final Object e)

Implements org.pcollections.PStack.minus.

Returns a sequence consisting of the elements of this without the first occurrence of e.

public ConsPStack<E>
minus(final int i)

Implements org.pcollections.PStack.minus.

public ConsPStack<E>
public ConsPStack<E>
plus(final E e)

Implements org.pcollections.PStack.plus.

Returns a stack consisting of the elements of this with e prepended.

public ConsPStack<E>
plus(final int i, final E e)

Implements org.pcollections.PStack.plus.

public ConsPStack<E>
plusAll(final Collection<? extends E> list)

Implements org.pcollections.PStack.plusAll.

Returns a stack consisting of the elements of this with list prepended in reverse.

public ConsPStack<E>
plusAll(final int i, final Collection<? extends E> list)

Implements org.pcollections.PStack.plusAll.

public static <E> ConsPStack<E>

Returns:

empty().plus(e)
singleton
(final E e)

public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size, java.util.List.size.

Returns the number of elements in this list.

public ConsPStack<E>
subList(final int
low endpoint (inclusive) of the subList
start
,
final int
high endpoint (exclusive) of the subList
end
)

Overrides java.util.AbstractList.subList.

Implements org.pcollections.PStack.subList, java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

public ConsPStack<E>
public ConsPStack<E>
with(final int i, final E e)

Implements org.pcollections.PStack.with.

Inherited from org.pcollections.AbstractUnmodifiableSequentialList:
addaddaddAlladdAllclearremoveremoveremoveAllremoveIfreplaceAllretainAllsetsort

Field Detail

EMPTYback to summary
private static final ConsPStack<Object> EMPTY
firstback to summary
private final E first
restback to summary
private final ConsPStack<E> rest
serialVersionUIDback to summary
private static final long serialVersionUID
sizeback to summary
private final int size

Constructor Detail

ConsPStackback to summary
private ConsPStack()
ConsPStackback to summary
private ConsPStack(final E first, final ConsPStack<E> rest)

Method Detail

emptyback to summary
public static <E> ConsPStack<E> empty()
Returns:ConsPStack<E>

an empty stack

Annotations
@SuppressWarnings:unchecked
fromback to summary
public static <E> ConsPStack<E> from(final Collection<? extends E> list)
Returns:ConsPStack<E>

a stack consisting of the elements of list in the order of list.iterator()

Annotations
@SuppressWarnings:unchecked
fromback to summary
private static <E> ConsPStack<E> from(final Iterator<? extends E> i)
listIteratorback to summary
public ListIterator<E> listIterator(final int index)

Implements abstract java.util.AbstractSequentialList.listIterator.

Implements java.util.List.listIterator.

Doc from java.util.List.listIterator.

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.

Parameters
index:int

index of the first element to be returned from the list iterator (by a call to next)

Returns:ListIterator<E>

a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list

Annotations
@Override
minusback to summary
public ConsPStack<E> minus(final Object e)

Implements org.pcollections.PStack.minus.

Doc from org.pcollections.PSequence.minus.

Returns a sequence consisting of the elements of this without the first occurrence of e.

Returns:ConsPStack<E>

Doc from org.pcollections.PCollection.minus.

this with a single instance of e removed, if e is in this

minusback to summary
public ConsPStack<E> minus(final int i)

Implements org.pcollections.PStack.minus.

Returns:ConsPStack<E>

Doc from org.pcollections.PSequence.minus.

a sequence consisting of the elements of this with the element at index i removed.

minusAllback to summary
public ConsPStack<E> minusAll(final Collection<?> list)

Implements org.pcollections.PStack.minusAll.

Returns:ConsPStack<E>

Doc from org.pcollections.PCollection.minusAll.

this with all elements of list completely removed

plusback to summary
public ConsPStack<E> plus(final E e)

Implements org.pcollections.PStack.plus.

Doc from org.pcollections.PStack.plus.

Returns a stack consisting of the elements of this with e prepended.

Returns:ConsPStack<E>

Doc from org.pcollections.PCollection.plus.

a collection which contains e and all of the elements of this

plusback to summary
public ConsPStack<E> plus(final int i, final E e)

Implements org.pcollections.PStack.plus.

Returns:ConsPStack<E>

Doc from org.pcollections.PSequence.plus.

a sequence consisting of the elements of this with e inserted at index i.

plusAllback to summary
public ConsPStack<E> plusAll(final Collection<? extends E> list)

Implements org.pcollections.PStack.plusAll.

Doc from org.pcollections.PStack.plusAll.

Returns a stack consisting of the elements of this with list prepended in reverse.

Returns:ConsPStack<E>

Doc from org.pcollections.PCollection.plusAll.

a collection which contains all of the elements of list and this

plusAllback to summary
public ConsPStack<E> plusAll(final int i, final Collection<? extends E> list)

Implements org.pcollections.PStack.plusAll.

Returns:ConsPStack<E>

Doc from org.pcollections.PSequence.plusAll.

a sequence consisting of the elements of this with list inserted at index i.

singletonback to summary
public static <E> ConsPStack<E> singleton(final E e)
Returns:ConsPStack<E>

empty().plus(e)

sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size, java.util.List.size.

Doc from java.util.List.size.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this list

Annotations
@Override
subListback to summary
public ConsPStack<E> subList(final int start, final int end)

Overrides java.util.AbstractList.subList.

Implements org.pcollections.PStack.subList, java.util.List.subList.

Doc from java.util.List.subList.

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

list.subList(from, to).clear();
Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Parameters
start:int

low endpoint (inclusive) of the subList

end:int

high endpoint (exclusive) of the subList

Returns:ConsPStack<E>

a view of the specified range within this list

Annotations
@Override
subListback to summary
public ConsPStack<E> subList(int start)

Implements org.pcollections.PStack.subList.

Returns:ConsPStack<E>

Doc from org.pcollections.PStack.subList.

subList(start,this.size())

withback to summary
public ConsPStack<E> with(final int i, final E e)

Implements org.pcollections.PStack.with.

Returns:ConsPStack<E>

Doc from org.pcollections.PSequence.with.

a sequence consisting of the elements of this with e replacing the element at index i.