The strangest thing about being a developer, to me, is that every time I think I have perfected my technique for something, I revisit it after some time away and find a better way of doing things. So this is my 4th iteration of how to pass and accept parameters through Expression Language. Allow me to introduce... the Mapper. (Quick side note: my old GetMap was based on the Map interface. Mapper is based on DataObject just because it is smaller. Mapper could just as easily implement Map if you prefer.
Just as a quick reminder, the goal is to accept a parameter through Expression Language, like so:
This came from studying functional programming concepts and I believe this is a vast improvement on my earlier versions. It is certainly much easier to read and implement, and the most flexible. It works like this:
Just as a quick reminder, the goal is to accept a parameter through Expression Language, like so:
styleClass="#{MyViewBean.buttonStyle['MyButton']}"
public class Mapper implements DataObject, Serializable {
public static interface Function {
private final Function func; //This will be an anonymous implementation
public Mapper(Function func) {
public Object getValue(Object key){
public void setValue(Object key, Object value){
public Class<?> getType(Object key) {
public boolean isReadOnly(Object key) {
}Object map(Object in);
}private final Function func; //This will be an anonymous implementation
public Mapper(Function func) {
this.func = func;
}public Object getValue(Object key){
return func.map(key); // Call the anonymous Function method
}public void setValue(Object key, Object value){
throw new UnsupportedOperationException();
}public Class<?> getType(Object key) {
throw new UnsupportedOperationException();
}public boolean isReadOnly(Object key) {
return true;
}
This came from studying functional programming concepts and I believe this is a vast improvement on my earlier versions. It is certainly much easier to read and implement, and the most flexible. It works like this:
public class MyViewBean {
One thing this version does sacrifice is strongly-typed objects due to the DataObject interface. You could use Map for better typing, but the endpoints are still going to be Objects, so it might be best to just validate the input and be on your way.
private String activeButton = "MyButton";
private final Mapper btnStyleMapper = new Mapper(
public String getActiveButton() {
public void setActiveButton(String activeButton) {
public Object getButtonStyle() {
}private final Mapper btnStyleMapper = new Mapper(
new Mapper.Function(){ // define the translation function
);public Object map(Object in) {
}return getActiveButton().equalsIgnoreCase(in.toString()) ? " active" : "";
}public String getActiveButton() {
return this.activeButton;
}public void setActiveButton(String activeButton) {
this.activeButton = activeButton;
}public Object getButtonStyle() {
return btnStyleMapper;
}
Comments
Post a Comment