I got in this situation recently where I was creating a system of defined arguments to be provided to build messages based on an enum to ensure that every element is present in the dynamic args and so that the types are correctly checked on the arguments not to provide a nonsensical arg.
Here is my code:
interface ParamList { Class<?> paramType(); }
@AllArgsConstructor
enum ParamType extends ParamList {
FOO(Foo.class),
BAR(Bar.class);
@Getter
private final Class<?> paramType;
}
class MessageParams<T extends Enum<T> & ParamList> {
private Map<T, Object> args;
// constructor...
public <U> MessageArgs<T> put(T key, U value) {
args.put(key, value);
return this;
}
// get...
}
My problem is int he last class: MessageParams
. I wonder if I could enforce the U value
to be the paramType()
of the provided T
at compile-time (I omitted here my runtime check) so that I don't need to explicitly provide the generic when calling put to get correct hinting.