I want to write a GWT JSNI wrapper function for the following JavaScript function.
object.cache(config);
where config is an object with the following optional parameters
- x Number
- y Number
- width Number
- height Number
- length Number
I think I would not be good to override all possible combinations of function arguments as a Java function.
How do I model such a JavaScript function with many optional arguments as a JSNI Java function?
I would model the config object as a JavaScriptObject
:
public class Config extends JavaScriptObject {
protected Config() { }
public native final boolean hasX() /*-{ return this.x == null; }-*/;
public native final double getX() /*-{ return this.x || 0; }-*/;
public native final void setX(double x) /*-{ this.x = x; }-*/;
public native final void unsetX() /*-{ delete this.x; }-*/;
…
There are obviously other ways to model it (using java.lang.Double
for instance) but that one is probably the most lightweight wrt the compiled JS output.
Or you could use java.lang.Double
arguments, but you'll pay the price for the wrapper object:
public native void cache(Double x, Double y, Double width, Double height, Double length) /*-{
var cache = {};
if (x != null) { cache.x = [email protected]::doubleValue()(); }
…
Finally, you could also use special values if that makes sense in your case:
var config = {};
if (x >= 0) { config.x = x; }