Sparshith P Sparshith P - 1 year ago 108
Java Question

What does this Holder<> do in Java?

Can someone please explain this code?

public void getSupplierByZipCode(
@WebParam(name = "zip", targetNamespace = "")
String zip,
@WebParam(name = "GetSupplierByZipCodeResult", targetNamespace = "", mode = WebParam.Mode.OUT)
Holder<Boolean> getSupplierByZipCodeResult,
@WebParam(name = "SupplierDataLists", targetNamespace = "", mode = WebParam.Mode.OUT)
Holder<SupplierDataList> supplierDataLists);

I've never seen
before in Java. What are
in the function? Are they like outputs?? I need the supplier data list from this function.

Answer Source

See Holder - The entire purpose is to "hold a value" while allowing side-effect modifications of itself.

The instance variable (value) representing the contained/"held" value can be reassigned; this is used to facilitate how [multiple] values are "returned" in the WS - through explicit modification of the holders supplied as parameters. (Note the usage of WebParam.Mode.OUT as well.)

This "extra layer" is required because Java is always Call By Value; the Holder then effectively fakes a pointer-indirection (let's call it "reference-indirection"), as what might be done in C, which leads to Call By (Object) Sharing semantics.


// Outside WS function - setup parameters and invoke
String zip = "98682";
Holder<Boolean> result = new Holder<Boolean>();
getSupplierByZipCode(zip, result, ..);

// Then inside the function the Holder is modified and a new value
// is assigned to it's value member.
getSupplierByZipCodeResult.value = true;

// And outside again, the mutations are visibile still
if (result.value) {
    // Yay!

Since strings are immutable and the zip is not wrapped in a Holder then the zip code cannot be changed (or "returned" by) the WS call.

See also:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download