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.ArrayList; 17 import java.util.List; 18 import java.util.function.Function; 19 import java.util.stream.Collectors; 20 21 /** An interface with default methods, lambdas, and generics */ 22 public interface GenericDefaultInterfaceWithLambda<T> { 23 getBaseValue()24 T getBaseValue(); 25 increment(T value)26 T increment(T value); 27 toString(T value)28 String toString(T value); 29 toList(int bound)30 public default ArrayList<T> toList(int bound) { 31 ArrayList<T> result = new ArrayList<>(); 32 if (bound == 0) { 33 return result; 34 } 35 result.add(getBaseValue()); 36 for (int i = 1; i < bound; ++i) { 37 result.add(increment(result.get(i - 1))); 38 } 39 return result; 40 } 41 convertToStringList(List<T> list)42 public default List<String> convertToStringList(List<T> list) { 43 return list.stream().map(this::toString).collect(Collectors.toList()); 44 } 45 toListSupplier()46 public default Function<Integer, ArrayList<T>> toListSupplier() { 47 return this::toList; 48 } 49 50 /** The type parameter is concretized to {@link Number} */ 51 interface LevelOne<T extends Number> extends GenericDefaultInterfaceWithLambda<T> {} 52 53 /** The type parameter is instantiated to {@link Integer} */ 54 interface LevelTwo extends LevelOne<Integer> { 55 56 @Override getBaseValue()57 default Integer getBaseValue() { 58 return 0; 59 } 60 } 61 62 /** An abstract class with no implementing methods. */ 63 abstract static class ClassOne implements LevelTwo {} 64 65 /** A class for {@link Integer} */ 66 class ClassTwo extends ClassOne { 67 68 @Override increment(Integer value)69 public Integer increment(Integer value) { 70 return value + 1; 71 } 72 73 @Override toString(Integer value)74 public String toString(Integer value) { 75 return value.toString(); 76 } 77 } 78 79 /** A class fo {@link Long} */ 80 class ClassThree implements LevelOne<Long> { 81 82 @Override getBaseValue()83 public Long getBaseValue() { 84 return Long.valueOf(0); 85 } 86 87 @Override increment(Long value)88 public Long increment(Long value) { 89 return value + 1; 90 } 91 92 @Override toString(Long value)93 public String toString(Long value) { 94 return value.toString(); 95 } 96 } 97 } 98