The java.util.Collections
class allows me to make collection instances unmodifiable. The following method
protected Map<String, List<String>> getCacheData() {
return Collections.unmodifiableMap(tableColumnCache);
}
returns an umodifiable Map
, so that an UnsupportedOperationException
is caused by attempting to alter the map instance.
@Test(expected = UnsupportedOperationException.class)
public void checkGetCacheData_Unmodifiable() {
Map<String, List<String>> cacheData = cache.getCacheData();
cacheData.remove("SomeItem");
}
Unfortunately all List<String>
childs aren't unmodifiable. So I want to know whether there is a way to enforce the unmofiable behavior for the child values List<String>
too?
Of course, alternatively I can iterate through the maps key value pairs, make the lists unmodifiable and reassemble the map.