Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Making a collection modifiable only in a specific class

 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is their some existing mechanism using which I can enforce that a collection is modifiable only in specific class(es) and is read-only elsewhere?I had a look a unmodifiable collection but they are unmodifiable everywhere.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside your class, use the collection normally. But don't expose the raw collection to the outside world. For that, use the unmodifiable collection.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Inside your class, use the collection normally. But don't expose the raw collection to the outside world. For that, use the unmodifiable collection.



Thanks jeff for replying! Your suggestion would be perfect if the collection was being instantiated inside the class. But unfortunately it's being passed as an argument to the class.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the creation of the collection is out of the hands of the class in which it is to be modifiable, then it is impossible to make it modifiable inside that class.

Suppose that someone passes you an immutable list (of an interface type). You don't know what implementation is behind the interface. It could be an implementation that doesn't have any methods to mutate it at all. You can't magically create those methods when you need them.

Why do you have this requirement? If the program has been properly designed, there's probably a good reason why the collection that you get passed is immutable, and you should find a different solution for the problem you're facing.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya

What else is the class responsible for with respect to the collection? At this moment, it sounds like the collection should be created in the constructor of the class protecting it from being modified.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying!!
Let me add the exact details. Maybe there is a problem with the design itself.
Basically I have a class called graph which gets a set of vertices and a set of edges in the constructor. There are getters for these sets as well in the class. Now I want to avoid any additions/deletions to these sets outside the graph class except the explicit call to graph.addVertex or graph.addEdge. This is necessary to avoid any graph manipulations without the graph class knowing about it.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then simply make a copy of the collections passed to the constructor. Your class will own - and therefore fully control access to - the new instances.

This is sometimes called "defensive copying", googling it up might bring more information about it, though there is really not much more to say about this topic.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Martin!
Yes, I was thinking on similar lines but now when I know that it is a known java practice, I am a bit more reassured.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic