001/*
002 * Copyright (C) 2016 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020import static java.util.Collections.emptyList;
021
022import com.google.common.annotations.GwtCompatible;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.NoSuchElementException;
026import java.util.Optional;
027import java.util.stream.Collector;
028import javax.annotation.CheckForNull;
029import org.checkerframework.checker.nullness.qual.Nullable;
030
031/**
032 * Collectors not present in {@code java.util.stream.Collectors} that are not otherwise associated
033 * with a {@code com.google.common} type.
034 *
035 * @author Louis Wasserman
036 * @since 33.2.0 (available since 21.0 in guava-jre)
037 */
038@GwtCompatible
039@SuppressWarnings("Java7ApiChecker")
040@IgnoreJRERequirement // Users will use this only if they're already using streams.
041public final class MoreCollectors {
042
043  /*
044   * TODO(lowasser): figure out if we can convert this to a concurrent AtomicReference-based
045   * collector without breaking j2cl?
046   */
047  private static final Collector<Object, ?, Optional<Object>> TO_OPTIONAL =
048      Collector.of(
049          ToOptionalState::new,
050          ToOptionalState::add,
051          ToOptionalState::combine,
052          ToOptionalState::getOptional,
053          Collector.Characteristics.UNORDERED);
054
055  /**
056   * A collector that converts a stream of zero or one elements to an {@code Optional}.
057   *
058   * @throws IllegalArgumentException if the stream consists of two or more elements.
059   * @throws NullPointerException if any element in the stream is {@code null}.
060   * @return {@code Optional.of(onlyElement)} if the stream has exactly one element (must not be
061   *     {@code null}) and returns {@code Optional.empty()} if it has none.
062   */
063  @SuppressWarnings("unchecked")
064  public static <T> Collector<T, ?, Optional<T>> toOptional() {
065    return (Collector) TO_OPTIONAL;
066  }
067
068  private static final Object NULL_PLACEHOLDER = new Object();
069
070  private static final Collector<@Nullable Object, ?, @Nullable Object> ONLY_ELEMENT =
071      Collector.<@Nullable Object, ToOptionalState, @Nullable Object>of(
072          ToOptionalState::new,
073          (state, o) -> state.add((o == null) ? NULL_PLACEHOLDER : o),
074          ToOptionalState::combine,
075          state -> {
076            Object result = state.getElement();
077            return (result == NULL_PLACEHOLDER) ? null : result;
078          },
079          Collector.Characteristics.UNORDERED);
080
081  /**
082   * A collector that takes a stream containing exactly one element and returns that element. The
083   * returned collector throws an {@code IllegalArgumentException} if the stream consists of two or
084   * more elements, and a {@code NoSuchElementException} if the stream is empty.
085   */
086  @SuppressWarnings("unchecked")
087  public static <T extends @Nullable Object> Collector<T, ?, T> onlyElement() {
088    return (Collector) ONLY_ELEMENT;
089  }
090
091  /**
092   * This atrocity is here to let us report several of the elements in the stream if there were more
093   * than one, not just two.
094   */
095  private static final class ToOptionalState {
096    static final int MAX_EXTRAS = 4;
097
098    @CheckForNull Object element;
099    List<Object> extras;
100
101    ToOptionalState() {
102      element = null;
103      extras = emptyList();
104    }
105
106    IllegalArgumentException multiples(boolean overflow) {
107      StringBuilder sb =
108          new StringBuilder().append("expected one element but was: <").append(element);
109      for (Object o : extras) {
110        sb.append(", ").append(o);
111      }
112      if (overflow) {
113        sb.append(", ...");
114      }
115      sb.append('>');
116      throw new IllegalArgumentException(sb.toString());
117    }
118
119    void add(Object o) {
120      checkNotNull(o);
121      if (element == null) {
122        this.element = o;
123      } else if (extras.isEmpty()) {
124        // Replace immutable empty list with mutable list.
125        extras = new ArrayList<>(MAX_EXTRAS);
126        extras.add(o);
127      } else if (extras.size() < MAX_EXTRAS) {
128        extras.add(o);
129      } else {
130        throw multiples(true);
131      }
132    }
133
134    ToOptionalState combine(ToOptionalState other) {
135      if (element == null) {
136        return other;
137      } else if (other.element == null) {
138        return this;
139      } else {
140        if (extras.isEmpty()) {
141          // Replace immutable empty list with mutable list.
142          extras = new ArrayList<>();
143        }
144        extras.add(other.element);
145        extras.addAll(other.extras);
146        if (extras.size() > MAX_EXTRAS) {
147          extras.subList(MAX_EXTRAS, extras.size()).clear();
148          throw multiples(true);
149        }
150        return this;
151      }
152    }
153
154    @IgnoreJRERequirement // see enclosing class (whose annotation Animal Sniffer ignores here...)
155    Optional<Object> getOptional() {
156      if (extras.isEmpty()) {
157        return Optional.ofNullable(element);
158      } else {
159        throw multiples(false);
160      }
161    }
162
163    Object getElement() {
164      if (element == null) {
165        throw new NoSuchElementException();
166      } else if (extras.isEmpty()) {
167        return element;
168      } else {
169        throw multiples(false);
170      }
171    }
172  }
173
174  private MoreCollectors() {}
175}