26 December 2008

String switch: Small language changes on JDK 7

According to Stephen Colebourne's Weblog: JDK 7 language changes - JavaEdge votes!
Allowing put String into switch statement is OK for me. But I cannot agree with placing that into JDK.
You can ask why. For me it's quite simple. Java is Object oriented language, while this change we make it more String or 'Script' oriented.

I need to suggest other solution. Allow to use switch with any kind of object.
In that case we would have add 2 or more switch statement.

How would that look?

Base Interfaces:

public interface Equally<Type> {
  /**
   * @return true if objects are equal
   */

  boolean equal(Type object, Type other);
}

public interface EquallyAsymmetric<Source,Target> {
  /**
   * @return true if objects are equal
   */

  boolean equal(Source source, Target target);
}

public interface ComparatorAsymmetric<Source,Target> {
  int compare(Source source, Target target);
}

Switch:

switch ( Expression : Equally) SwitchBlock

switch ( Expression : EquallyAsymmetric) SwitchBlock

switch ( Expression : Comparator) SwitchBlock

switch ( Expression : ComparatorAsymmetric) SwitchBlock

How would that work?

public class SomeData {

  public SomeData(String key, Data data) {
    super();
    this.key = key;
    this.data = data;
    // validation
  }

  private Data data;

  public final String key;

  public final static Equally<SomeData> equally = new EquallySomeData();

  public final static EquallyAsymmetric<SomeData, String> equallyAsymmetric = new EquallyAsymmetricSomeData();  ;

  public final static Comparator<SomeData> comparator = new ComparatorSomeData();

  private static class ComparatorSomeData implements Comparator<SomeData> {
    @Override
    public int compare(SomeData o1, SomeData o2) {
      return o1.key.compareTo(o2.key);
    }

  };

  private static class EquallyAsymmetricSomeData implements EquallyAsymmetric<SomeData, String> {
    @Override
    public boolean equal(SomeData source, String target) {
      return source.key.equals(target);
    }
  };

  private static class EquallySomeData implements Equally<SomeData> {
    @Override
    public boolean equal(SomeData object, SomeData other) {
      return object.key.equals(other.key);
    }
  }

}

Equally sample:

SomeData getMatch(String key)
{
 switch (new SomeData(key,null):SomeData.equally) {
    case someData:
      break;
    default:
      break;
    }
}

Equally asymmetric sample:

SomeData getMatch(String key)
{
 switch (new SomeData(key,null):SomeData.equallyAsymmetric ) {
 case „some”:
 break;
 default:
 break;
 }
}

Comparator sample:

SomeData getMatch(String key)
{
 switch (new SomeData(key,null):SomeData.equallyAsymmetric ) {
    case lower <= ... < upper:
      break;
    default:
      break;
    }
}


Asymmetric look on operation allow to keep encapsulation while perform operation we need.

@see Using Strings and Objects in Switch case statements

3 comments:

  1. This looks like an interesting way to allow Java to have more powerful switch statements closer such as handling ranges. I suspect that an interface based approach for switch statements wouldn't be adopted by Sun, as it might be seen as too complex. It is more powerful though...

    ReplyDelete
  2. @Stephen
    I have never had to use Switch with Strings, so I expect more enlargable solutions from Sun.

    ReplyDelete
  3. It can make code more readable. I think so

    ReplyDelete