HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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} |
Yuta HIGUCHI | 498fa1d | 2017-05-17 16:08:40 -0700 | [diff] [blame] | 37 | * |
| 38 | * @deprecated in 1.11.0 consider using {@link ImmutableSet#toImmutableSet()} instead. |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 39 | */ |
Yuta HIGUCHI | 498fa1d | 2017-05-17 16:08:40 -0700 | [diff] [blame] | 40 | @Deprecated |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 41 | 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 Li | 7f256f5 | 2016-01-24 15:08:05 -0800 | [diff] [blame] | 53 | * @param <T> type |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 54 | * @return a {@code Collector} which collects all the input elements into a |
| 55 | * {@code ImmutableList}, in encounter order |
Yuta HIGUCHI | 498fa1d | 2017-05-17 16:08:40 -0700 | [diff] [blame] | 56 | * |
| 57 | * @deprecated in 1.11.0 consider using {@link ImmutableList#toImmutableList()} instead. |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 58 | */ |
Yuta HIGUCHI | 498fa1d | 2017-05-17 16:08:40 -0700 | [diff] [blame] | 59 | @Deprecated |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 60 | 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 | } |