HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onlab.util; |
| 17 | |
| 18 | import java.util.stream.Collector; |
| 19 | import java.util.stream.Collector.Characteristics; |
| 20 | |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import 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 | */ |
| 28 | public final class GuavaCollectors { |
| 29 | |
| 30 | /** |
| 31 | * Returns a {@code Collector} that accumulates the input elements into a |
| 32 | * new ImmutableSet. |
| 33 | * |
Jian Li | 7f256f5 | 2016-01-24 15:08:05 -0800 | [diff] [blame] | 34 | * @param <T> type |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 35 | * @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 Li | 7f256f5 | 2016-01-24 15:08:05 -0800 | [diff] [blame] | 50 | * @param <T> type |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 51 | * @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 | } |