• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Sharing temporary data between disparate classes. Like a shared Map or something.

 
Sheriff
Posts: 4961
318
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning Ranchers,

I'm writing some tests and need to make some assertions based on values given during setup. Normally the setup and assertion hook methods live in the same class so is trivial to store data between them using local fields. However, in this case the setup and assertion methods live in different classes. For example:
Please put to one side the silliness of my example, I have simplified it from reality to the point where it looks ridiculous (doesn't even compile) but does demonstrate the key problem which is how to share the dataStore between classes.

I know that I could take a Map instance and wrap it up in a singleton, but I feel this is a problem that's likely already been solved so am looking for an 'off the shelf' solution. Perhaps Spring, or Guava, or Apache, or something else has a ready made solution?

Does anyone know of something like this?

Cheers, Tim
 
Marshal
Posts: 71615
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Cooke wrote:. . . I could take a Map instance and wrap it up in a singleton, . . .

If you are sending the info only in one direction, what about a read‑only Map from Collections#unmodifiableMap().
 
Tim Cooke
Sheriff
Posts: 4961
318
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would the "sending" part work?
 
Campbell Ritchie
Marshal
Posts: 71615
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create your Map in one location, with a getMap() method; you can then send data from that location but in a read‑only format. You can't send anything the other way with the same Map, obviously. If you are working in a small team, your ordinary Map would do nicely.
 
Tim Cooke
Sheriff
Posts: 4961
318
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the absence of anything better, I wrapped a HashMap in a singleton.

Suggestions for improvement are very welcome.
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic