📄 maputils.java
字号:
/*
* Copyright 2001-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections;
import java.io.PrintStream;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.collections.map.FixedSizeMap;
import org.apache.commons.collections.map.FixedSizeSortedMap;
import org.apache.commons.collections.map.LazyMap;
import org.apache.commons.collections.map.LazySortedMap;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.collections.map.PredicatedMap;
import org.apache.commons.collections.map.PredicatedSortedMap;
import org.apache.commons.collections.map.TransformedMap;
import org.apache.commons.collections.map.TransformedSortedMap;
import org.apache.commons.collections.map.TypedMap;
import org.apache.commons.collections.map.TypedSortedMap;
import org.apache.commons.collections.map.UnmodifiableMap;
import org.apache.commons.collections.map.UnmodifiableSortedMap;
/**
* Provides utility methods and decorators for
* {@link Map} and {@link SortedMap} instances.
* <p>
* It contains various type safe methods
* as well as other useful features like deep copying.
* <p>
* It also provides the following decorators:
*
* <ul>
* <li>{@link #fixedSizeMap(Map)}
* <li>{@link #fixedSizeSortedMap(SortedMap)}
* <li>{@link #lazyMap(Map,Factory)}
* <li>{@link #lazyMap(Map,Transformer)}
* <li>{@link #lazySortedMap(SortedMap,Factory)}
* <li>{@link #lazySortedMap(SortedMap,Transformer)}
* <li>{@link #predicatedMap(Map,Predicate,Predicate)}
* <li>{@link #predicatedSortedMap(SortedMap,Predicate,Predicate)}
* <li>{@link #transformedMap(Map, Transformer, Transformer)}
* <li>{@link #transformedSortedMap(SortedMap, Transformer, Transformer)}
* <li>{@link #typedMap(Map, Class, Class)}
* <li>{@link #typedSortedMap(SortedMap, Class, Class)}
* <li>{@link #multiValueMap( Map )}
* <li>{@link #multiValueMap( Map, Class )}
* <li>{@link #multiValueMap( Map, Factory )}
* </ul>
*
* @since Commons Collections 1.0
* @version $Revision: 357494 $ $Date: 2005-12-18 19:05:31 +0000 (Sun, 18 Dec 2005) $
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
* @author Paul Jack
* @author Stephen Colebourne
* @author Matthew Hawthorne
* @author Arun Mammen Thomas
* @author Janek Bogucki
* @author Max Rydahl Andersen
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @author <a href="mailto:jcarman@apache.org">James Carman</a>
* @author Neil O'Toole
*/
public class MapUtils {
/**
* An empty unmodifiable map.
* This was not provided in JDK1.2.
*/
public static final Map EMPTY_MAP = UnmodifiableMap.decorate(new HashMap(1));
/**
* An empty unmodifiable sorted map.
* This is not provided in the JDK.
*/
public static final SortedMap EMPTY_SORTED_MAP = UnmodifiableSortedMap.decorate(new TreeMap());
/**
* String used to indent the verbose and debug Map prints.
*/
private static final String INDENT_STRING = " ";
/**
* <code>MapUtils</code> should not normally be instantiated.
*/
public MapUtils() {
}
// Type safe getters
//-------------------------------------------------------------------------
/**
* Gets from a Map in a null-safe manner.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map, <code>null</code> if null map input
*/
public static Object getObject(final Map map, final Object key) {
if (map != null) {
return map.get(key);
}
return null;
}
/**
* Gets a String from a Map in a null-safe manner.
* <p>
* The String is obtained via <code>toString</code>.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a String, <code>null</code> if null map input
*/
public static String getString(final Map map, final Object key) {
if (map != null) {
Object answer = map.get(key);
if (answer != null) {
return answer.toString();
}
}
return null;
}
/**
* Gets a Boolean from a Map in a null-safe manner.
* <p>
* If the value is a <code>Boolean</code> it is returned directly.
* If the value is a <code>String</code> and it equals 'true' ignoring case
* then <code>true</code> is returned, otherwise <code>false</code>.
* If the value is a <code>Number</code> an integer zero value returns
* <code>false</code> and non-zero returns <code>true</code>.
* Otherwise, <code>null</code> is returned.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Boolean, <code>null</code> if null map input
*/
public static Boolean getBoolean(final Map map, final Object key) {
if (map != null) {
Object answer = map.get(key);
if (answer != null) {
if (answer instanceof Boolean) {
return (Boolean) answer;
} else if (answer instanceof String) {
return new Boolean((String) answer);
} else if (answer instanceof Number) {
Number n = (Number) answer;
return (n.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE;
}
}
}
return null;
}
/**
* Gets a Number from a Map in a null-safe manner.
* <p>
* If the value is a <code>Number</code> it is returned directly.
* If the value is a <code>String</code> it is converted using
* {@link NumberFormat#parse(String)} on the system default formatter
* returning <code>null</code> if the conversion fails.
* Otherwise, <code>null</code> is returned.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Number, <code>null</code> if null map input
*/
public static Number getNumber(final Map map, final Object key) {
if (map != null) {
Object answer = map.get(key);
if (answer != null) {
if (answer instanceof Number) {
return (Number) answer;
} else if (answer instanceof String) {
try {
String text = (String) answer;
return NumberFormat.getInstance().parse(text);
} catch (ParseException e) {
logInfo(e);
}
}
}
}
return null;
}
/**
* Gets a Byte from a Map in a null-safe manner.
* <p>
* The Byte is obtained from the results of {@link #getNumber(Map,Object)}.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Byte, <code>null</code> if null map input
*/
public static Byte getByte(final Map map, final Object key) {
Number answer = getNumber(map, key);
if (answer == null) {
return null;
} else if (answer instanceof Byte) {
return (Byte) answer;
}
return new Byte(answer.byteValue());
}
/**
* Gets a Short from a Map in a null-safe manner.
* <p>
* The Short is obtained from the results of {@link #getNumber(Map,Object)}.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Short, <code>null</code> if null map input
*/
public static Short getShort(final Map map, final Object key) {
Number answer = getNumber(map, key);
if (answer == null) {
return null;
} else if (answer instanceof Short) {
return (Short) answer;
}
return new Short(answer.shortValue());
}
/**
* Gets a Integer from a Map in a null-safe manner.
* <p>
* The Integer is obtained from the results of {@link #getNumber(Map,Object)}.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Integer, <code>null</code> if null map input
*/
public static Integer getInteger(final Map map, final Object key) {
Number answer = getNumber(map, key);
if (answer == null) {
return null;
} else if (answer instanceof Integer) {
return (Integer) answer;
}
return new Integer(answer.intValue());
}
/**
* Gets a Long from a Map in a null-safe manner.
* <p>
* The Long is obtained from the results of {@link #getNumber(Map,Object)}.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Long, <code>null</code> if null map input
*/
public static Long getLong(final Map map, final Object key) {
Number answer = getNumber(map, key);
if (answer == null) {
return null;
} else if (answer instanceof Long) {
return (Long) answer;
}
return new Long(answer.longValue());
}
/**
* Gets a Float from a Map in a null-safe manner.
* <p>
* The Float is obtained from the results of {@link #getNumber(Map,Object)}.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Float, <code>null</code> if null map input
*/
public static Float getFloat(final Map map, final Object key) {
Number answer = getNumber(map, key);
if (answer == null) {
return null;
} else if (answer instanceof Float) {
return (Float) answer;
}
return new Float(answer.floatValue());
}
/**
* Gets a Double from a Map in a null-safe manner.
* <p>
* The Double is obtained from the results of {@link #getNumber(Map,Object)}.
*
* @param map the map to use
* @param key the key to look up
* @return the value in the Map as a Double, <code>null</code> if null map input
*/
public static Double getDouble(final Map map, final Object key) {
Number answer = getNumber(map, key);
if (answer == null) {
return null;
} else if (answer instanceof Double) {
return (Double) answer;
}
return new Double(answer.doubleValue());
}
/**
* Gets a Map from a Map in a null-safe manner.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -