Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making a collection modifiable only in a specific class

 
Satya Maheshwari
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.
 
Jeff Verdegan
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • 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.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic