My wishlist and Ideas for java 7 as java developer

Ali Ebrahimi ali.ebrahimi1781 at gmail.com
Fri Apr 24 10:08:04 PDT 2009


Hi All,
java needs cool features.

My wishlist and ideas for java 7:

1) switch on objects.

switch(obj){
      case String str: println("obj instance of String and str result of
cast (String)obj"); break;  // instanceof case
      case obj2 : println("obj  equals obj2"); break;     // equals case
     ....
}

a few rules for this statement.
1) subclasses must be apears after superclass
class Base{}
clase SubClass extends Base{}

switch(obj){
  case  Base b:  dowithBase(b); ....     // Compile error or Warning
  case SubClasss s: dowithSub(s);
}
this rule anagulus to rules for catch clauses
....

I very like this switch statement compared with all proposed proposals to
list in this case. I want to see this in java 7.

2) function types(method references) base on Method Handles(Thanks john rose
for your great work).
In other words, add language support for Method Handles.
sample:

  public static void each(Collection<?> c, MethodHandle mh) {
      for(Object element:c) {

          mh.invoke(element);
      }
  }
  public static void sayHello(Object message) {
      System.out.println("hello "+message);
  }
  public static void main(String[] args) {
      MethodType methodType=MethodType.make(void.class, Object.class);

      MethodHandle
myFunc=MethodHandles.findStatic(MethodHandleTest.class, "sayHello",
methodType);

      List list=Arrays.asList("item1", "item2");

      each(list, myFunc);
  }

with language support for Method Handles(function types) code would be as
below :

  public static void each(Collection<?> c, (Object)=>void method) { //
or  void(Object)
      for(Object element:c) {
          method.invoke(element);  or method(element)

      }
  }
  public static void sayHello(Object message) {
      System.out.println("hello "+message);
  }

  public static void main(String[] args) {
      (Object)=>void myFunc = MyClass#sayHello(Object);

      List list=Arrays.asList("item1", "item2");
      each(list, myFunc);
  }

3) Property support with Automatic Binding Support
sample:
class Person{
   //form1: with Compiler aware Annotation(As Override)
   private @Property String name;
   // Or

   // form2:
   property String name;

}

Translated to:
class Person{

public final Property<String> Name = Property.<String>make();

public String getName(){
   return this.name.getValue();
}

public void setName(String name){
   this.name.setValue(name);
}
}

how to use:
Person person = new Person();

person.name = "Ali";  //Translated to person.setName("Ali");

String PName = person.name; //Translated to String PName = person.getName();


Property<String> nameProperty = person.Name; // access property object


   - Adding auto binding support:



With only one bind method in Property class

public void bind(Propery<T> dest){ bind(dest,false)}

public void bind(Propery<T> dest, boolean inverse){ .....}

usage :
//UI class
class TextFieldUI extends ...{
     property String text;
}

Person person = new Person();
person.name = "Ali";

TextFieldUI nameUI = new TextFieldUI();

// Uni-directional binding
nameUI.Text.bind(person.Name); // Or bind(nameUI.Text,person.Name);

// bi-directional binding
nameUI.Text.bind(person.Name,true); // bind(nameUI.Text,person.Name,true);



   - Validation support:

By adding one method to Property class

public void addValidator(Validator validator){ ...}



   - Triger or Change Listener support

By adding one method to Property class

public void addTriger(ChangeListener changeListener){ ...} // or
addChangeListener



Propery class thing like as below:

class Property<T>{

private T value;

public T getValue(){
   return value;
}
public void setValue(T newVal){

   T old = this.value;
   if( !old.equals(newVal)){

      if(validate(newVal)){
          this.value = newVal;

          valueChanged(old,newVal);
      }
   }
}

private Set<Property<T>> dependents;
private Set<Validator> validators;
private Set<ChangeListener> trigers;


private valueChanged(K oldV, K newV){
   fireTrigers(oldV,newV);
   propagateToDependencies();
}
private void fireTrigers(K oldV, K newV){

   if(trigers == null || trigers.size() == 0) return;
   for(ChangeListener triger:trigers){
      triger.onChange(oldV,newV);
   }
}
private void propagateToDependendts(){
   if(dependents == null || dependents.size() == 0) return;
   for(Property dependentP:dependents){

      depedentP.setValue(this.value);
   }
}
public void addTriger(ChangeListener changeListener){

   if(trigers == null) trigers=new HashSet<ChangeListener>();

   trigers.add(changeListener);
}
public void removeTriger(ChangeListener changeListener){

   if(trigers != null) trigers.remove(changeListener);

}
public void bind(Property<T> dest){ bind(dest,false);}
public void bind(Property<T> dest, boolean inverse){
    dest.addDependency(this);
    if(inverse) this.addDependency(dest)

}

public void addDependent(Property<T> dependentProperty){

   if(dependents == null) dependets = new HashSet<Property>();

   dependencts.add(dependentProperty);

}
public void removeDependent(Property<T> dependentProperty){

   if(dependents != null) dependents.remove(dependentProperty);

}

public void unBind(Property<T> dest){
    dest.removeDependet(this);

}
...

}

Advantages
1) comprehensive property support
2) Non-Reflection base: do'nt use reflection therefore high perfomance

3) Minimum language change;
....

And many other ideas.

Best Regards
Ali Ebrahimi



More information about the coin-dev mailing list