Saturday, 15 February 2014

java - How to deal with GlazedLists's PluggableList requirement for shared publisher and lock -



java - How to deal with GlazedLists's PluggableList requirement for shared publisher and lock -

i have started using glazedlists in java project uses beansbinding extensively (mvvm pattern).

pluggablelist allowing me bind source list table, , alter source list @ runtime. in order create happen every source list must share same listeventpublisher , readwritelock, since pluggablelist must share lock , plublisher it's source. accomplish creating static publisher , lock in class owns potential source lists, utilize static values create list in every instantiation of class pluggablelist, shown in pseudo code below:

public class modelclass { final static eventlist list = new basiceventlist(); final static listeventpublisher listeventpublisher = list.getpublisher(); final static readwritelock readwritelock = list.getreadwritelock(); final eventlist sourcelist = new basiceventlist(listeventpublisher, readwritelock); } public class uicontrollerclass { final pluggablelist pluggablelist = new pluggablelist(modelclass.listeventpublisher, modelclass.readwritelock); // ... phone call pluggablelist.setsource(somesourcelist) }

i have 2 concerns this:

(1) have create decision in model because of specific requirement of component in uicontroller. seems violate mvvm pattern.

(2) shared lock potentially impacts performance of lists if there many , accessed frequently, since share same lock. each of these lists should otherwise able operate independently without caring each other.

am going incorrectly? there improve way create pluggablelists work without modelclass having know special uicontrollerclass requirement , without potential performance hit?

i came elegant solution preserves mvvm pattern eliminates need shared lock , publisher.

i created custom list transformation extends pluggablelist , overrides it's setsource method. new source list synchronized new list created pluggablelist (it have same publisher , lock pluggablelist).

public class hotswappablepluggablelist<t> extends pluggablelist<t> { private eventlist<t> syncsourcelist = new basiceventlist<>(); private listeventlistener<t> listeventlistener = null; public hotswappablepluggablelist() { super(new basiceventlist<t>()); } @override public void setsource(final eventlist<t> sourcelist) { getreadwritelock().writelock().lock(); seek { if (listeventlistener != null) { syncsourcelist.removelisteventlistener(listeventlistener); } syncsourcelist = sourcelist; final eventlist<t> synctargetlist = createsourcelist(); listeventlistener = glazedlists.synceventlisttolist(syncsourcelist, synctargetlist); super.setsource(synctargetlist); } { getreadwritelock().writelock().unlock(); } } }

java mvvm glazedlists beansbinding pluggable

No comments:

Post a Comment