I have an API that reads NBT files. It spits out the tags in a Map, but how do I know what Tags they are? Tag is an object that is inherited by object like LongTag. I want to get them out from the Map as the right tag-type, as the Map is Map<String, Tag>
How do I check what tag types they are instead of doing 16 if statements with instanceof.
object.getClass()
Can you show me how to store it as the correct object? I still don’t see how to avoid the if-statements :-\
Ideally you would use polymorphism so you don’t need to know what kind of tag it is. Otherwise the if statements really aren’t that bad. You could give Tag an int field and then use a switch.
Or a TagType enum field with a switch? Bit more type safe.
Problems like these make me wish Java had double dispatch!