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 */ 018 019package org.apache.commons.exec.util; 020 021import java.util.HashMap; 022import java.util.Map; 023 024/** 025 * Helper classes to manipulate maps to pass substition map to the CommandLine. This class is not part of the public API 026 * and could change without warning. 027 * 028 * @version $Id: MapUtils.java 1636205 2014-11-02 22:32:33Z ggregory $ 029 */ 030public class MapUtils 031{ 032 /** 033 * Clones a map. 034 * 035 * @param source 036 * the Map to clone 037 * @param <K> 038 * the map key type 039 * @param <V> 040 * the map value type 041 * @return the cloned map 042 */ 043 public static <K, V> Map<K, V> copy(final Map<K, V> source) { 044 045 if (source == null) { 046 return null; 047 } 048 049 final Map<K, V> result = new HashMap<K, V>(); 050 result.putAll(source); 051 return result; 052 } 053 054 /** 055 * Clones a map and prefixes the keys in the clone, e.g. for mapping "JAVA_HOME" to "env.JAVA_HOME" to simulate the 056 * behaviour of Ant. 057 * 058 * @param source 059 * the source map 060 * @param prefix 061 * the prefix used for all names 062 * @param <K> 063 * the map key type 064 * @param <V> 065 * the map value type 066 * @return the clone of the source map 067 */ 068 public static <K, V> Map<String, V> prefix(final Map<K, V> source, final String prefix) { 069 070 if (source == null) { 071 return null; 072 } 073 074 final Map<String, V> result = new HashMap<String, V>(); 075 076 for (final Map.Entry<K, V> entry : source.entrySet()) { 077 final K key = entry.getKey(); 078 final V value = entry.getValue(); 079 result.put(prefix + '.' + key.toString(), value); 080 } 081 082 return result; 083 } 084 085 /** 086 * Clones the lhs map and add all things from the rhs map. 087 * 088 * @param lhs 089 * the first map 090 * @param rhs 091 * the second map 092 * @param <K> 093 * the map key type 094 * @param <V> 095 * the map value type 096 * @return the merged map 097 */ 098 public static <K, V> Map<K, V> merge(final Map<K, V> lhs, final Map<K, V> rhs) { 099 100 Map<K, V> result = null; 101 102 if (lhs == null || lhs.size() == 0) { 103 result = copy(rhs); 104 } 105 else if (rhs == null || rhs.size() == 0) { 106 result = copy(lhs); 107 } 108 else { 109 result = copy(lhs); 110 result.putAll(rhs); 111 } 112 113 return result; 114 } 115}