Overview
This covers a possible extension to Java, giving an array a super class for arrays.
This follows from the idea of Project Coin
OVERVIEW
Provide a two sentence or shorter description of these five aspects of the feature:
FEATURE SUMMARY: Should be suitable as a summary in a language tutorial.
Treat array as objects with their own methods rather than values to be passed to helper methods. This leads to more natural coding and gives the methods more immediacy. e.g. through code completion.
MAJOR ADVANTAGE: What makes the proposal a favorable change?
It bring OO programming to arrays , supporting methods already available and written.
MAJOR BENEFIT: Why is the platform better if the proposal is adopted?
Object Orientated consistency for arrays.
MAJOR DISADVANTAGE: There is always a cost.
Someone has to write it and test it.
ALTERNATIVES: Can the benefits and advantages be had some way without a language change?
Call helper methods.
EXAMPLES
Show us the code!
SIMPLE EXAMPLE: Show the simplest possible program utilizing the new feature.
int[] ints = {5,4,3,2,1}; ints.sort(); // instead of Arrays.sort(ints); int pos = ints.indexOf(5); // instead of Arrays.asList(ints).indexOf(5); or ArraysUtils.indexOf(ints, 5); ints.reverse(); // instead of Arrays.reverse(ints); Array array = ints; // cast to super class. int length = array.getLength(); // instead of Array.getLength(array); Object n = array.get(3); // instead of Array.get(array, 3); array.set(3, 7); // instead of Array. Object obj = array; System.out.println(obj); // prints [5,4,7,2,1] instead of having to if (obj instanceof int[]) System.out.println(Array.toString((int[]) obj)); else if (....)
ADVANCED EXAMPLE: Show advanced usage(s) of the feature.
int[] ints = {5,4,3,2,1}; int[] ints2 = ints.copyOf(2); int[] ints3 = ints.subArray(2,4); ints.sort(myComparator); List<Integer> list = ints.asList(); Set<Integer> set = ints.asSet(); long total = ints.sum(); double avg = int.average(); int max = ints.max(); int max2 = ints.max(myComparator); http://commons.apache.org/lang/api/org/apache/commons/lang/ArrayUtils.html int[] onemore = ints.add(8); // instead of ArrayUtils.add(ints, 8); int[] moreInts = ints.addAll(ints2); // instead of ArraysUtils.addAll(ints, ints2); int[] oneless = int.remove(3); // instead of ArrayUtils.remove(ints, 3); Integer[] integers = int.toObject(); int[] intsAgain = integers.toPrimitive();
DETAILS
SPECIFICATION: Describe how the proposal affects the grammar, type system, and meaning of expressions and statements in the Java Programming Language as well as any other known impacts.
A class such as java.lang.Array would need to be added as the parent of all arrays. Subclasses for specific int[], boolean[] might also be needed.
The grammar shouldn't be dramatically different.
COMPILATION: How would the feature be compiled to class files? Show how the simple and advanced examples would be compiled. Compilation can be expressed as at least one of a desugaring to existing source constructs and a translation down to bytecode. If a new bytecode is used or the semantics of an existing bytecode are changed, describe those changes, including how they impact verification. Also discuss any new class file attributes that are introduced. Note that there are many downstream tools that consume class files and that they may to be updated to support the proposal!
In the provides a new parent for arrays could be used, the compilation would be the same as it is now. However, it is the JVM which would need to accept that an array has a different super class.
TESTING: How can the feature be tested?
Check the new methods do the same things as the helper methods. (Should be simple if indeed they just call the same helper methods)
LIBRARY SUPPORT: Are any supporting libraries needed for the feature?
This should be added to the rt.jar
REFLECTIVE APIS: Do any of the various and sundry reflection APIs need to be updated? This list of reflective APIs includes but is not limited to core reflection (java.lang.Class and java.lang.reflect.*), javax.lang.model.*, the doclet API, and JPDA.
The super class for an array would need to return java.lang.Array or the like instead of java.lang.Object. However, again this may be a change for the JVM rather than the rt.jar code.
OTHER CHANGES: Do any other parts of the platform need be updated too? Possibilities include but are not limited to JNI, serialization, and output of the javadoc tool.
The change should be reflected in the javadoc.
MIGRATION: Sketch how a code base could be converted, manually or automatically, to use the new feature.
Replace calls to Arrays.xxx(array, args) to array.xxx(args);
COMPATIBILITY
BREAKING CHANGES: Are any previously valid programs now invalid? If so, list one.
Calls to hashCode() and equals() would be changed if every method were taken. This may be unacceptable in which case these methods could be left as they are rather than call Arrays.hashCode() or Arrays.equals();
EXISTING PROGRAMS: How do source and class files of earlier platform versions interact with the feature? Can any new overloadings occur? Can any new overriding occur?
No.
REFERENCES
EXISTING BUGS: Please include a list of any existing Sun bug ids related to this proposal.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4168079 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4171916 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4439572 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4751076 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4846797 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6353471 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6239196 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4348595
URL FOR PROTOTYPE (optional):
Adding this to the Object class works. It appears the JVM doesn't allow you to add more than two methods but it should be simple to fix.
int[] ints = {5, 4, 3, 2, 1}; System.out.println("ints= "+ints); ints.sort_(); System.out.println("after sort() ints= "+ints); ints = ints.add_(6); System.out.println("after add(6) ints= "+ints);
Prints
ints= [5, 4, 3, 2, 1] after sort() ints= [1, 2, 3, 4, 5] after add(6) ints= [1, 2, 3, 4, 5, 6]
If you change the methods in java.lang.Object as follows.
//// These method should be refactoring into an Array super class for arrays. //// //// This is required to avoid poluting the name space of the Object class //// //// Ideally, each primitive array type would have its own class to avoid the long instanceof chains //// public String toString() { if (this instanceof boolean[]) return Arrays.toString((boolean[]) this); if (this instanceof byte[]) return Arrays.toString((byte[]) this); if (this instanceof short[]) return Arrays.toString((short[]) this); if (this instanceof char[]) return Arrays.toString((char[]) this); if (this instanceof int[]) return Arrays.toString((int[]) this); if (this instanceof float[]) return Arrays.toString((float[]) this); if (this instanceof long[]) return Arrays.toString((long[]) this); if (this instanceof double[]) return Arrays.toString((double[]) this); if (this instanceof Object[]) return Arrays.toString((Object[]) this); return getClass().getName() + '@' + Integer.toHexString(hashCode()); } public void sort_() { if (this instanceof byte[]) Arrays.sort((byte[]) this); else if (this instanceof short[]) Arrays.sort((short[]) this); else if (this instanceof char[]) Arrays.sort((char[]) this); else if (this instanceof int[]) Arrays.sort((int[]) this); else if (this instanceof float[]) Arrays.sort((float[]) this); else if (this instanceof long[]) Arrays.sort((long[]) this); else if (this instanceof double[]) Arrays.sort((double[]) this); else if (this instanceof Object[]) Arrays.sort((Object[]) this); else throw new AssertionError(); } public <ArrayType, Element> ArrayType add_(Element element) { int length = Array.getLength(this); ArrayType array2 = (ArrayType) Array.newInstance(getClass().getComponentType(), length + 1); System.arraycopy(this, 0, array2, 0, length); Array.set(array2, length, element); return array2; }
Additional example methods.
(These don't work as the JVM won't accept more than two extra methods)
public <ArrayType> void arraycopy(int srcPos, ArrayType dest, int destPos, int length) {
System.arraycopy(this, srcPos, dest, destPos, length);
}
/**
* @return For arrays this is never null.
*/
public Class<?> getComponentType() {
return getClass().getComponentType();
}
/**
* @return length of the array.
*/
public int getLength() {
return Array.getLength(this);
}
public <Element> Element get(int index) {
return (Element) Array.get(this, index);
}
public <Element> void set(int index, Element element) {
Array.set(this, index, element);
}
public <ArrayType, Element> ArrayType add(Element element) {
int length = getLength();
ArrayType array2 = (ArrayType) Array.newInstance(getComponentType(), length + 1);
arraycopy(0, array2, 0, length);
Array.set(array2, length, element);
return array2;
}
public <ArrayType, Element> ArrayType addAll(Element... elements) {
int length = getLength();
ArrayType array2 = (ArrayType) Array.newInstance(getComponentType(), length + elements.length);
arraycopy(0, array2, 0, length);
System.arraycopy(elements, length, array2, length, elements.length);
return array2;
}
public <Element> int indexOf(Element element) {
int length = getLength();
if (element == null)
for (int i = 0; i < length; i++) {
Object value = Array.get(this, i);
if (value == null)
return i;
}
else
for (int i = 0; i < length; i++) {
Object value = Array.get(this, i);
if (element.equals(value))
return i;
}
return -1;
}
public <ArrayType> ArrayType remove(int index) {
int length = getLength();
ArrayType array2 = (ArrayType) Array.newInstance(getComponentType(), length - 1);
arraycopy(0, array2, 0, index);
arraycopy(index+1, array2, index, length-index-1);
return array2;
}
public <ArrayType, Element> ArrayType removeElement(Element element) {
int index = indexOf(element);
if (index < 0)
return (ArrayType) this;
return (ArrayType) remove(index);
}
Comments (2)
Feb 18, 2009
Anonymous says:
Ideally wouldn't you also want the Array type to be treated the same as native a...Ideally wouldn't you also want the Array type to be treated the same as native arrays?
Feb 18, 2009
Peter Lawrey says:
I believe there is a proposal to treat List and Map like arrays to support the [...I believe there is a proposal to treat List and Map like arrays to support the [ ] syntax.
Add Comment