📄 abstractspotter.java
字号:
package org.placelab.spotter;import java.util.Enumeration;import java.util.Vector;import org.placelab.core.Measurement;import org.placelab.eventsystem.EventListener;import org.placelab.eventsystem.EventSystem;/** * Provides common functionality for sending notifications to * listeners and doing single scans. Spotters which use * a stream based approach might choose to subclass this class, * but generally Spotters should subclass either SyncSpotter * or AsyncSpotter for the most convenience. * @see SyncSpotter * @see AsyncSpotter */public abstract class AbstractSpotter implements Spotter { private Vector listeners; public AbstractSpotter() { listeners = new Vector(); } public void addListener(SpotterListener listener) { if (listener != null) { listeners.addElement(listener); } } public void removeListener(SpotterListener listener) { listeners.removeElement(listener); } private static class ScanOnceImplementation implements SpotterListener { private AbstractSpotter spotter; private EventSystem evs; public ScanOnceImplementation(AbstractSpotter s, EventSystem e) { spotter = s; evs = e; spotter.addListener(this); if (evs == null) spotter.startScanning(); else spotter.startScanning(evs); } public void gotMeasurement(Spotter s, Measurement m) { // we are done scanning spotter.stopScanning(); spotter.removeListener(this); spotter.notifyEndOfScan(evs); } /* (non-Javadoc) * @see org.placelab.spotter.SpotterListener#spotterExceptionThrown(org.placelab.spotter.SpotterException) */ public void spotterExceptionThrown(Spotter s,SpotterException ex) { ex.printStackTrace(); } } public void scanOnce() { new ScanOnceImplementation(this, null); } public void scanOnce(EventSystem evs) { new ScanOnceImplementation(this, evs); } protected void notifyGotMeasurement(Measurement m) { Enumeration it=listeners.elements(); while (it.hasMoreElements()) { SpotterListener listener=null; try { listener = (SpotterListener) it.nextElement(); } catch (RuntimeException e) { throw new RuntimeException("notifyGotMeasurement.2:"+e.getMessage()); } try { listener.gotMeasurement(this, m); } catch (RuntimeException e) { e.printStackTrace(); throw new RuntimeException("notifyGotMeasurement.3:"+listener.getClass().getName()+":"+e.getMessage()); } } /*try { disposeIterator(it); } catch (RuntimeException e) { throw new RuntimeException("notifyGotMeasurement.4:"+e.getMessage()); }*/ } protected void notifyGotException(SpotterException ex) { Enumeration it = listeners.elements(); while (it.hasMoreElements()) { SpotterListener listener = (SpotterListener) it.nextElement(); listener.spotterExceptionThrown(this, ex); } } protected void notifyGotMeasurement(EventSystem evs, Measurement m) { if (evs==null) { try { notifyGotMeasurement(m); } catch (RuntimeException e){ throw new RuntimeException("notifyGotMeasurement(EVS).1:"+e.getMessage()); } } else { evs.notifyTransientEvent(new EventListener() { public void callback(Object eventType, Object data) { notifyGotMeasurement((Measurement)data); } }, m); } } protected void notifyGotException(EventSystem evs, SpotterException ex) { if (evs==null) { notifyGotException(ex); } else { evs.notifyTransientEvent(new EventListener() { public void callback(Object eventType, Object data) { notifyGotException((SpotterException)data); } }, ex); } } protected void notifyEndOfScan() { Enumeration it = listeners.elements(); while (it.hasMoreElements()) { SpotterListener listener = (SpotterListener) it.nextElement(); if (listener instanceof ScanOnceListener) { ((ScanOnceListener)listener).endOfScan(this); } } } protected void notifyEndOfScan(EventSystem evs) { if (evs==null) { notifyEndOfScan(); } else { evs.notifyTransientEvent(new EventListener() { public void callback(Object eventType, Object data) { notifyEndOfScan(); } }, null); } } public void waitForThread(Thread t) { if (t==Thread.currentThread()) return; boolean done = false; while (!done) { try { t.join(); done = true; } catch (InterruptedException e) { } } } public abstract void startScanning(); public abstract void startScanning(EventSystem evs); public abstract void stopScanning(); /* Theoretically, the fact that everything is backed by a Vector now should * eliminate any race conditions that this stuff was intended to prevent * (I don't think this stuff was quite right to begin with anyway) */ /* private class ListenerIterator implements Enumeration { ListIterator it; int savedIndex; public ListenerIterator() { it = listeners.listIterator(0); } public boolean hasMoreElements() { return it.hasMoreElements(); } public Object nextElement() { return it.nextElement(); } public void remove() { it.remove(); } public void save(int deleteIndex) { savedIndex = it.nextIndex(); if (deleteIndex >= 0 && deleteIndex < savedIndex) { savedIndex--; } } public void reset() { it = listeners.listIterator(savedIndex); } } private Vector iterators = new Vector(); private Enumeration listenerIterator() { Enumeration it = new ListenerIterator(); iterators.addElement(it); return it; } private void disposeIterator(Enumeration it) { iterators.removeElement(it); } private void saveIterators(SpotterListener delete) { int deleteIndex = (delete != null ? listeners.indexOf(delete) : -1); for (Enumeration it = iterators.elements(); it.hasMoreElements(); ) { ListenerIterator li = (ListenerIterator) it.nextElement(); li.save(deleteIndex); } } private void resetIterators() { for (Enumeration it = iterators.elements(); it.hasMoreElements(); ) { ListenerIterator li = (ListenerIterator) it.nextElement(); li.reset(); } }*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -