Debian Codegraph @

common/src/main/java/com/android/utils/ImmutableCollectors.java

raw
1 /*
2 * Copyright (C) 2016 The Android Open Source Project
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
17 package com.android.utils;
18
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableSet;
21
22 import java.util.stream.Collector;
23 import java.util.stream.Collectors;
24
25 /**
26 * Util for creating {@link Collector} that creates immutable collections, similar to
27 * {@link Collectors}.
28 */
29 public class ImmutableCollectors {
30 public static <T> Collector<T, ImmutableList.Builder<T>, ImmutableList<T>> toImmutableList() {
31 return Collector.of(
32 ImmutableList::builder,
33 ImmutableList.Builder::add,
34 (b1, b2) -> b1.addAll(b2.build()),
35 ImmutableList.Builder::build);
36 }
37
38 public static <T> Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> toImmutableSet() {
39 return Collector.of(
40 ImmutableSet::builder,
41 ImmutableSet.Builder::add,
42 (b1, b2) -> b1.addAll(b2.build()),
43 ImmutableSet.Builder::build);
44 }
45 }