Minecolonies – Clean up compiler warnings and type system issues

in #utopian-io9 years ago

Minecolonies has a sophisticated requesting system, where you can ask for any ingame material and it will will do all the heavy lifting, all the routing and finding the things to be done in your colony so your request can be fulfilled.

This system relies a lot on java generics to make it as general as possible. But generics in java are limited by what the compiler understands and conversions are not always easy. For example a List<Apple> can't be cast to List<Fruit> as you can't add an Orange to it.

So we used raw types in many places to speed up development. But raw types are not something you want to keep everywhere: https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it

When compiling we had many warnings on type safety: image.png
image.png
image.png

After two pull requests and a combined 100 files touched, compiling goes through with no errors:
image.png

Most changes were pretty simple and repetitive: Check what invariants should never be violated by parameters and return types and find the best bound for each type invariant. Then check if code depends on it and do the same there, till all errors are resolved. Then go to the next warning.

Some warnings have to be deliberately suppressed, as the java compiler can be easily confused sometimes and we have to verify by hand that all invariants hold.

I also introduced a few new utility methods to make using the system easier, as there are still a few places where you have to cast. One such method is getRequestOfType:
image.png

Previously you had to check for each request if it actually is a Deliverable or a Retryable when you only wanted to handle these. With the Optional pattern, callers don't need any instanceof and casting.

image.png

This code only wants to act when the request is of a certain type. And optional makes this really easy with conditionally executing a lambda (or skipping it). And since we say what type we want, no casting is needed.

(The second pull request will be merged in the next couple hours, already got some approval: https://github.com/Minecolonies/minecolonies/pull/1726)

Since this is a fairly technical pull request, the explanation is fairly technical too. If you have any aditional questions, I will answer them in the comments.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

I really like it good work ✌

Congratulations @reggaemuffin! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Hey @reggaemuffin I am @utopian-io. I have just upvoted you!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

thank you for ınfos and contrıbution ı follow you allways

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.102
BTC 63964.42
ETH 1857.01
USDT 1.00
SBD 0.39