    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017
    018package org.apache.commons.exec.util;
    019
    020import java.util.HashMap;
    021import java.util.Map;
    022import java.util.Objects;
    023
    024/**
    025 * Helper classes to manipulate maps to pass substition map to the CommandLine. This class is not part of the public API and could change without warning.
    026 */
    027public class MapUtils {
    028    /**
    029     * Clones a map.
    030     *
    031     * @param source the Map to clone.
    032     * @param <K>    the map key type.
    033     * @param <V>    the map value type.
    034     * @return the cloned map.
    035     */
    036    public static <K, V> Map<K, V> copy(final Map<K, V> source) {
    037        return source == null ? null : new HashMap<>(source);
    038    }
    039
    040    /**
    041     * Clones the lhs map and add all things from the rhs map.
    042     *
    043     * @param lhs the first map.
    044     * @param rhs the second map.
    045     * @param <K> the map key type.
    046     * @param <V> the map value type.
    047     * @return the merged map.
    048     */
    049    public static <K, V> Map<K, V> merge(final Map<K, V> lhs, final Map<K, V> rhs) {
    050        Map<K, V> result = null;
    051        if (lhs == null || lhs.isEmpty()) {
    052            result = copy(rhs);
    053        } else if (rhs == null || rhs.isEmpty()) {
    054            result = copy(lhs);
    055        } else {
    056            result = copy(lhs);
    057            result.putAll(rhs);
    058        }
    059        return result;
    060    }
    061
    062    /**
    063     * Clones a map and prefixes the keys in the clone, e.g. for mapping "JAVA_HOME" to "env.JAVA_HOME" to simulate the behavior of Ant.
    064     *
    065     * @param source the source map.
    066     * @param prefix the prefix used for all names.
    067     * @param <K>    the map key type.
    068     * @param <V>    the map value type.
    069     * @return the clone of the source map.
    070     */
    071    public static <K, V> Map<String, V> prefix(final Map<K, V> source, final String prefix) {
    072        if (source == null) {
    073            return null;
    074        }
    075        final Map<String, V> result = new HashMap<>();
    076        for (final Map.Entry<K, V> entry : source.entrySet()) {
    077            result.put(prefix + '.' + Objects.toString(entry.getKey(), ""), entry.getValue());
    078        }
    079        return result;
    080    }
    081}