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