PROPOSAL: Multiple switch expressions and case ranges
Marek Kozieł
develop4lasu at gmail.com
Thu Mar 5 22:40:33 PST 2009
Hi.
I hoped that no one will be interested in this solution (but I was wrong).
Extending switch is no problem for me, but why so partially?
Let's see some switch construction that would allow to handle any object
(inluding null-s) and keep encapsulation as well:
>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);
}
// Same as Comparator but allow encapsulation
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
switch ( Expression : null) SwitchBlock // for natural order
>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, special:
break;
default:
break;
}
}
Notice that if you want ranges in case soon or later o will need relations:
<=, >=, <, >, because there is no (""-1), I know there is workaround but as
I think there is no need to put solution which require new workarounds.
>WORKAROUND:
enum Switch {
ONE("asd") {
@Override
public void action() {
System.out.println(this.text);
}
},//
TWO("bad");//
protected String text;
public void action() {
throw new NotImplementedException();
}
Switch(String text) {
if (text == null) { throw new NullPointerException("text"); }
this.text = text;
}
public static Switch getByText(String text) {
if (text == null) return null;
for (Switch casee : Switch.values()) {
if (casee.text.equals(text)) return casee;
}
return null;
}
public static Switch getByTextForStartWith(String prefix) {
if (prefix == null) return null;
for (Switch casee : Switch.values()) {
if (casee.text.startsWith(prefix)) return casee;
}
return null;
}
}
REFERENCES:
http://bugs.sun.com/view_bug.do?bug_id=5012262
http://lasu2string.blogspot.com/2008/12/string-switch-small-language-changes-on.html
--
Pozdrowionka. / Regards.
Lasu aka Marek Kozieł
http://lasu2string.blogspot.com/
More information about the coin-dev
mailing list