1 /* 2 * Copyright (C) 2018 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 libcore.internal; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.ByteArrayOutputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.util.ArrayList; 24 import java.util.Arrays; 25 import java.util.List; 26 import java.util.Objects; 27 import java.util.concurrent.atomic.AtomicReference; 28 29 /** 30 * Proof of concept / fake code whose only purpose is to demonstrate that Java 9 31 * language features are supported in libcore. 32 */ 33 public class Java9LanguageFeatures { 34 35 public interface Person { name()36 String name(); 37 isPalindrome()38 default boolean isPalindrome() { 39 return name().equals(reverse(name())); 40 } 41 isPalindromeIgnoreCase()42 default boolean isPalindromeIgnoreCase() { 43 return name().equalsIgnoreCase(reverse(name())); 44 } 45 46 // Language feature: private interface method reverse(String s)47 private String reverse(String s) { 48 return new StringBuilder(s).reverse().toString(); 49 } 50 } 51 52 @SafeVarargs toListString(T... values)53 public static<T> String toListString(T... values) { 54 return toString(values).toString(); 55 } 56 57 // Language feature: @SafeVarargs on private methods 58 @SafeVarargs toString(T... values)59 private static<T> List<String> toString(T... values) { 60 List<String> result = new ArrayList<>(); 61 for (T value : values) { 62 result.add(value.toString()); 63 } 64 return result; 65 } 66 createReference(T content)67 public <T> AtomicReference<T> createReference(T content) { 68 // Language feature: <> on anonymous class 69 return new AtomicReference<>(content) { }; 70 } 71 copy(byte[] bytes)72 public static byte[] copy(byte[] bytes) throws IOException { 73 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 74 InputStream inputStream = new ByteArrayInputStream(bytes); 75 try (inputStream) { // Language feature: try on effectively-final variable 76 int value; 77 while ((value = inputStream.read()) != -1) { 78 byteArrayOutputStream.write(value); 79 } 80 } 81 return byteArrayOutputStream.toByteArray(); 82 } 83 84 85 } 86