Sparshith P Sparshith P - 3 months ago 5
Java Question

What does this Holder<> do in Java?

Can someone please explain this code?

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


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

Answer

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.

Imagine:

// 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:

Comments