You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2004/08/28 04:28:20 UTC

DO NOT REPLY [Bug 30911] New: - Map that returns a defaul value if key is not present

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=30911>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=30911

Map that returns a defaul value if key is not present

           Summary: Map that returns a defaul value if key is not present
           Product: Commons
           Version: 3.2
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Collections
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: r.u.c.afonso@uol.com.br


By definition if a map does not contain a determined key it returns null. But 
sometimes may be desirable that it returns a not-null value for not present 
keys. My propose is create a Decorated Map that extends 
org.apache.commons.collections.map.AbstractMapDecorator class. In constructor 
it receives the original Map and a default value to be returned is for a 
inexistent key. This class will override get() method like this:

public Object get(Object key) {
	Object value = super.get(key);
	if(value == null) {
		value = this.defaultValue;
	}
	return value;
}

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org