File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Making a collection modifiable only in a specific class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Making a collection modifiable only in a specific class" Watch "Making a collection modifiable only in a specific class" New topic
Author

Making a collection modifiable only in a specific class

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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.


Thanks and Regards
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 01, 2007
Posts: 368
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

Joined: Aug 16, 2005
Posts: 14153
    
  18

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1021
    
    5

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

Joined: Jan 01, 2007
Posts: 368
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

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Jan 01, 2007
Posts: 368
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making a collection modifiable only in a specific class