P
Piotr Kobzda
Below is the code generator of utility class that may possibly appear
useful extension for java.util.Arrays methods.
Basic usages of the generated class includes:
1) Lists backed with primitive arrays manipulation.
int[] array = { 1, 2, 3 };
Collections.shuffle(PrimitiveArrays.asList(array));
2) Simple primitive type arrays boxing/unboxing.
int[] array = { 1, 2, 3 };
Integer[] boxedArray = PrimitiveArrays.box(array);
array = PrimitiveArrays.unbox(boxedArray);
3) Type-safe, non-dimensions limited, extended boxing/unboxing of
primitive arrays.
int[][][] a = {{{ 1, 2, 3 }}};
Integer[][][] b = PrimitiveArrays.intBoxer().deeper().deeper().box(a);
// operate on b...
PrimitiveArrays.intBoxer().deeper().deeper().unbox(b, a);
// operate on updated a...
What do you think about that? Is that worth of becoming a new Java RFE?
Regards,
piotr
import static java.lang.System.out;
public class Generator {
static String[][] pairs = {
{"boolean", "Boolean"},
{"byte", "Byte"},
{"char", "Character"},
{"double", "Double"},
{"float", "Float"},
{"int", "Integer"},
{"long", "Long"},
{"short", "Short"},
};
public static void main(String[] args) {
// out.println("package java.util;");
out.println("import java.lang.reflect.Array;");
out.println("import java.util.AbstractList;");
out.println("import java.util.List;");
out.println("import java.util.RandomAccess;");
out.println();
out.println("public class PrimitiveArrays {");
out.println();
out.println("private static abstract class ArrayBackedList<E>");
out.println(" extends AbstractList<E> implements RandomAccess {");
out.println("}");
out.println();
generateAsListMethods();
generateBoxersBase();
generateBoxers();
generateBoxingMethods();
out.println("}");
}
static void generateAsListMethods() {
new Object() {
void gen(String from, String to) {
out.println("public static List<"+ to +"> asList(final "+ from
+"[] array) {");
out.println(" return new ArrayBackedList<"+ to +">() {");
out.println();
out.println(" @Override");
out.println(" public int size() {");
out.println(" return array.length;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ to +" get(int index) {");
out.println(" return array[index];");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ to +" set(int index, "+ to +" element)
{");
out.println(" "+ to +" r = array[index];");
out.println(" array[index] = element;");
out.println(" return r;");
out.println(" }");
out.println(" };");
out.println("}");
out.println();
}
{
for(String[] pair : pairs) {
gen(pair[0], pair[1]);
}
}
};
}
static void generateBoxersBase() {
out.println("public static abstract class Boxer<S, T> {");
out.println(" private Class<S> typeOfS;");
out.println(" private Class<T> typeOfT;");
out.println();
out.println(" Boxer(Class<S> typeOfS, Class<T> typeOfT) {");
out.println(" this.typeOfS = typeOfS;");
out.println(" this.typeOfT = typeOfT;");
out.println(" }");
out.println();
out.println(" private Boxer<S[], T[]> child;");
out.println();
out.println(" public Boxer<S[], T[]> deeper() {");
out.println(" if (child != null) return child;");
out.println();
out.println(" final Boxer<S, T> parent = Boxer.this;");
out.println(" return child = new Boxer<S[], T[]>(");
out.println(" arrayType(typeOfS), arrayType(typeOfT)) {");
out.println();
out.println(" @Override");
out.println(" public T[] box(S[] source) {");
out.println(" T[] target = newArray(typeOfT, source.length);");
out.println(" for(int i = 0, len = source.length; i < len;
++i)");
out.println(" target = parent.box(source);");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public T[] box(S[] source, T[] target) {");
out.println(" for(int i = 0, len = source.length; i < len;
++i)");
out.println(" parent.box(source, target);");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public S[] unbox(T[] source) {");
out.println(" S[] target = newArray(typeOfS, source.length);");
out.println(" for(int i = 0, len = source.length; i < len;
++i)");
out.println(" target = parent.unbox(source);");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public S[] unbox(T[] source, S[] target) {");
out.println(" for(int i = 0, len = source.length; i < len;
++i)");
out.println(" parent.unbox(source, target);");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public int dimensions() {");
out.println(" return parent.dimensions() + 1;");
out.println(" }");
out.println();
out.println(" };");
out.println(" }");
out.println();
out.println(" public abstract T box(S source);");
out.println();
out.println(" public abstract T box(S source, T target);");
out.println();
out.println(" public abstract S unbox(T source);");
out.println();
out.println(" public abstract S unbox(T source, S target);");
out.println();
out.println();
out.println(" public int dimensions() {");
out.println(" return 1;");
out.println(" }");
out.println();
out.println(" @SuppressWarnings(\"unchecked\")");
out.println(" static <T> Class<T[]> arrayType(Class<T>
componentType) {");
out.println(" return (Class<T[]>)
Array.newInstance(componentType, 0).getClass();");
out.println(" }");
out.println();
out.println(" @SuppressWarnings(\"unchecked\")");
out.println(" static <T> T[] newArray(Class<T> componentType, int
length) {");
out.println(" return (T[]) Array.newInstance(componentType,
length);");
out.println(" }");
out.println();
out.println("}");
out.println();
}
static void generateBoxers() {
new Object() {
void genBoxer(String from, String to) {
out.println("private static class "+ to +"Boxer extends Boxer<"+
from +"[], "+ to +"[]> {");
out.println(" static final "+ to +"Boxer INSTANCE = new "+ to
+"Boxer();");
out.println();
out.println(" "+ to +"Boxer() {");
out.println(" super("+ from +"[].class, "+ to +"[].class);");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ to +"[] box("+ from +"[] source) {");
out.println(" return box(source, new "+ to +"[source.length]);");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ to +"[] box("+ from +"[] source, "+ to
+"[] target) {");
out.println(" for(int i = 0, len = source.length; i < len; ++i)");
out.println(" target = source;");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ from +"[] unbox("+ to +"[] source) {");
out.println(" return unbox(source, new "+ from
+"[source.length]);");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ from +"[] unbox("+ to +"[] source, "+
from +"[] target) {");
out.println(" for(int i = 0, len = source.length; i < len; ++i)");
out.println(" target = source;");
out.println(" return target;");
out.println(" }");
out.println("}");
out.println();
}
void genAccessMethod(String from, String to) {
out.println("public static Boxer<"+ from +"[], "+ to +"[]> "+
from +"Boxer() {");
out.println(" return "+ to +"Boxer.INSTANCE;");
out.println("}");
out.println();
}
{
for(String[] pair : pairs) {
genBoxer(pair[0], pair[1]);
}
for(String[] pair : pairs) {
genAccessMethod(pair[0], pair[1]);
}
}
};
}
static void generateBoxingMethods() {
new Object(){
void gen(String name, String from, String to, String boxer) {
out.println("public static "+ to +"[] "+ name +"("+ from +"[]
source) {");
out.println(" return "+ boxer +"Boxer.INSTANCE."+ name +
"(source);");
out.println("}");
out.println();
}
{
for(String[] pair : pairs) {
gen("box", pair[0], pair[1], pair[1]);
}
for(String[] pair : pairs) {
gen("unbox", pair[1], pair[0], pair[1]);
}
}
};
}
}
useful extension for java.util.Arrays methods.
Basic usages of the generated class includes:
1) Lists backed with primitive arrays manipulation.
int[] array = { 1, 2, 3 };
Collections.shuffle(PrimitiveArrays.asList(array));
2) Simple primitive type arrays boxing/unboxing.
int[] array = { 1, 2, 3 };
Integer[] boxedArray = PrimitiveArrays.box(array);
array = PrimitiveArrays.unbox(boxedArray);
3) Type-safe, non-dimensions limited, extended boxing/unboxing of
primitive arrays.
int[][][] a = {{{ 1, 2, 3 }}};
Integer[][][] b = PrimitiveArrays.intBoxer().deeper().deeper().box(a);
// operate on b...
PrimitiveArrays.intBoxer().deeper().deeper().unbox(b, a);
// operate on updated a...
What do you think about that? Is that worth of becoming a new Java RFE?
Regards,
piotr
import static java.lang.System.out;
public class Generator {
static String[][] pairs = {
{"boolean", "Boolean"},
{"byte", "Byte"},
{"char", "Character"},
{"double", "Double"},
{"float", "Float"},
{"int", "Integer"},
{"long", "Long"},
{"short", "Short"},
};
public static void main(String[] args) {
// out.println("package java.util;");
out.println("import java.lang.reflect.Array;");
out.println("import java.util.AbstractList;");
out.println("import java.util.List;");
out.println("import java.util.RandomAccess;");
out.println();
out.println("public class PrimitiveArrays {");
out.println();
out.println("private static abstract class ArrayBackedList<E>");
out.println(" extends AbstractList<E> implements RandomAccess {");
out.println("}");
out.println();
generateAsListMethods();
generateBoxersBase();
generateBoxers();
generateBoxingMethods();
out.println("}");
}
static void generateAsListMethods() {
new Object() {
void gen(String from, String to) {
out.println("public static List<"+ to +"> asList(final "+ from
+"[] array) {");
out.println(" return new ArrayBackedList<"+ to +">() {");
out.println();
out.println(" @Override");
out.println(" public int size() {");
out.println(" return array.length;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ to +" get(int index) {");
out.println(" return array[index];");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ to +" set(int index, "+ to +" element)
{");
out.println(" "+ to +" r = array[index];");
out.println(" array[index] = element;");
out.println(" return r;");
out.println(" }");
out.println(" };");
out.println("}");
out.println();
}
{
for(String[] pair : pairs) {
gen(pair[0], pair[1]);
}
}
};
}
static void generateBoxersBase() {
out.println("public static abstract class Boxer<S, T> {");
out.println(" private Class<S> typeOfS;");
out.println(" private Class<T> typeOfT;");
out.println();
out.println(" Boxer(Class<S> typeOfS, Class<T> typeOfT) {");
out.println(" this.typeOfS = typeOfS;");
out.println(" this.typeOfT = typeOfT;");
out.println(" }");
out.println();
out.println(" private Boxer<S[], T[]> child;");
out.println();
out.println(" public Boxer<S[], T[]> deeper() {");
out.println(" if (child != null) return child;");
out.println();
out.println(" final Boxer<S, T> parent = Boxer.this;");
out.println(" return child = new Boxer<S[], T[]>(");
out.println(" arrayType(typeOfS), arrayType(typeOfT)) {");
out.println();
out.println(" @Override");
out.println(" public T[] box(S[] source) {");
out.println(" T[] target = newArray(typeOfT, source.length);");
out.println(" for(int i = 0, len = source.length; i < len;
++i)");
out.println(" target = parent.box(source);");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public T[] box(S[] source, T[] target) {");
out.println(" for(int i = 0, len = source.length; i < len;
++i)");
out.println(" parent.box(source, target);");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public S[] unbox(T[] source) {");
out.println(" S[] target = newArray(typeOfS, source.length);");
out.println(" for(int i = 0, len = source.length; i < len;
++i)");
out.println(" target = parent.unbox(source);");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public S[] unbox(T[] source, S[] target) {");
out.println(" for(int i = 0, len = source.length; i < len;
++i)");
out.println(" parent.unbox(source, target);");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public int dimensions() {");
out.println(" return parent.dimensions() + 1;");
out.println(" }");
out.println();
out.println(" };");
out.println(" }");
out.println();
out.println(" public abstract T box(S source);");
out.println();
out.println(" public abstract T box(S source, T target);");
out.println();
out.println(" public abstract S unbox(T source);");
out.println();
out.println(" public abstract S unbox(T source, S target);");
out.println();
out.println();
out.println(" public int dimensions() {");
out.println(" return 1;");
out.println(" }");
out.println();
out.println(" @SuppressWarnings(\"unchecked\")");
out.println(" static <T> Class<T[]> arrayType(Class<T>
componentType) {");
out.println(" return (Class<T[]>)
Array.newInstance(componentType, 0).getClass();");
out.println(" }");
out.println();
out.println(" @SuppressWarnings(\"unchecked\")");
out.println(" static <T> T[] newArray(Class<T> componentType, int
length) {");
out.println(" return (T[]) Array.newInstance(componentType,
length);");
out.println(" }");
out.println();
out.println("}");
out.println();
}
static void generateBoxers() {
new Object() {
void genBoxer(String from, String to) {
out.println("private static class "+ to +"Boxer extends Boxer<"+
from +"[], "+ to +"[]> {");
out.println(" static final "+ to +"Boxer INSTANCE = new "+ to
+"Boxer();");
out.println();
out.println(" "+ to +"Boxer() {");
out.println(" super("+ from +"[].class, "+ to +"[].class);");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ to +"[] box("+ from +"[] source) {");
out.println(" return box(source, new "+ to +"[source.length]);");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ to +"[] box("+ from +"[] source, "+ to
+"[] target) {");
out.println(" for(int i = 0, len = source.length; i < len; ++i)");
out.println(" target = source;");
out.println(" return target;");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ from +"[] unbox("+ to +"[] source) {");
out.println(" return unbox(source, new "+ from
+"[source.length]);");
out.println(" }");
out.println();
out.println(" @Override");
out.println(" public "+ from +"[] unbox("+ to +"[] source, "+
from +"[] target) {");
out.println(" for(int i = 0, len = source.length; i < len; ++i)");
out.println(" target = source;");
out.println(" return target;");
out.println(" }");
out.println("}");
out.println();
}
void genAccessMethod(String from, String to) {
out.println("public static Boxer<"+ from +"[], "+ to +"[]> "+
from +"Boxer() {");
out.println(" return "+ to +"Boxer.INSTANCE;");
out.println("}");
out.println();
}
{
for(String[] pair : pairs) {
genBoxer(pair[0], pair[1]);
}
for(String[] pair : pairs) {
genAccessMethod(pair[0], pair[1]);
}
}
};
}
static void generateBoxingMethods() {
new Object(){
void gen(String name, String from, String to, String boxer) {
out.println("public static "+ to +"[] "+ name +"("+ from +"[]
source) {");
out.println(" return "+ boxer +"Boxer.INSTANCE."+ name +
"(source);");
out.println("}");
out.println();
}
{
for(String[] pair : pairs) {
gen("box", pair[0], pair[1], pair[1]);
}
for(String[] pair : pairs) {
gen("unbox", pair[1], pair[0], pair[1]);
}
}
};
}
}