MapUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */

  17. package org.apache.commons.exec.util;

  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.Objects;

  21. /**
  22.  * 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.
  23.  */
  24. public class MapUtils {
  25.     /**
  26.      * Clones a map.
  27.      *
  28.      * @param source the Map to clone.
  29.      * @param <K>    the map key type.
  30.      * @param <V>    the map value type.
  31.      * @return the cloned map.
  32.      */
  33.     public static <K, V> Map<K, V> copy(final Map<K, V> source) {
  34.         return source == null ? null : new HashMap<>(source);
  35.     }

  36.     /**
  37.      * Clones the lhs map and add all things from the rhs map.
  38.      *
  39.      * @param lhs the first map.
  40.      * @param rhs the second map.
  41.      * @param <K> the map key type.
  42.      * @param <V> the map value type.
  43.      * @return the merged map.
  44.      */
  45.     public static <K, V> Map<K, V> merge(final Map<K, V> lhs, final Map<K, V> rhs) {
  46.         Map<K, V> result = null;
  47.         if (lhs == null || lhs.isEmpty()) {
  48.             result = copy(rhs);
  49.         } else if (rhs == null || rhs.isEmpty()) {
  50.             result = copy(lhs);
  51.         } else {
  52.             result = copy(lhs);
  53.             result.putAll(rhs);
  54.         }
  55.         return result;
  56.     }

  57.     /**
  58.      * 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.
  59.      *
  60.      * @param source the source map.
  61.      * @param prefix the prefix used for all names.
  62.      * @param <K>    the map key type.
  63.      * @param <V>    the map value type.
  64.      * @return the clone of the source map.
  65.      */
  66.     public static <K, V> Map<String, V> prefix(final Map<K, V> source, final String prefix) {
  67.         if (source == null) {
  68.             return null;
  69.         }
  70.         final Map<String, V> result = new HashMap<>();
  71.         for (final Map.Entry<K, V> entry : source.entrySet()) {
  72.             result.put(prefix + '.' + Objects.toString(entry.getKey(), ""), entry.getValue());
  73.         }
  74.         return result;
  75.     }

  76.     /**
  77.      * Constructs a new instance.
  78.      *
  79.      * @deprecated Will be private in the next major version.
  80.      */
  81.     @Deprecated
  82.     public MapUtils() {
  83.         // empty
  84.     }
  85. }