Menu

Partly Unmodifiable Collections

Partly Unmodifiable Collections

In this section you can read about the features provided by event-based-collections like Set, List, Map etc..

Modify-strategy

The JDK unmodifiable decorator - Collections.unmodifiableCollection(Collection c) is simple to use but has only one feature: it makes the collection unmodifiable! The happy-collections implements four features for an unmodifiable state of the collection:

  • AddAllowed - elements can be added, but not removed
  • RemoveAllowed - elements can be removed but not added
  • Modifiable - can be modified
  • Unmodifiable - can't be modified

example:

UnmodifiableCollection_1x0 c = UnmodifiableCollection_1x0.of(    new ArrayList(), 
                                            UnmodifiableStrategy_1x0.AddAllowed);
//add some elements
c.add(1);
c.add(2);
c.add(3);
c.add(4);
//change modify-strategy    
c.setStrategy(UnmodifiableStrategy_1x0.Unmodifiable);

UnmodifiableCollection

The UnmodifiableCollection can be used to decorate any java.util.Collection types, below you can see an example how to forbid removing any elements from a decorated collection.

Collection c = ...;
c = UnmodifiableCollection_1x0.of(c,  UnmodifiableStrategy_1x0.RemoveAllowed);

UnmodifiableList

UnmodifiableList can be created in analogue to the UnmodifiableCollection and can decorate any java.util.List types. For example in the code bellow the list will be decorated to be unmodifiable for the removing of any elements, but it can be modified by adding new elements.

List list =...;
list = UnmodifiableList_1x0.of( list,  UnmodifiableStrategy_1x0.AddAllowed);

UnmodifiableSet

UnmodifiableSet can be created in the same manner as the Unmodified Collection or List, see the code below:

Set set = new HashSet();
//...
//decorate set to be unmodifiable
set = UnmodifiableSet_1x0.of(set,  UnmodifiableStrategy_1x0.AddAllowed);

UnmodifiableMap

UnmodifiableMap example looks almost the same as other decorating examples above:

Map map = new HashMap();
//...
//decorate map to be unmodifiable
map = UnmodifiableMap_1x0.of(map,  UnmodifiableStrategy_1x0.AddAllowed);

Partly Unmodifiable Collections examples