O argumento genérico do Java estende A ou B [duplicado]
Esta pergunta já tem uma resposta aqui:
OR genérico em vez de AND <T estende Number | CharSequence> 3 respostasEm Java, temos argumentos genéricos e genéricos que podem ser especificados para estender / implementar uma determinada classe / interface.
class MyClass<T extends Number>
Pode-se também especificar que ele deve implementar várias interfaces
class MyClass<T extends Number & Comparable>
Minha pergunta é; como posso especificar que um argumento genérico deve estender uma das duas classes?
Class MyClass<T extends JComponent OR JFrame>
O que eu tenho é umPropery
classe, que neste momento se parece com isso.
public abstract class Property<T> {
public abstract void set(JComponent component, T value);
public abstract void set(JFrame component, T value);
public abstract T get(JComponent component);
public abstract T get(JFrame component);
public static Property<Integer> HEIGHT = new Property<Integer>() {
@Override
public void set(JComponent component, Integer value) {
component.setSize(component.getWidth(), value);
}
@Override
public Integer get(JComponent component) {
return component.getHeight();
}
@Override
public void set(JFrame component, Integer value) {
component.setSize(component.getWidth(), value);
}
@Override
public Integer get(JFrame component) {
return component.getHeight();
}
};
public static Property<Integer> WIDTH = new Property<Integer>() {
@Override
public void set(JComponent component, Integer value) {
component.setSize(value, component.getHeight());
}
@Override
public Integer get(JComponent component) {
return component.getWidth();
}
@Override
public void set(JFrame component, Integer value) {
component.setSize(value, component.getHeight());
}
@Override
public Integer get(JFrame component) {
return component.getWidth();
}
};
...
}
No entanto, mais propriedades, como BACKGROUND_COLOR e LOCATION.
O problema realmente começa aqui, pois você pode ver o código duplicado de cada propriedade, mas fica pior na classe Animation:
public class Animation {
public static void callbackAnimation(AnimationCallback callback, double duration, double fps) {
double timeout = (1/fps)*1000;
int iterations = (int)(duration/timeout);
for (int i = 0; i <= iterations; i++) {
callback.run((double)i/(double)iterations);
try {
Thread.sleep((long)timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void animateProperty(final JComponent component, final Property<Color> property, Color to, double duration, double fps) {
final Color currentValue = property.get(component);
final int differenceRed = to.getRed() - currentValue.getRed();
final int differenceGreen = to.getGreen() - currentValue.getGreen();
final int differenceBlue = to.getBlue() - currentValue.getBlue();
final int differenceAlpha = to.getAlpha() - currentValue.getAlpha();
Animation.callbackAnimation(new AnimationCallback() {
@Override
public void run(double fraction) {
Color newColor = new Color(
(int)(currentValue.getRed()+(differenceRed*fraction)),
(int)(currentValue.getGreen()+(differenceGreen*fraction)),
(int)(currentValue.getBlue()+(differenceBlue*fraction)),
(int)(currentValue.getAlpha()+(differenceAlpha*fraction))
);
property.set(component, newColor);
}
}, duration, fps);
}
public static void animateProperty(final JFrame component, final Property<Color> property, Color to, double duration, double fps) {
final Color currentValue = property.get(component);
final int differenceRed = to.getRed() - currentValue.getRed();
final int differenceGreen = to.getGreen() - currentValue.getGreen();
final int differenceBlue = to.getBlue() - currentValue.getBlue();
final int differenceAlpha = to.getAlpha() - currentValue.getAlpha();
Animation.callbackAnimation(new AnimationCallback() {
@Override
public void run(double fraction) {
Color newColor = new Color(
(int)(currentValue.getRed()+(differenceRed*fraction)),
(int)(currentValue.getGreen()+(differenceGreen*fraction)),
(int)(currentValue.getBlue()+(differenceBlue*fraction)),
(int)(currentValue.getAlpha()+(differenceAlpha*fraction))
);
property.set(component, newColor);
}
}, duration, fps);
}
}
Eu tirei alguns métodos, mas você deveria entender. Eu tenho que escrever todos os métodos duas vezes, uma vez para JFrame e uma vez para JComponent, o que é uma dor.