Reputation: 22028
Consider the following class:
public class Cars extends Observable{
private ArrayList<String> carList = new ArrayList<String>();
public void addToCarList(String car){
// ...
hasChanged();
notifyObservers();
}
public void removeFromCarList(String car){
// ...
hasChanged();
notifyObservers();
}
public ArrayList<String> getCarList() {
return carList;
}
}
As you can see, every time the carList is changed, I want to notify the Observers
.
If someone does getCarList().add(...);
, this is circumvented.
How can I give read access to the carList
(for iterating over it etc.) but prevent write access to it except for the special methods addToCarList
and removeFromCarList
?
I thought about this:
public ArrayList<String> getCarList() {
return (ArrayList<String>)carList.clone();
}
but someone using my class would, when adding something to the clone of carList
, not be informed that that's not the way it's meant to be done.
Upvotes: 31
Views: 4699
Reputation: 146
use Collections.unmodifiableList(list) as it provides a new List object which cannot be modified , it would throw an UnsupportedOperationException while trying to update/add/delete objects list.
Upvotes: 0
Reputation: 3416
I think you could probably make your Object implement the Collection-Interface, if it is in fact an ObservableList. It is a List and it should be Observable - so it should implement both interfaces.
You could even Extend List<..> because you just want to add extra functionality (observers) to the current functionality and your List can be used everywhere where a normal List could be used...
Upvotes: 2
Reputation: 41188
Jon Skeet's answer is excellent (as always) but the one thing it doesn't touch on is concurrency issues.
Returning an unmodifiable collection will still leave you with issues if multiple threads are accessing this object at the same time. For example if one thread is iterating over the list of cars and then at the same time another thread adds a new card.
You will still need to synchronize access to that list somehow, and this is one reason why you might consider returning a clone()
of the list as well as or instead of just wrapping it in the unmodifiableList
wrapper. You would still need to synchronize around the clone()
but once the clone is completed and the list returned to the querying code it no longer needs to be synchronized.
Upvotes: 9
Reputation: 115338
First, always avoid using concrete class at the left side of assignment and as a return value of method. So, fix your class as
public class Cars extends Observable{
private List<String> carList = new ArrayList<String>();
........................
public List<String> getCarList() {
return carList;
}
}
Now you can use Collections.unmodifiableList()
to make you list read-only:
public List<String> getCarList() {
return Collections.unmodifiableList(carList);
}
BTW, if you do not really have to return List
you can probably return Collection
or even Iterable
. This will make increase the encapsulation level of your code and make future modifications easier.
Upvotes: 13
Reputation: 1500825
You can return an unmodifiable view of it, changing the return type to List<String>
instead of ArrayList<String>
:
public List<String> getCars() {
return Collections.unmodifiableList(carList);
}
Note that as Collections.unmodifiableList
does only provide a view, the caller will still see any other changes that are made via addToCarList
and removeFromCarList
(which I'd rename to addCar
and removeCar
, probably). Is that what you want?
Any mutating operations on the returned view will result in an UnsupportedOperationException
.
Upvotes: 41