P. Werner
@GG:10503226225456996381110 years ago
Edit
P. Werner
@GG:10503226225456996381110 years ago
P. Werner
@GG:10503226225456996381110 years ago
P. Werner
@GG:10503226225456996381110 years ago
public static final class Lenght5 implements Lenght { @Override public final int getLenght() { return 5; } } public static final class Lenght8 implements Lenght { @Override public int getLenght() { return 8; } } public static interface Lenght { int getLenght(); } public static class ID<T extends Lenght> { public ID(final T lenght, final String str) { if (str.length() > lenght.getLenght()) { throw new IllegalArgumentException("ID to long!"); } } } public static void main(final String[] args) { final ID<Lenght5> id1 = new ID<>(new Lenght5(), "12345");// works final ID<Lenght5> id2 = new ID<>(new Lenght5(), "123456");// throws Exception }You use an interface that has a getter-method for the non-type-argument. And then force to pass a derived instance of the interface to the constructor of the class, that should've the argument.
P. Werner
@GG:10503226225456996381110 years ago
P. Werner
@GG:10503226225456996381110 years ago