summaryrefslogtreecommitdiff
path: root/test/java/com/google/devtools/build/android/desugar/testdata/MethodReference.java
blob: a293ceab030e5387ebf7f406036ea2a94cff6634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.android.desugar.testdata;

import com.google.devtools.build.android.desugar.testdata.separate.SeparateBaseClass;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class MethodReference extends SeparateBaseClass<String> {

  private final List<String> names;

  public MethodReference(List<String> names) {
    super(names);
    this.names = names;
  }

  // Class method reference
  public void appendAll(StringBuilder dest) {
    names.stream().forEach(dest::append);
  }

  // Interface method reference (regression test for b/33304582)
  public List<String> transform(Transformer<String> transformer) {
    return names.stream().map(transformer::transform).collect(Collectors.toList());
  }

  // Private method reference (regression test for b/33378312)
  public List<String> some() {
    return names.stream().filter(MethodReference::startsWithS).collect(Collectors.toList());
  }

  // Protected method reference in a base class of another package (regression test for b/33378312)
  public List<String> intersect(List<String> other) {
    return other.stream().filter(this::contains).collect(Collectors.toList());
  }

  // Contains the same method reference as intersect
  public List<String> onlyIn(List<String> other) {
    Predicate<String> p = this::contains;
    return other.stream().filter(p.negate()).collect(Collectors.toList());
  }

  // Private method reference to an instance method that throws (regression test for b/33378312)
  public Callable<String> stringer() {
    return this::throwing;
  }

  /** Returns a method reference derived from an expression (object.toString()). */
  public static Function<Integer, Character> stringChars(Object object) {
    return (object == null ? "" : object.toString())::charAt;
  }

  /** Returns a method reference derived from a field */
  public Predicate<String> toPredicate() {
    return names::contains;
  }

  private static boolean startsWithS(String input) {
    return input.startsWith("S");
  }

  private String throwing() throws Exception {
    StringBuilder msg = new StringBuilder();
    appendAll(msg);
    throw new IOException(msg.toString());
  }

  /** Interface to create a method reference for in {@link #transform}. */
  public interface Transformer<T> {
    T transform(T input);
  }
}