blob: 75d059eda09026ded315f093495f97a8b5f0e1af [file] [log] [blame]
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.util;
17
18import java.util.stream.Collector;
19import java.util.stream.Collector.Characteristics;
20
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableSet;
23
24/**
25 * Implementations of {@link Collector} that implement various useful reduction
26 * operations, such as accumulating elements into Guava collections.
27 */
28public final class GuavaCollectors {
29
30 /**
31 * Returns a {@code Collector} that accumulates the input elements into a
32 * new ImmutableSet.
33 *
Jian Li7f256f52016-01-24 15:08:05 -080034 * @param <T> type
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080035 * @return a {@code Collector} which collects all the input elements into a
36 * {@code ImmutableSet}
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -070037 *
38 * @deprecated in 1.11.0 consider using {@link ImmutableSet#toImmutableSet()} instead.
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080039 */
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -070040 @Deprecated
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080041 public static <T> Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> toImmutableSet() {
42 return Collector.of(ImmutableSet.Builder<T>::new,
43 ImmutableSet.Builder<T>::add,
44 (s, r) -> s.addAll(r.build()),
45 ImmutableSet.Builder<T>::build,
46 Characteristics.UNORDERED);
47 }
48
49 /**
50 * Returns a {@code Collector} that accumulates the input elements into a
51 * new ImmutableList.
52 *
Jian Li7f256f52016-01-24 15:08:05 -080053 * @param <T> type
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080054 * @return a {@code Collector} which collects all the input elements into a
55 * {@code ImmutableList}, in encounter order
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -070056 *
57 * @deprecated in 1.11.0 consider using {@link ImmutableList#toImmutableList()} instead.
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080058 */
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -070059 @Deprecated
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080060 public static <T> Collector<T, ImmutableList.Builder<T>, ImmutableList<T>> toImmutableList() {
61 return Collector.of(ImmutableList.Builder<T>::new,
62 ImmutableList.Builder<T>::add,
63 (s, r) -> s.addAll(r.build()),
64 ImmutableList.Builder<T>::build);
65 }
66
67 private GuavaCollectors() {}
68}