1 // Copyright 2017 The Bazel Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package com.google.devtools.build.android.desugar.testdata.java8; 15 16 import java.util.List; 17 import java.util.function.Function; 18 import java.util.stream.Collectors; 19 20 /** Desugaring test input interface that includes a default method and a static method. */ 21 public interface FunctionWithDefaultMethod<T extends Number> extends Function<T, T> { 22 23 @Override 24 T apply(T input); 25 26 static <T extends Number> Function<T, Long> toLong() { 27 return input -> input.longValue(); 28 } 29 30 default T twice(T input) { 31 return apply(apply(input)); 32 } 33 34 /** Don't call this method from tests, it won't work since Desugar moves it! */ 35 static FunctionWithDefaultMethod<Integer> inc(int add) { 36 return input -> input + add; 37 } 38 39 /** 40 * Implementation of {@link FunctionWithDefaultMethod} that overrides the default method. 41 * Also declares static methods the test uses to exercise the code in this file. 42 */ 43 public static class DoubleInts implements FunctionWithDefaultMethod<Integer> { 44 @Override 45 public Integer apply(Integer input) { 46 return 2 * input; 47 } 48 49 @Override 50 public Integer twice(Integer input) { 51 return 5 * input; // deliberately wrong :) 52 } 53 54 public static List<Long> add(List<Integer> ints, int add) { 55 return ints.stream().map(inc(add)).map(toLong()).collect(Collectors.toList()); 56 } 57 58 public static FunctionWithDefaultMethod<Integer> doubleLambda() { 59 return input -> 2 * input; 60 } 61 62 public static FunctionWithDefaultMethod<Integer> incTwice(int add) { 63 return inc(add)::twice; 64 } 65 66 public static FunctionWithDefaultMethod<Integer> times5() { 67 return new DoubleInts2()::twice; 68 } 69 70 public static Function<Integer, FunctionWithDefaultMethod<Integer>> incFactory() { 71 return FunctionWithDefaultMethod::inc; 72 } 73 } 74 75 /** Empty subclass that explicitly implements the interface the superclass already implements. */ 76 public static class DoubleInts2 extends DoubleInts implements FunctionWithDefaultMethod<Integer> { 77 } 78 } 79