Glue classes proposal 0.9 (original) (raw)

Marek Kozieł develop4lasu at gmail.com
Fri Mar 20 07:35:55 PDT 2009


AUTHOR: Lasu aka Marek Kozieł

OVERVIEW

FEATURE SUMMARY: Glue classes allow to link utils direct with objects.

MAJOR ADVANTAGE:

MAJOR BENEFIT(s):

MAJOR DISADVANTAGE: Do not know any ;) sorry.

ALTERNATIVES: Utils classes, more work on project, duplicated code...

EXAMPLES

SIMPLE EXAMPLE:

Now time: public class base {

public static T[] flatten(T[][] this) { int length = 0; for (T[] subArray : this) { length += subArray.length; } T[] ret = (T[]) Array.newInstance(this.getClass().getComponentType().getComponentType(), length); int pos = 0; for (T[] subArray : this) { System.arraycopy(subArray, 0, ret, pos, subArray.length); pos += subArray.length; } return ret; }

} Now time(usage): public static void main(String[] args) { Integer[][] array = new Integer[][] { { 1, 2, 3, 4 }, { 5, 6, 7 }, {}, { 9, 0 } }; System.out.println(Arrays.toString(base.flatten(array))); }

Glue: public class base glue( T[][] ) {

public glue T[] flatten( this) { int length = 0; for (T[] subArray : this) { length += subArray.length; } T[] ret = (T[]) Array.newInstance(this.getClass().getComponentType().getComponentType(), length); int pos = 0; for (T[] subArray : this) { System.arraycopy(subArray, 0, ret, pos, subArray.length); pos += subArray.length; } return ret; }

} Glue(usage): public static void main(String[] args) { Integer[][] array = new Integer[][] { { 1, 2, 3, 4 }, { 5, 6, 7 }, {}, { 9, 0 } }; System.out.println(array..flatten()..toString()); }

ADVANCED EXAMPLE: Now time: public class base {

public static int addAll( ArrayList this, Iterator<? extends T> iterator) { int added = 0; while (iterator.hasNext()) { added += this.add(iterator.next()) ? 1 : 0; } return added; }

public static <T, E extends T> int addAll(ArrayList this, E[] elements) { int added = 0; for (T t : elements) { added += this.add(t) ? 1 : 0; } return added; }

} Now time(usage): public static void main(String[] args) { Integer[] add = new Integer[] { 1, 2, 34, 2, 5, }; ArrayList arrayList = new ArrayList(); ArrayList finall = new ArrayList();

base.addAll(arrayList, add);
base.addAll(finall, add);
base.addAll(finall,arrayList.iterator());

System.out.println(finall);

}

Glue: public class base glue( ArrayList ){

public glue int addAll( this, Iterator<? extends T> iterator) { int added = 0; while (iterator.hasNext()) { added += this.add(iterator.next()) ? 1 : 0; } return added; }

public glue int addAll( this, E[] elements) { int added = 0; for (T t : elements) { added += this.add(t) ? 1 : 0; } return added; }

} Glue(usage): public static void main(String[] args) { Integer[] add = new Integer[] { 1, 2, 34, 2, 5, }; ArrayList arrayList = new ArrayList(); ArrayList finall = new ArrayList();

arrayList..addAll(add);
finall..addAll(add);
finall..addAll(arrayList.iterator());

System.out.println(finall);

}

Glue second: public class base glue( Date ) { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");

public glue String format( this ) { synchronized (sdf) { return sdf.format(this); } }

private static final Date applicationStart = new Date();

public static glue Date applicationStart() { return applicationStart; } } Glue second(usage): public static void main(String[] args) { System.out.println(Date..applicationStart()..format()); } DETAILS

SPECIFICATION: JLS 8.8: ClassDeclaration: NormalClassDeclaration GlueClassDeclaration EnumDeclaration

GlueClassDeclaration ClassModifiers_opt class Identifier TypeParameters_opt glue ( GlueFormalParameter ) GlueClassBody

GlueFormalParameter VariableModifiers Type

GlueClassBody: { GlueClassBodyDeclarations_opt }

GlueClassBodyDeclarations: GlueClassBodyDeclaration GlueClassBodyDeclarations GlueClassBodyDeclaration

GlueClassBodyDeclaration: GlueClassMemberDeclaration StaticInitializer

GlueClassMemberDeclaration: FieldDeclaration StaticMethodDeclaration GlueMethodDeclaration StaticClassDeclaration InterfaceDeclaration ;

GlueMethodDeclaration: GlueMethodHeader MethodBody

GlueMethodHeader: MethodModifiers_opt glue TypeParameters*opt ResultType GlueMethodDeclarator Throwsopt

GlueMethodDeclarator: Identifier ( this,*opt FormalParameterListopt )

*opt: 'this' occurs always and only if glue method is not static.

TypeParameters from GlueClassDeclaration are always visible for TypeParameters in GlueMethodHeader (even if method is static).

15.12 Method Invocation Expressions:

GlueMethodInvocation:
Primary .. NonWildTypeArguments*_opt TypeName_opt GlueMethodIdentifier ( ArgumentList_opt )
super .. NonWildTypeArguments*_opt GlueMethodIdentifier ( ArgumentList_opt )
ClassName . super .. NonWildTypeArguments*_opt GlueMethodIdentifier ( ArgumentList_opt )
TypeName .. NonWildTypeArguments* GlueMethodIdentifier ( ArgumentList_opt )

NonWildTypeArguments* suit TypeParameters in GlueMethodHeader.

COMPILATION: If object (except null) is valid for GlueFormalParameter then glue-method can be called for this object. If type is compatible with GlueFormalParameter then static glue-method can be called for this type. For non static methods glued-object is passed as first 'this' parameter. Compiled method signature contains TypeParameters from GlueClassDeclaration (at start), and it's always static.

TESTING: Like simple static methods.

LIBRARY SUPPORT: No.

REFLECTIVE APIS: No.

OTHER CHANGES: No.

MIGRATION: None.

COMPATIBILITY New jars are fully compatible. Code is only backward compatible.

REFERENCES

Glue classes proposal 0.9 : http://lasu2string.blogspot.com/2009/03/glue-classes-proposal-09.html

Bug: Extension of 'Interface' definition to include class (static) methods: http://bugs.sun.com/view_bug.do?bug_id=4093687

-- Pozdrowionka. / Regards. Lasu aka Marek Kozieł

http://lasu2string.blogspot.com/



More information about the coin-dev mailing list