blob: 40043e46431250a1f0badbc4285356cac1f86f16 [file] [log] [blame]
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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}
37 */
38 public static <T> Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> toImmutableSet() {
39 return Collector.of(ImmutableSet.Builder<T>::new,
40 ImmutableSet.Builder<T>::add,
41 (s, r) -> s.addAll(r.build()),
42 ImmutableSet.Builder<T>::build,
43 Characteristics.UNORDERED);
44 }
45
46 /**
47 * Returns a {@code Collector} that accumulates the input elements into a
48 * new ImmutableList.
49 *
Jian Li7f256f52016-01-24 15:08:05 -080050 * @param <T> type
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080051 * @return a {@code Collector} which collects all the input elements into a
52 * {@code ImmutableList}, in encounter order
53 */
54 public static <T> Collector<T, ImmutableList.Builder<T>, ImmutableList<T>> toImmutableList() {
55 return Collector.of(ImmutableList.Builder<T>::new,
56 ImmutableList.Builder<T>::add,
57 (s, r) -> s.addAll(r.build()),
58 ImmutableList.Builder<T>::build);
59 }
60
61 private GuavaCollectors() {}
62}