My task is to create a translator, as I have discussed earlier in this thread. How do you use a generic type as return value and a generic type as a parameter?
Short, I have one language that needs to be translated in another. Like this.
public abstract class Translator <T extends OriginalText, V extends LanguageTranslation> {
public abstract V translate(T originalText);
}
The new challenge is, that at design time I do NOT get a sub class of OriginalText
to translate, but rather an instance of the super class OriginalText
. Thus when I call an implementation of the translate
function, I'm able to set the specific LanguageTranslation
V, but need to remain OriginalText
, as oppose to EnglishText
, ChineseText
, etc, which are sub classes.
// I know response type V (specific class of LanguageTranslation) from a parameter from this function.
// However, this is non-sense anyway, because Translator is abstract.
Translator<OriginalText, V> translator = new Translator<OriginalText, V>();
// At design time, I have no clue what is returned by the repository. Hence, I expect the translate method to do the appropriate decision at run time.
vType = translator.translate(repository.getTextObject(textId))
I want my translator - or a factory - to translate in a way that is specific to the OriginalText
(sub classes have several different attributes) and the LanguageTranslation
sub type I have provided. There is a one-to-one matching. However, I do not know this at design time! I need something like a Factory to create this translator for me, right? Other approaches? Double dispatch?