001/*
002 * Copyright (c) 2012, the Last.fm Java Project and Committers
003 * All rights reserved.
004 *
005 * Redistribution and use of this software in source and binary forms, with or without modification, are
006 * permitted provided that the following conditions are met:
007 *
008 * - Redistributions of source code must retain the above
009 *   copyright notice, this list of conditions and the
010 *   following disclaimer.
011 *
012 * - Redistributions in binary form must reproduce the above
013 *   copyright notice, this list of conditions and the
014 *   following disclaimer in the documentation and/or other
015 *   materials provided with the distribution.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
018 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
019 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
020 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
021 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
023 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
025 */
026package de.umass.util;
027
028import java.util.Map;
029
030/**
031 * Utility class to perform various operations on Maps.
032 *
033 * @author Adrian Woodhead
034 */
035public final class MapUtilities {
036
037        private MapUtilities() {
038        }
039
040        /**
041         * Puts the passed key and value into the map only if the value is not null.
042         *
043         * @param map Map to add key and value to.
044         * @param key Map key.
045         * @param value Map value, if null will not be added to map.
046         */
047        public static void nullSafePut(Map<String, String> map, String key, String value) {
048                if (value != null) {
049                        map.put(key, value);
050                }
051        }
052
053        /**
054         * Puts the passed key and value into the map only if the value is not null.
055         *
056         * @param map Map to add key and value to.
057         * @param key Map key.
058         * @param value Map value, if null will not be added to map.
059         */
060        public static void nullSafePut(Map<String, String> map, String key, Integer value) {
061                if (value != null) {
062                        map.put(key, value.toString());
063                }
064        }
065
066        /**
067         * Puts the passed key and value into the map only if the value is not -1.
068         *
069         * @param map Map to add key and value to.
070         * @param key Map key.
071         * @param value Map value, if -1 will not be added to map.
072         */
073        public static void nullSafePut(Map<String, String> map, String key, int value) {
074                if (value != -1) {
075                        map.put(key, Integer.toString(value));
076                }
077        }
078
079        /**
080         * Puts the passed key and value into the map only if the value is not -1.
081         *
082         * @param map Map to add key and value to.
083         * @param key Map key.
084         * @param value Map value, if -1 will not be added to map.
085         */
086        public static void nullSafePut(Map<String, String> map, String key, double value) {
087                if (value != -1) {
088                        map.put(key, Double.toString(value));
089                }
090        }
091}