Javadoc safeAddToMap

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131810 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-07-17 21:23:59 +00:00
parent 7da91cadc4
commit df3509d391
1 changed files with 14 additions and 11 deletions

View File

@ -67,7 +67,7 @@ import org.apache.commons.collections.map.UnmodifiableSortedMap;
* </ul> * </ul>
* *
* @since Commons Collections 1.0 * @since Commons Collections 1.0
* @version $Revision: 1.46 $ $Date: 2004/04/21 20:34:11 $ * @version $Revision: 1.47 $ $Date: 2004/07/17 21:23:59 $
* *
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a> * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a> * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
@ -1074,24 +1074,27 @@ public class MapUtils {
} }
/** /**
* Nice method for adding data to a map in such a way * Protects against adding null values to a map.
* as to not get NPE's. The point being that if the * <p>
* value is null, map.put() will throw an exception. * This method checks the value being added to the map, and if it is null
* That blows in the case of this class cause you may want to * it is replaced by an empty string.
* essentially treat put("Not Null", null ) == put("Not Null", "") * <p>
* We will still throw a NPE if the key is null cause that should * This could be useful if the map does not accept null values, or for
* never happen. * receiving data from a source that may provide null or empty string
* which should be held in the same way in the map.
* <p>
* Keys are not validated.
* *
* @param map the map to add to, may not be null * @param map the map to add to, may not be null
* @param key the key * @param key the key
* @param value the value * @param value the value, null converted to ""
* @throws NullPointerException if the map is null * @throws NullPointerException if the map is null
*/ */
public static void safeAddToMap(Map map, Object key, Object value) throws NullPointerException { public static void safeAddToMap(Map map, Object key, Object value) throws NullPointerException {
if (value == null) { if (value == null) {
map.put ( key, "" ); map.put(key, "");
} else { } else {
map.put ( key, value ); map.put(key, value);
} }
} }