ZS50 week4 was all about writing efficient code, whether by harnessing existing functionality available in the Apex Standard Library
or implementing our own code that leverages inheritance
.
In this week's challenge we'll tap into both concepts by implementing an interface from the standard library, while calling back to a few concepts we explored in prior weeks. Let's jump in!
In week2 we touched on the ability to tell the Salesforce runtime, via the equals()
and hashcode()
methods, how to determine the uniqueness of objects constructed from our custom types. In this challenge, we'll tell Salesforce how to sort our custom types. How? By implementing the Comparable interface.
Scenario
Remember the
LeadEnrichmentService
class we implemented last week? Further review of the documentation for theHttpEnrichmentService
class we call from it has revealed that theMap<String, String>
(companies -> industry) it returns is first sorted alphabetically by company name.Lead volume is increasing and we want to make sure our algorithms are operating at maximum efficiency 🚗. To that end, we'd like to also sort our
List<Lead>
by company before retrieving industry from the returnedMap
.Of course, we can't edit the
Lead
class itself since it's part of the Standard Library. What we can do is implement a wrapper class around our Leads, and implement Comparable on that...
To successfully complete this challenge:
- If you didn't successfully complete the week3 challenge, do that first! You'll need the code written there to get a
success
on this challenge. - Create a class
LeadWrapper
thatimplements
theComparable
interface. The class should have a single-argument constructor which accepts an argument of type Lead, and sets the following instance variables:public Lead theLead
(set to the passed-in Lead)public String company
(set to the passed in Lead's Company value)
- Note that the
compareTo
method takes a single argument of typeObject
(the superclass of all complex data types in Apex). To implement this logic, you'll need tocast
the compareTo argument toLeadWrapper
in your implementation. - Comparison should be based on the first character (good enough for our purposes) of the
LeadWrapper.company
values, without respect to case, such that:- Return
-1
ifthis
instance's first char comes before compareTo's first char in the alphabet, - Return
1
ifthis
instance's first char comes after compareTo's first char in the alphabet, - In all other cases, return
0
(first chars must be the same)
- Return
- Refactor your
LeadEnrichmentService.enrich()
method as follows:- Instantiate a variable of type
List<LeadWrapper>
named wrappers. - For each Lead passed to your method, construct a
LeadWrapper
instance and add it to wrappers - Call the
wrappers.sort()
- Iterate through wrappers, getting the industry from the Map returned by
HttpEnrichmentService
based on company, and setting each Lead's Industry variable to it (e.g.LeadWrapper.theLead
).
- Instantiate a variable of type
- This challenge will validate that logic implemented last week still returns that same result, plus the additional pre-sorting implemented through
LeadWrapper
. Remember, we'rerefactoring
, which changes only the implementation details, not the outcome.
Hint: Remember back to week1 when we discovered how characters correspond to integers in encoding schemas like ascii
and utf-8
. Make sure to check the String Class documentation to see if there's an easy way to convert that first char into a more useful Integer for comparison.
If you need a little extra help on this challenge, make sure to check out the Comparable Example Implementation in the Apex Developer Guide. And, as always, take advantage of the scaf
command in the zs50
CLI to get up-and-running with less overhead.