• Post Reply Bookmark Topic Watch Topic
  • New Topic

Observing non Observable  RSS feed

 
Puneet Nayyar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've a scenario where I need to observe an ArrayList object. Actually this ArrayList object is not in scope of my module but i get a reference to this list from another component. So, I cannot make this list object observable.
Is there any way in which i can monitor the changes in this list?
Any help would be appreciated.

Thanks,
Puneet
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope.

And with this pattern it takes 2 to tango, you need one to monitor changes and another to notify of changes.
You register your object as a listener, like the ActionListener type classes. Then you're notified of changes.
If this other component you speak of cannot be changed to pass you a different object, one that 'has a' list,
then I'm not sure how you'll approach this.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a wrapper:
That's the easy part. The harder part lies in iterator(), listIterator(), listIterator(int) and subList(int, int). Those are all views on the original List that also allow changing the List. subList is the easiest:

One thing you can't do, is assume method implementations. You can't assume addAll calls add, but you also cannot assume addAll does not call add. One implementation may do that, another one may not. I've given two possible implementations that solve this issue.
The same holds for the iterators - don't assume their remove methods call the List's remove method or vice versa.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You could use a wrapper:


This would work for observing changes in the List in your scope (ie the code you have control over and can use the ObservedList in, or code that you provide the List to). If the List provided by the other component has changes made to it in the other component (that you don't have control over) using its original reference then you will not be notified of those changes.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct, but the only other option would require constant monitoring and comparing against the previous value (from a different thread).
 
Puneet Nayyar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...

Seems like I've no other option but to run a constant thread and periodically check the change in the list.
Thanks Taariq, Rob and Steve for your suggestions!!

Best Regards,
Puneet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!