Created
June 20, 2015 00:55
-
-
Save Overruler/5a9990c609b9ad0adfe7 to your computer and use it in GitHub Desktop.
Prototyping custom receiver types for self-typing and more
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package root.sub; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
import javax.annotation.Generated; | |
import root.List; | |
public class ArrayList<T> implements List<T> { | |
protected @SuppressWarnings("unchecked") T[] array = (T[]) new Object[0]; | |
@Generated(value = { "javac" }) | |
public @Override <THIS extends List<T2>, T2 extends CharSequence> String join_List(THIS thiz, CharSequence delim) { | |
return join_ArrayList((ArrayList<T2>) thiz, delim); | |
} | |
@Generated(value = { "consumer" }) | |
public <THIS extends ArrayList<T2>, T2 extends Object & CharSequence> String join_ArrayList(THIS thiz, CharSequence delim) { | |
assert this == thiz; | |
T2[] array2 = thiz.array; | |
if(array2.length == 0) { | |
return ""; | |
} | |
if(array2.length == 1) { | |
return array2[0].toString(); | |
} | |
int length = delim.length() * (array2.length - 1); | |
for(int i = 0; i < array2.length; i++) { | |
CharSequence t2 = array2[i]; | |
length += t2.length(); | |
} | |
StringBuilder bu = new StringBuilder(length); | |
bu.append(array2[0]); | |
for(int i = 1; i < array2.length; i++) { | |
bu.append(delim); | |
bu.append(array2[i]); | |
} | |
return bu.toString(); | |
} | |
@Generated(value = { "javac" }) | |
public @Override <THIS extends List<T2>, T2 extends List<T3>, T3> T2 flatten_List(THIS thiz) { | |
return flatten_ArrayList((ArrayList<T2>) thiz); | |
} | |
@Generated(value = { "consumer" }) | |
public <THIS extends ArrayList<T2>, T2 extends List<T3>, T3> T2 flatten_ArrayList(THIS thiz) { | |
assert this == thiz; | |
Object[] array2 = thiz.array; | |
if(array2.length == 0) { | |
return null; | |
} | |
int length = 0; | |
for(int i = 0; i < array2.length; i++) { | |
if(array2[i].getClass() == ArrayList.class) { | |
ArrayList<?> list = (ArrayList<?>) array2[i]; | |
length += list.array.length; | |
} else { | |
return List.super.flatten_List(thiz); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
ArrayList<T3> first = (ArrayList<T3>) array2[0]; | |
int offset = first.array.length; | |
first.array = Arrays.copyOf(first.array, length); | |
for(int i = 1; i < array2.length; i++) { | |
ArrayList<?> src = (ArrayList<?>) array2[i]; | |
System.arraycopy(src.array, 0, first.array, offset, src.array.length); | |
offset += src.array.length; | |
} | |
@SuppressWarnings("unchecked") | |
T2 result = (T2) first; | |
return result; | |
} | |
@Generated(value = { "javac" }) | |
public @Override <THIS extends List<T>, T2 extends T> THIS addAll_List(THIS thiz, Iterable<T2> iterable) { | |
ArrayList<T> thiz2 = addAll_ArrayList((ArrayList<T>) thiz, iterable); | |
@SuppressWarnings("unchecked") | |
THIS thiz3 = (THIS) thiz2; | |
return thiz3; | |
} | |
@Generated(value = { "javac" }) | |
public <THIS extends ArrayList<T>, T2 extends T> THIS addAll_ArrayList(THIS thiz, Iterable<T2> iterable) { | |
return List.super.addAll_List(thiz, iterable); | |
} | |
@Generated(value = { "javac" }) | |
public @Override <THIS extends List<T>> THIS add_List(THIS thiz, T t) { | |
ArrayList<T> thiz2 = (ArrayList<T>) thiz; | |
@SuppressWarnings("unchecked") | |
THIS thiz3 = (THIS) add_ArrayList(thiz2, t); | |
return thiz3; | |
} | |
@Generated(value = { "consumer" }) | |
public <THIS extends ArrayList<T>> THIS add_ArrayList(THIS thiz, T t) { | |
assert this == thiz; | |
thiz.array = Arrays.copyOf(thiz.array, thiz.array.length + 1); | |
thiz.array[thiz.array.length - 1] = t; | |
return thiz; | |
} | |
@Generated(value = { "consumer" }) | |
public @Override Iterator<T> iterator() { | |
return Arrays.asList(array).iterator(); | |
} | |
@Generated(value = { "consumer" }) | |
public @Override String toString() { | |
return Arrays.toString(array); | |
} | |
@Generated(value = { "consumer" }) | |
public @SafeVarargs static <T> ArrayList<T> asList(T... ts) { | |
@SuppressWarnings("unchecked") | |
T[] ts2 = (T[]) new Object[ts.length]; | |
System.arraycopy(ts, 0, ts2, 0, ts.length); | |
ArrayList<T> a = new ArrayList<>(); | |
a.array = ts2; | |
return a; | |
} | |
public static void main(String[] args) { | |
version1("11"); | |
version2("12"); | |
version3("13"); | |
version4("14"); | |
version5("15"); | |
version6("16"); | |
version7("17"); | |
version8("18"); | |
} | |
private static void version1(String name) { | |
ArrayList<String> a1 = asList("1", "2"); | |
ArrayList<String> a2 = asList("4", "5"); | |
ArrayList<ArrayList<String>> b = asList(a1.add_List(a1, "3"), a2.add_ArrayList(a2, "6")); | |
ArrayList<ArrayList<String>> c = b.addAll_ArrayList(b, asList(asList("7"), asList(name))); | |
ArrayList<String> d = c.flatten_ArrayList(c); | |
String e = d.join_ArrayList(d, "-"); | |
System.out.print(d); | |
System.out.println(e); | |
} | |
private static void version2(String name) { | |
List<String> a1 = asList("1", "2"); | |
ArrayList<String> a2 = asList("4", "5"); | |
ArrayList<List<String>> b = asList(a1.add_List(a1, "3"), a2.add_List(a2, "6")); | |
ArrayList<List<String>> c = b.addAll_List(b, asList(asList("7"), asList(name))); | |
List<String> d = c.flatten_ArrayList(c); | |
String e = d.join_List(d, ""); | |
System.out.print(d); | |
System.out.println(e); | |
} | |
private static void version3(String name) { | |
ArrayList<String> a1 = asList("1", "2"); | |
List<String> a2 = asList("4", "5"); | |
ArrayList<List<String>> b = asList(a1.add_ArrayList(a1, "3"), a2.add_List(a2, "6")); | |
ArrayList<List<String>> c = b.addAll_ArrayList(b, asList(asList("7"), asList(name))); | |
List<String> d = c.flatten_List(c); | |
String e = d.join_List(d, "-"); | |
System.out.print(d); | |
System.out.println(e); | |
} | |
private static void version4(String name) { | |
List<String> a1 = asList("1", "2"); | |
List<String> a2 = asList("4", "5"); | |
ArrayList<List<String>> b = asList(a1.add_List(a1, "3"), a2.add_List(a2, "6")); | |
ArrayList<List<String>> c = b.addAll_List(b, asList(asList("7"), asList(name))); | |
List<String> d = c.flatten_List(c); | |
String e = d.join_List(d, ""); | |
System.out.print(d); | |
System.out.println(e); | |
} | |
private static void version5(String name) { | |
ArrayList<String> a1 = asList("1", "2"); | |
ArrayList<String> a2 = asList("4", "5"); | |
List<ArrayList<String>> b = asList(a1.add_ArrayList(a1, "3"), a2.add_List(a2, "6")); | |
List<ArrayList<String>> c = b.addAll_List(b, asList(asList("7"), asList(name))); | |
ArrayList<String> d = c.flatten_List(c); | |
String e = d.join_List(d, "-"); | |
System.out.print(d); | |
System.out.println(e); | |
} | |
private static void version6(String name) { | |
List<String> a1 = asList("1", "2"); | |
ArrayList<String> a2 = asList("4", "5"); | |
List<List<String>> b = asList(a1.add_List(a1, "3"), a2.add_ArrayList(a2, "6")); | |
List<List<String>> c = b.addAll_List(b, asList(asList("7"), asList(name))); | |
List<String> d = c.flatten_List(c); | |
String e = d.join_List(d, ""); | |
System.out.print(d); | |
System.out.println(e); | |
} | |
private static void version7(String name) { | |
ArrayList<String> a1 = asList("1", "2"); | |
List<String> a2 = asList("4", "5"); | |
List<List<String>> b = asList(a1.add_List(a1, "3"), a2.add_List(a2, "6")); | |
List<List<String>> c = b.addAll_List(b, asList(asList("7"), asList(name))); | |
List<String> d = c.flatten_List(c); | |
String e = d.join_List(d, "-"); | |
System.out.print(d); | |
System.out.println(e); | |
} | |
private static void version8(String name) { | |
List<String> a1 = asList("1", "2"); | |
List<String> a2 = asList("4", "5"); | |
List<List<String>> b = asList(a1.add_List(a1, "3"), a2.add_List(a2, "6")); | |
List<List<String>> c = b.addAll_List(b, asList(asList("7"), asList(name))); | |
List<String> d = c.flatten_List(c); | |
String e = d.join_List(d, ""); | |
System.out.print(d); | |
System.out.println(e); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package root; | |
import javax.annotation.Generated; | |
public interface List<T> extends Iterable<T> { | |
@Generated(value = { "consumer" }) | |
default <THIS extends List<T2>, T2 extends CharSequence> String join_List(THIS thiz, CharSequence delim) { | |
assert this == thiz; | |
return String.join(delim, thiz); | |
} | |
@Generated(value = { "consumer" }) | |
default <THIS extends List<T3>, T3 extends List<T2>, T2> T3 flatten_List(THIS thiz) { | |
assert this == thiz; | |
T3 result = null; | |
for(T3 items : thiz) { | |
result = result == null ? items : result.addAll_List(result, items); | |
} | |
return result; | |
} | |
@Generated(value = { "consumer" }) | |
default <THIS extends List<T>, T2 extends T> THIS addAll_List(THIS thiz, Iterable<T2> iterable) { | |
assert this == thiz; | |
THIS result = thiz; | |
for(T2 item : iterable) { | |
result = result.add_List(result, item); | |
} | |
return result; | |
} | |
@Generated(value = { "consumer" }) | |
<THIS extends List<T>> THIS add_List(THIS thiz, T t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment