Let’s say your company wants to offer special offers. When a customer calls about one of these offers, you want to play an automated customer support message using SignalWire, a phone voice tree scripting tool.
This is a natural case for using a Map
data structure. What is what Ajayhis predecessor did. They just… uh… weren’t sure how to use a Map
.
public String convert(Deal deal)
List<Deal> single = Collections.singletonList(deal);
Map<Deal, String> swml = client.getSWMLs(single, version);
for(Map.Entry<Deal, String> entry : swml.entrySet())
if (deal.equals(entry.getKey()))
return entry.getValue();
return "";
This Java code looks like an attempt to clumsily patch together some APIs.
Our function occupies a Deal
object. The client.getSWMLs
the function obviously requires a a list as its input, because we start by converting the input ua singletonList
– list with one item.
client.getSWMLs
then returns a map, probably a mapping each list entry in the corresponding SWML script. Of course, we only sent one, so I doubt our ticket has only one key. No problem, as we can simply call it get
on Map
…
Except that’s not what we do. We pretend Map
ua Set
from Map.Entry
s and then iterate through the set. If it is Deal
it is complete Deal
we are looking, we have found our value.
Bonus points for simply returning an empty string instead of a meaningful error when the input doesn’t map to the output. I’m sure it never created any awkward customer support moments.
Continuously monitor your servers for configuration changes and report if configuration drift occurs. Get started with Otter today!