na strane 6 sme sa dozvedeli ze to ma bezat na raspberry pi.
Vy jste mi tu dal řešení?
vasi kolegovia musia mat radost: bud vam vec naprogramuju alebo na nich otvorite stream nadavok :-)
ale kedze je s Vami sranda zasluzite si riesenie java 7.
package cz.root;
import java.util.Observable;
import java.util.Observer;
public class Counter {
public static class CounterObservable extends Observable {
private Counter counter;
public CounterObservable(Counter counter) {
if(counter == null) {
throw new NullPointerException("Counter must be set");
}
this.counter = counter;
}
/**
* Expose method as public to allow invocation
* from delegatees.
*/
@Override
public synchronized void setChanged() {
super.setChanged();
}
public Counter getCounter() {
return counter;
}
}
public static class CounterObserver implements Observer {
public final void update(Observable o, Object arg) {
if(o instanceof CounterObservable && arg instanceof Integer) {
onUpdate(((CounterObservable) o).getCounter(), (Integer) arg);
}
}
public void onUpdate(Counter counter, int newValue) {
// no-op
}
}
private CounterObservable observableDelegate = new CounterObservable(this);
private int value;
public int getValue() {
return value;
}
public void setValue(int newValue) {
if(this.value != newValue) {
this.value = newValue;
fireValueChanged(this.value);
}
}
private void fireValueChanged(int newValue) {
observableDelegate.setChanged();
observableDelegate.notifyObservers(newValue);
}
public CounterObservable asObservable() {
return this.observableDelegate;
}
public static void main(String[] args) {
Counter a = new Counter();
final Counter b = new Counter();
a.asObservable().addObserver(new CounterObserver() {
@Override
public void onUpdate(Counter counter, int newValue) {
b.setValue(newValue);
}
});
a.setValue(12);
System.out.printf("a = %d, b = %d\n", a.getValue(), b.getValue());
b.setValue(48);
System.out.printf("a = %d, b = %d\n", a.getValue(), b.getValue());
}
}
třeba že chci, aby Java měla v stdlib podporu pro události - ANO TO OPRAVDU CHCI, vždyť je naprosto elementární věc
ok
*) riesenie observable v stdlib vam nevyhovuje lebo viacnasobna dedicnost
*) prisposobit si ho nechcete lebo chcete standardne riesenie
*) pisat vlastne veci nechcete.
*) cudzie veci pouzit nechcete.
toto je dead end. mozete tu dupkat nozickami po capslocku ze vam chyba podpora signalov / viacnasobna dedicnost ale takto to vo vyvoji daleko nedotiahnete.