Top Description Inners Fields Constructors Methods
com.google.common.collect

public final Class MoreCollectors

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, java.util.Collections.emptyList

Collectors not present in java.util.stream.Collectors that are not otherwise associated with a com.google.common type.
Author
Louis Wasserman
Since
21.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
MoreCollectors.ToOptionalState

This atrocity is here to let us report several of the elements in the stream if there were more than one, not just two.

Field Summary

Modifier and TypeField and Description
private static final Object
private static final Collector<@Nullable Object, ?, @Nullable Object>
private static final Collector<Object, ?, Optional<Object>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <T extends @Nullable Object> Collector<T, ?, T>
onlyElement()

A collector that takes a stream containing exactly one element and returns that element.

public static <T> Collector<T, ?, Optional<T>>

Returns:

Optional.of(onlyElement) if the stream has exactly one element (must not be null) and returns Optional.empty() if it has none.
toOptional
()

A collector that converts a stream of zero or one elements to an Optional.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NULL_PLACEHOLDERback to summary
private static final Object NULL_PLACEHOLDER
ONLY_ELEMENTback to summary
private static final Collector<@Nullable Object, ?, @Nullable Object> ONLY_ELEMENT
TO_OPTIONALback to summary
private static final Collector<Object, ?, Optional<Object>> TO_OPTIONAL

Constructor Detail

MoreCollectorsback to summary
private MoreCollectors()

Method Detail

onlyElementback to summary
public static <T extends @Nullable Object> Collector<T, ?, T> onlyElement()

A collector that takes a stream containing exactly one element and returns that element. The returned collector throws an IllegalArgumentException if the stream consists of two or more elements, and a NoSuchElementException if the stream is empty.

Annotations
@SuppressWarnings:unchecked
toOptionalback to summary
public static <T> Collector<T, ?, Optional<T>> toOptional()

A collector that converts a stream of zero or one elements to an Optional.

Returns:Collector<T, ?, Optional<T>>

Optional.of(onlyElement) if the stream has exactly one element (must not be null) and returns Optional.empty() if it has none.

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the stream consists of two or more elements.
NullPointerException:
if any element in the stream is null.
com.google.common.collect back to summary

private final Class MoreCollectors.ToOptionalState

extends Object
Class Inheritance

This atrocity is here to let us report several of the elements in the stream if there were more than one, not just two.

Field Summary

Modifier and TypeField and Description
pack-priv Object
pack-priv List<Object>
pack-priv static final int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
pack-priv MoreCollectors.ToOptionalState
pack-priv Object
pack-priv Optional<Object>
pack-priv IllegalArgumentException
multiples(boolean overflow)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

elementback to summary
pack-priv Object element
Annotations
@CheckForNull
extrasback to summary
pack-priv List<Object> extras
MAX_EXTRASback to summary
pack-priv static final int MAX_EXTRAS

Constructor Detail

ToOptionalStateback to summary
pack-priv ToOptionalState()

Method Detail

addback to summary
pack-priv void add(Object o)
combineback to summary
pack-priv MoreCollectors.ToOptionalState combine(MoreCollectors.ToOptionalState other)
getElementback to summary
pack-priv Object getElement()
getOptionalback to summary
pack-priv Optional<Object> getOptional()
multiplesback to summary
pack-priv IllegalArgumentException multiples(boolean overflow)