A qu. style: JSTL dynamic map breaks work with missing parameter function calls

This is not a problem that needs to be solved, but a question for an experienced developer (I hope that they will be resolved here too ...) about how they evaluate this proposal.

In JSP / JSTL you can only "call" pure getters (without parameters), i.e. properties. There are probably good reasons for this (by the way - what would it be?)

But in everyday work, this can be very limited. Often you want you to be able to give some simple parameter for some getter, simply because you need it. What you need to do is pre-compute the data: first run all the β€œlogic” (using forEach and if) of the page in Java code, create a new wrapper class (only for a specific thing) and, possibly, create data structures from them .

A lot of work, very time consuming, very annoying. For the lazy us. Am i lazy May be. Isn't there such an old saying that coders are lazy in nature, and is it their incentive to search for interesting new ideas?

OK, here is my hack. I am very curious what you think about this. My project manager doesn't like it at all.

I have an implementation of the DynamicMap map that not only contains some previously saved values, but get () runs the function, passing the key and returning the result as a value. The function must be provided by an object that implements a call to DynamicMapCalculator (). (other map methods return some valid default values, such as size 1, empty - false, etc. - not so important) (*)

public class DynamicMap<K, V> implements Map<K, V> { private final DynamicMapCalculator<K, V> calculator; public DynamicMap(final DynamicMapCalculator<K, V> calculator) { this.calculator = calculator; } public V get(final Object key) { return calculator.call(key); } } public interface DynamicMapCalculator<K, V> { public V call(Object key); } 

Now, in essence, we can issue a card through a getter:

 public DynamicMap<Node, String> getPathBelowDynMap() { return new DynamicMap<Node, String>(new DynamicMapCalculator<Node, String>() { private Node node; public DynamicMapCalculator<Node, String> init(final Node node) { this.node = node; return this; } public String call(final Object key) { return node.getPathBelow((Node) key); // some parent node we want the relative path of this node below } }.init(this)); } 

So, you create an instance that implements the DynamicMapCalculator interface, using an anonymous class, passing the init method to the calculator object (this) (it can be built as necessary and should return the instance itself, "internal this") and implementing the call () using some either a method of this object.

So, now for the JSP, the reason we all did this is where you can access [] the map. It is very simple here:

 <c:forEach var="node" items="${myNodeList}"> <li><c:out value="${node.pathBelowDynMap[someParentNode]}"/></li> </c:forEach> 

Ok, I find it sophisticated and elegant. Now, crush me .. but with good reasons and good explanations why this is sooo baaaad, please. Of course, I would prefer if you would give me good reasons why this is normal. And exquisite. And elegant. :)

Thanks for attention!

(*) By the way, why does Map implement "V get (Object key)" and not "V get (K key)"?

0
source share

All Articles