oop - Java: Returning an Unmodifiable Collection but should be able to update the underlying Collection -


i facing design problem requirement such maintaining map, map<string, list<string>>

i need able return unmodifiable instance of map no 1 accessing api can tamper underlying map.

class inmemorydb {    private map<string,list<string>> typeslabvaluesmap;    public list<string> getslabs(string typevalue){      if (typeslabvaluesmap.containskey(typevalue) {       return collections.unmodifiablelist(typeslabvaluesmap.get(typevalue);     }     return collections.emptylist();   } } 

now should able change map , when needed for, e.g.

  1. a new type comes in should able add type , slab values
  2. slab values need changed should able update map these values

now assuming can expose getslabs in unmodifiable list, how should design update part of map mentioned above?

all updates of map should take place within class (which seems desired original author of class, anyway, since used collections.unmodifiable wrapper). provide public methods validate , alter map according business logic. regarding given examples:

  1. public void addnewtype(string type, list<string> values)
  2. public void updatetype(string type, list<string> values)

both have same signature, content different. in these methods, prevent map gets inconsistent.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -