samedi 17 août 2019

Working with generics to store a method execution

I am trying to learn a bit of what I consider more complex Java, I got interested in Generics as I noticed it can do amazing things. I thought about doing this: Have a Map that will store a class and some form of method, this would be used so in case of giving a certain class, lets say String.class it would run the code inside the method.

I saw it being done and working on a project called ACF (Aikar command framework).

An example of how it manages to register the new method:

registerContext(char.class, c -> {
    String s = c.popFirstArg();
    if (s.length() > 1) {
        throw new InvalidCommandArgument(MessageKeys.MUST_BE_MAX_LENGTH, "{max}", String.valueOf(1));
    }
    return s.charAt(0);
});

His register method seems simple being just:

// The map
protected final Map<Class<?>, ContextResolver<?, R>> contextMap = new HashMap<>();
// The register class
public <T> void registerContext(Class<T> context, ContextResolver<T, R> supplier) {
    contextMap.put(context, supplier);
}

I tried making something similar but yet I couldn't really understand. I am whiling to do more research, but at this point I don't know what to search for. I don't know what this type of method storage or method declaration is called. I watched a few videos and read a few posts about the generics in Java but mostly stay very basic which I manage to understand, but still can't figure out how this works. The results I expect is to be able to for example call contextMap.get(class).run() and it would invoke the method.

edit:
PS: I'm not asking anyone to write code for me or do this or that, just to point me in the right direction to learn and I'll do the rest myself. Or maybe some small explanations about generics.





Aucun commentaire:

Enregistrer un commentaire