1
\$\begingroup\$

The method below converts an array of TheatreListData objects to an array of TheatreListDataWrapper. It also loads additional attributes such as patientIdentifierNumber and requestText to enrich the converted data.

This method is called by many EJBs, and so I'm especially looking for ways to improve performance.

/** 
* Converts TheatreListDatas into TheatreListDataWrappers. This also 
* loads the attributes that has to be generated from client such as 
* patientIdentifierNumber, requestText 
* 
* @param theatreListData 
* @return TheatreListDataWrapper[] 
*/
public TheatreListDataWrapper[] wrapData(TheatreListData[] theatreListData){
 TheatreListDataWrapper[] wrapperList = wrapDataWithoutRequestData(theatreListData);
 if (wrapperList != null && wrapperList.length > 0)
 {
 Set<String> requestIds = new HashSet<String>();
 for (TheatreListData listData : theatreListData)
 {
 // Collect request id for each List Data into a set
 String requestId = listData.getCarePlan().requestId;
 //Should check for '-1' here to avoid calls to fetch invalid requests.
 //'-1' could have been saved with registrations with 'None' requests.
 if (requestId != null && requestId.trim().length() > 0 && !requestId.equals("-1"))
 {
 //Since adding to a set no duplicates are possible.
 requestIds.add(requestId);
 }
 }
 //Load request Id, request Text map using the collected request ids.
 Map<String, String> requestMap = Collections.emptyMap();
 if (!requestIds.isEmpty())
 {
 requestMap = getRequestDelegator().getRequestPresentationStringsByIds(new ArrayList<String>(requestIds));
 }
 //Create wrappers & fill with loaded data.
 for (int i = 0; i < theatreListData.length; i++)
 {
 TheatreListDataWrapper wrapper = wrapperList[i];
 TheatreListData listData = theatreListData[i];
 String requestId = listData.getCarePlan().requestId;
 if (requestId != null && requestId.trim().length() > 0)
 {
 wrapper.requestText = requestMap.get(requestId) != null ? requestMap.get(requestId) : "";
 }
 wrapperList[i] = wrapper;
 }
 }
 return wrapperList;
 }
janos
113k15 gold badges154 silver badges396 bronze badges
asked Mar 30, 2016 at 11:30
\$\endgroup\$
0

2 Answers 2

2
\$\begingroup\$

Depending on the constant overhead of retrieving the request presentation string for a request ID (i.e. is it considerably more efficient to get all the strings for a bunch of IDs at once than for each ID separately?), a better design might be to iterate over the theatre list only once, getting the representation strings as you go and caching the results in a Map.

Map<String, String> cache = new HashMap<>();
for (int i = 0; i < theatreListData.length; i++) {
 TheatreListDataWrapper wrapper = wrapperList[i];
 TheatreListData listData = theatreListData[i];
 String requestId = listData.getCarePlan().requestId;
 if (validRequestId(requestId)) {
 String text = cache.get(requestId);
 if (text == null) {
 text = getRequestDelegator().getRequestPresentationStringsById(requestId);
 cache.put(requestId, text);
 }
 wrapper.requestText = text;
 }
}

This is probably not a good idea of getting the request text requires, say, a database query.

Other than that, I can think of no algorithmic improvements. You should store the result of requestMap.get(requestId) in a local variable so you don't do the call twice; that's inefficient. Perhaps you can modify getRequestPresentationStringsByIds to take a Collection instead of a List so that you don't have to wrap your set in an ArrayList. Can't think of more.

answered Mar 30, 2016 at 12:12
\$\endgroup\$
0
1
\$\begingroup\$

If you're under Java 8 then I'd prefer using the Stream API:

theatreListData
 .stream()
 .map(listData -> listData.getCarePlan().requestId)
 .filter(id -> id != null)
 .filter(id -> id.trim().length() > 0)
 .filter(id -> !id.equals("-1"))
 .collect(Collectors.toSet())

And this comparsion never throws NPE.

"Constant".equals(variable);
Tunaki
9,3011 gold badge31 silver badges46 bronze badges
answered Mar 30, 2016 at 11:49
\$\endgroup\$
2
  • \$\begingroup\$ Sorry mate. I'm in Java 7 \$\endgroup\$ Commented Mar 30, 2016 at 11:50
  • \$\begingroup\$ Then I would define each "for" as new function \$\endgroup\$ Commented Mar 30, 2016 at 12:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.