Let's say your company wanted to offer special deals. When a customer calls about one of these deals, you want to play an automated customer support message using SignalWire, a tool for scripting phone voice trees.
This is a natural case for using a Map
data structure. Which is what Ajay's 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 a case of trying to fit some APIs together awkwardly.
Our function takes a Deal
object. The client.getSWMLs
function apparently requires a list as its input, because we start by converting the input into a singletonList
- a list with one item in it.
client.getSWMLs
then returns a map, presumably mapping every input in the list to the appropriate SWML script. Of course, we only sent it one, so I suspect that our map only has one key. No problem, though, as we can just call get
on the Map
…
Except that's not what we do. We convert the Map
into a Set
of Map.Entry
s, and then iterate across the set. If the Deal
in the set is the Deal
we're looking for, we've found our value.
Bonus points for simply returning an empty string instead of a meaningful error when the input doesn't map to an output. I'm sure that never created any awkward customer support moments.