/tw-generics-kata

Kata for Java Generics

Primary LanguageJava

Kata for Java Generics

Level: Intermediate
Timebox: 1hr

This kata helps you be more comfortable with Java generics beyond simple List<String>. You will practice writing generics classes and methods, and not just using them. You will also practice lambda expressions and method references.

Goal

Write a ExchangeDesk class to change currencies, like the foreign currency desk of an airport. Avoid hard-coding any particular currency class in the converter. Preserve currency types in the conversion.

Part 1

The currency conversion call should look like:

final INR rupees = new INR(1_000);
final USD dollar = exchangeDesk.convert(rupees, USD.class); 

So your goal is to change the type declarations in convert from specific currencies to general Currency references using generics.

Hard-code the exchange rate for this part.

Part 2

Remember: Your "exchange desk" will need to know exchange rates (try INR to USD for example rates), but not look them up in real-time—assume static rates for the kata:

exchangeDesk.addRate(USD.class, INR.class, 64.5d);

Adding a rate for USD to INR should also add the corresponding rate for INR to USD.

Rules for success

  • Always start with a failing test (hint: use the above code in your test)
  • Keep test coverage at 100%
  • Keep clean code style
  • No compiler warnings
  • No type casting (exception for casting to a generic parameter, if required)
  • No use of reflection: use lambdas and/or method references

Tips

  • Fork the kata (https://github.com/binkley/tw-generics-kata) to your github account; work from there, pushing your local changes back to your fork
  • Ensure your editor has annotation processing enabled for javac, and a Lombok plugin or support turned on
  • Use the "ABC" pattern to your advantage: git add . && ./gradlew test && git commit (or amend)
  • Commit every time the tests pass; save ./gradlew clean build for pushing your commit

Extra credit

Each of these are suitable for follow-on katas:

Further reading

Updates

  • 2018/10/24 - Refreshed Gradle, dependencies and tooling