I'm working with Java Immutable library http://immutables.github.io/ I have 3 Immutable classes, so basically 3 classes with a bunch of params. For each of these classes, we have a unique key.
So, I decided to define an interface like this:
public interface DataKey<T> {
public T getKey();
}
Now, I want each Immutable class to implement this Interface, here is an example
@Value.Immutable
public abstract class CurrencyDemo implements DataKey<String> {
public abstract String getCode();
@Value.Derived
public String getKey() {
return getCode();
}
}
This works all fine. I was able to get this working because I knew that I should provide an implementation for getKey, otherwise I'll get an Exception when building the Immutable java object.
Is there any way to oblige the user to force the Developer to provide an implementation of getKey() when they define an immutable class implementing the DataKey interface??
Purpose; I could write common code on these immutable classes with a guarantee that they will all have a method called getKey()