Map with Generics
I got a class called
EnchantmentStack<E extends Event>. And in that class there is a method called trigger(E event)
And when you create a new EnchantmentStack, it gets stored in a map like this:
Map<Event, List<EnchantmentStack<?>>>
And when a certain event happens. I get all the events and loop in them calling the trigger method. But it won't work because it's not type safe and the generic is unknown <?>. So is there a way to solve it? Like I could call them and trigger them with the known event39 Replies
basically you'd have to cast it
or
but do you really want events as the keys and not event types?
The problem is that the map is static, and the method that triggers all of them is static
public static void triggerEnchants(Event event) {
for (EnchantmentStack<?> stack : triggers.get(event)) {
stack.trigger(event); // This doesn't work
}
}
So you can't really use generics for it
That's why I put casts in the code
So casting is enough?
if you do it like that, it would work but it isn't type safe
I didn't use generics much before. But I wanted to make a chance in my system
So it may make errors if the Generic type and the event doesn't match?
you can't do it in a type-safe way just with a
Map
If the Map only contains entries where the key matches the value, no
It's like this.
BlockDropItemEvent
If the
register method ensures the event matches the listener, you wouldn't get the errorsOkay good.
But what is better. Do I store the event itselfEvent or do I store the Class<? extends Event>
that's what I mentioned before - if you use the
Event as the key, the listeners would be for that specific event only so if you have data associated with the event, it would need to be the exact same data
If you use the Class as the key, it would be the type of eventOoooo. I think I should use the Class for the Key,
We are using the same construction for event listeners in our codebase so this is a common pattern. Unfortunately the Java type system is not powerful enough to have such associated generics to have the call typesafe without casting.
The Event got data yes. So it will always be null when I try to get from the map right? I should use the Class
It will be safe if I disturbute the enchantment stacks and the events correctly
It is safe when you are caring for the correct type while putting/adding.
This is the method

This is the method


Should this method work?


I think it is possible with a small trick but not practical
i.e. you'd need a different Map implementation that allows you to get the entry (which needs to be of a custom class) for any given key
You are thinking of reified generics?
no
This just sounds like hiding the unsafe cast behind another method.
So I should just cast it?
Should this one work?
This is the old code
yes
But how?
I am not too bright with generics
Never used them before in situations like these
not on its own
Wdym?
something like that
While that solution includes an unsafe cast, you can be sure that it shouldn't throw an exception due to it if you only insert stuff via
addStack
it doesn't include the logic that ensures that you only have the right eventI will try this. Thanks
It says that I cannot cast
Inconvertible types
where exactly?
On the cast.
List<...> listeners = (...) ...;
The first line of the method
Oh wait. You use EnchantmentStack
The list is
MysticEnchantmentComponent
Ugh still inconvertible typesoh, my fault
I edited it
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.