java_generics_signature.java (4301B)
1 // VIM_TEST_SETUP let g:java_highlight_functions = 'style' 2 // VIM_TEST_SETUP let g:java_highlight_signature = 1 3 // VIM_TEST_SETUP let g:java_highlight_generics = 1 4 // VIM_TEST_SETUP let g:java_highlight_java_lang = 1 5 6 // VIM_TEST_SETUP hi link javaGenericsC1 Todo 7 // VIM_TEST_SETUP hi link javaGenericsC2 Error 8 // VIM_TEST_SETUP hi link javaWildcardBound Error 9 10 11 import java.math.BigInteger; 12 import java.util.function.Consumer; 13 import java.util.function.Function; 14 import java.util.function.LongFunction; 15 import java.util.function.Predicate; 16 17 class Generics$Tests<T extends Number & Comparable<? super T>, U> 18 { // JDK 21+. 19 static final Function<Function<Object, Object>, Object> PARTIAL = 20 Generics$Tests.y0(); 21 static final Function<BigInteger, BigInteger> FACTORIAL_2000 = 22 Generics$Tests.<BigInteger, BigInteger>y1() 23 .apply(f -> x -> (x.compareTo(BigInteger.ONE) < 1) 24 ? BigInteger.ONE 25 : x.multiply(f.apply(x.subtract(BigInteger.ONE)))); 26 27 static <T1> Y0<T1> y0() 28 { 29 return (Function<T1, T1> f) -> f.apply( 30 Generics$Tests.<T1>y0() 31 .apply(f)); 32 } 33 34 static <T1, T2> Y1<T1, T2> y1() 35 { 36 return (Function<Function<T1, T2>, Function<T1, T2>> f) -> 37 (T1 x) -> f.apply(Generics$Tests.<T1, T2>y1() 38 .apply(f)) 39 .apply(x); 40 } 41 42 static<T> void noOp(T dummy) { } 43 44 interface alpha<T> { } 45 46 interface Y0<T1> extends Function<Function<T1, T1>, T1> { } 47 48 interface Y1<T1, T2> extends Function<Function<Function<T1, T2>, 49 Function<T1, T2>>, 50 Function<T1, T2>> { } 51 52 interface Stackable<E> extends Iterable<E> 53 { 54 boolean isEmpty(); 55 E peek(); 56 E pop(); 57 Stackable<E> popAll(Stackable<? super E> elements); 58 Stackable<E> popSome(Stackable<? super E> elements, 59 Predicate<? super E> filter); 60 Stackable<E> push(E element); 61 Stackable<E> pushAll(Iterable<? extends E> elements); 62 Stackable<E> pushSome(Iterable<? extends E> elements, 63 Predicate<? super E> filter); 64 Stackable<E> wind(Consumer<? super Stackable<E>> action); 65 } 66 67 sealed interface Num<N extends Number> 68 { 69 int radix(); 70 N value(); 71 } 72 73 record Bin<N extends Number>(N value) implements Num<N> 74 { 75 public int radix() { return 2; } 76 } 77 78 record Dec<N extends Number>(N value) implements Num<N> 79 { 80 public int radix() { return 10; } 81 } 82 83 record Hex<N extends Number>(N value) implements Num<N> 84 { 85 public int radix() { return 16; } 86 } 87 88 record Oct<N extends Number>(N value) implements Num<N> 89 { 90 public int radix() { return 8; } 91 } 92 93 static Num<Long> fromDecimal(long x, int radix) 94 { 95 record Pair(LongFunction<Num<Long>> a, 96 LongFunction<String> b) { } 97 final Pair p = switch (radix) { 98 case 2 -> new Pair(Bin::new, Long::toBinaryString); 99 case 8 -> new Pair(Oct::new, Long::toOctalString); 100 case 16 -> new Pair(Hex::new, Long::toHexString); 101 default -> new Pair(Dec::new, 102 y -> Long.toString(y)); 103 }; 104 return p.a().apply(Long.parseLong(p.b().apply(x), radix)); 105 } 106 107 static long toDecimal(Num<Long> x) 108 { 109 return Long.parseLong(switch (x) { 110 case Bin<?>(Long b) -> Long.toBinaryString(b); 111 case Oct<?>(Long o) -> Long.toOctalString(o); 112 case Hex<?>(Long h) -> Long.toHexString(h); 113 default -> Long.toString(x.value()); 114 }, x.radix()); 115 } 116 117 static Class<?> eitherComparableOrIterable(Object o) 118 { 119 final boolean comparable; 120 return ((comparable = o instanceof Comparable) ^ 121 o instanceof Iterable) 122 ? (comparable) 123 ? Comparable.class 124 : Iterable.class 125 : java.lang.Class.class; 126 } 127 128 <A, T extends java.util.function.Supplier<A>, 129 B, U extends java.util.function.Supplier<B>> U convert(T o) 130 { 131 throw new UnsupportedOperationException("TODO"); 132 } 133 134 @java.lang.annotation.Target( 135 java.lang.annotation.ElementType.TYPE_USE) 136 @interface Taggable 137 { 138 String value() default ""; 139 } 140 141 { 142 int N = 0, X = 1, Y = 2; 143 Predicate<T> f = y->N<y.intValue(); 144 Predicate<T> g = y->X<N&&(Integer)y>N; 145 boolean[] bb = { 146 X<N||N>Y, X < Y, X <Y, X <(Y), X<(Y), (X)<Y, 147 Double.isFinite(X<<Y), 148 X<=Y, X<(int)(byte)Y, X<~Y, X<-Y, X<+Y, 149 }; 150 Class<?> klass = Generics$Tests.class; 151 Class< java.lang.Class<@Taggable("<>")int[][]> [] [] > 152 [ ] [ ] $ [ ] [ ]; 153 if (false) { new Generics$Tests<>(); } 154 alpha<?> ao; 155 alpha<U> au; 156 alpha<alpha<U>> aau; 157 alpha<Y0<?>> ay0o; 158 alpha<Y0<U>> ay0u; 159 Y0<alpha<?>> y0ao; 160 Y0<alpha<U>> y0au; 161 } 162 }