java - How to make new object and preserve Listeners from old object of the same type -


i have class flight implemented propertychangesupport follows:

protected list changelisteners;

public void addpropertychangelistener(propertychangelistener listenertoadd){ ...}

public void removepropertychangelistener(propertychangelistener listenertoremove){ ... }

private void notifychangelisteners(string message){ ...}

...classic observer pattern. want accomplish creation of new flight objects, preservation of existing propertychangelisteners. simple task took me deep multithreading , concurrency troubles, discovering "lapsed listener problem".

this first "simple solution" doesnt work:

public flight newflightwitholdlisteners(list<scans> scanslist){     flight newflight=new flightimpl();     newflight.setscanslist(scanslist);     newflight.setexplorermanager(this.getexplorermanager());     (iterator<propertychangelistener> iterator = this.getchangelisteners().iterator(); iterator.hasnext();) {         propertychangelistener listener=iterator.next();         newflight.addpropertychangelistener(listener);         iterator.remove();     }     return newflight; } 

i'm not getting error, doesnt act should. propertychangelisteneres remain linked old flight objects, , propertychangeevents fired before should fired.

am missed logic? should trivial?

question design. design creation of new objects old propertychangelisteners?

you cannot directly change underlying collection while iterating on or concurrentmodificationexception. should use iterator's remove() method instead.

instead of

this.changelisteners.remove(iterator.next()); 

just do

iterator.remove() 

as logic should do

for (iterator<propertychangelistener> iterator = this.getchangelisteners().iterator(); iterator.hasnext();) {     propertychangelistener propertychangelistener   = (propertychangelistener )iterator.next();     newflight.addpropertychangelistener(propertychangelistener);     iterator.remove(); } 

Comments

Popular posts from this blog

python - Installing PyDev in eclipse is failed -

PHP OOP-based login system -

c# - Nested Internal Class with Readonly Hashtable throws Null ref exception.. on assignment -