mardi 17 mars 2015

Java Wrapper enum switch polymorphism

I have this code with using Wrapper, from original interface. On bases enums a will call the methods of linked list, like putFirst(T data), putCurrent(T data), putLast(), removeFirst(), ...



...

@Override
public void put(T data, EnumPosition position) {

switch (position) {
case FIRST:
abstrList.putFirst();
break;

case LAST:
abstrList.putLast();
break;

...

default:
throw new NoSuchElementException("something exception...");
}

@Override
public void remove(T data, EnumPosition position) {

switch (position) {
case FIRST:
abstrList.removeFirst(data);
break;

case LAST:
abstrList.removeLast(data);
break;

...

default:
throw new NoSuchElementException("something exception...");
}


My EnumPosition now looks simple. (is possible change it implement some interface)



public enum EnumPosition {
FIRST, LAST, CURRENT ...


}


A would like to ask, if is possible remove this switch and replace it some polymorphism, consumer with lambda in java 8 or other... Something more effective.


Thanks for your help


Aucun commentaire:

Enregistrer un commentaire