📄 maputils.java
字号:
/* * Copyright 1999-2004 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.*;
import java.text.*;
import java.text.NumberFormat;
import java.util.*;
/**
* A helper class for using {@link Map Map} instances.<P>
*
* It contains various typesafe 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 #lazySortedMap(SortedMap,Factory)}
* <LI>{@link #predicatedMap(Map,Predicate,Predicate)}
* <LI>{@link #predicatedSortedMap(SortedMap,Predicate,Predicate)}
* </UL>
*
* @since 1.0
* @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
*/
public class MapUtils {
private static int debugIndent = 0;
/**
* Please don't instantiate a <Code>MapUtils</Code>.
*/
public MapUtils() {
}
// Type safe getters
//-------------------------------------------------------------------------
/**
* Synonym for {@link Map#get(Object)}.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return null if the map is null; or the result of
* <Code>map.get(key)</Code>
*/
public static Object getObject( Map map, Object key ) {
if ( map != null ) {
return map.get( key );
}
return null;
}
/**
* Looks up the given key in the given map, converting the result into
* a string.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return null if the map is null; null if the value mapped by that
* key is null; or the <Code>toString()</Code>
* result of the value for that key
*/
public static String getString( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
return answer.toString();
}
}
return null;
}
/**
* Looks up the given key in the given map, converting the result into
* a {@link Boolean}. If the map is null, this method returns null.
* If the value mapped by the given key is a
* {@link Boolean}, then it is returned as-is. Otherwise, if the value
* is a string, then if that string ignoring case equals "true", then
* a true {@link Boolean} is returned. Any other string value will
* result in a false {@link Boolean} being returned. OR, if the value
* is a {@link Number}, and that {@link Number} is 0, then a false
* {@link Boolean} is returned. Any other {@link Number} value results
* in a true {@link Boolean} being returned.<P>
*
* Any value that is not a {@link Boolean}, {@link String} or
* {@link Number} results in null being returned.<P>
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return a {@link Boolean} or null
*/
public static Boolean getBoolean( Map map, 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;
}
/**
* Looks up the given key in the given map, converting the result into
* a {@link Number}. If the map is null, this method returns null.
* Otherwise, if the key maps to a {@link Number}, then that number
* is returned as-is. Otherwise, if the key maps to a {@link String},
* that string is parsed into a number using the system default
* {@link NumberFormat}.<P>
*
* If the value is not a {@link Number} or a {@link String}, or if
* the value is a {@link String} that cannot be parsed into a
* {@link Number}, then null is returned.<P>
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return a {@link Number} or null
*/
public static Number getNumber( Map map, 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;
}
/**
* Looks up the given key in the given map, converting the result into
* a {@link Byte}. First, {@link #getNumber(Map,Object)} is invoked.
* If the result is null, then null is returned. Otherwise, the
* byte value of the resulting {@link Number} is returned.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return a {@link Byte} or null
*/
public static Byte getByte( Map map, 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() );
}
/**
* Looks up the given key in the given map, converting the result into
* a {@link Short}. First, {@link #getNumber(Map,Object)} is invoked.
* If the result is null, then null is returned. Otherwise, the
* short value of the resulting {@link Number} is returned.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return a {@link Short} or null
*/
public static Short getShort( Map map, 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() );
}
/**
* Looks up the given key in the given map, converting the result into
* an {@link Integer}. First, {@link #getNumber(Map,Object)} is invoked.
* If the result is null, then null is returned. Otherwise, the
* integer value of the resulting {@link Number} is returned.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return an {@link Integer} or null
*/
public static Integer getInteger( Map map, 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() );
}
/**
* Looks up the given key in the given map, converting the result into
* a {@link Long}. First, {@link #getNumber(Map,Object)} is invoked.
* If the result is null, then null is returned. Otherwise, the
* long value of the resulting {@link Number} is returned.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return a {@link Long} or null
*/
public static Long getLong( Map map, 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() );
}
/**
* Looks up the given key in the given map, converting the result into
* a {@link Float}. First, {@link #getNumber(Map,Object)} is invoked.
* If the result is null, then null is returned. Otherwise, the
* float value of the resulting {@link Number} is returned.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return a {@link Float} or null
*/
public static Float getFloat( Map map, 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() );
}
/**
* Looks up the given key in the given map, converting the result into
* a {@link Double}. First, {@link #getNumber(Map,Object)} is invoked.
* If the result is null, then null is returned. Otherwise, the
* double value of the resulting {@link Number} is returned.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return a {@link Double} or null
*/
public static Double getDouble( Map map, 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() );
}
/**
* Looks up the given key in the given map, returning another map.
* If the given map is null or if the given key doesn't map to another
* map, then this method returns null. Otherwise the mapped map is
* returned.
*
* @param map the map whose value to look up
* @param key the key whose value to look up in that map
* @return a {@link Map} or null
*/
public static Map getMap( Map map, Object key ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null && answer instanceof Map ) {
return (Map) answer;
}
}
return null;
}
// Type safe getters with default values
//-------------------------------------------------------------------------
/**
* Looks up the given key in the given map, converting null into the
* given default value.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null
* @return the value in the map, or defaultValue if the original value
* is null or the map is null
*/
public static Object getObject( Map map, Object key, Object defaultValue ) {
if ( map != null ) {
Object answer = map.get( key );
if ( answer != null ) {
return answer;
}
}
return defaultValue;
}
/**
* Looks up the given key in the given map, converting the result into
* a string, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return the value in the map as a string, or defaultValue if the
* original value is null, the map is null or the string conversion
* fails
*/
public static String getString( Map map, Object key, String defaultValue ) {
String answer = getString( map, key );
if ( answer == null ) {
answer = defaultValue;
}
return answer;
}
/**
* Looks up the given key in the given map, converting the result into
* a boolean, using the default value if the the conversion fails.
*
* @param map the map whose value to look up
* @param key the key of the value to look up in that map
* @param defaultValue what to return if the value is null or if the
* conversion fails
* @return the value in the map as a boolean, or defaultValue if the
* original value is null, the map is null or the boolean conversion
* fails
*/
public static Boolean getBoolean( Map map, Object key, Boolean defaultValue ) {
Boolean answer = getBoolean( map, key );
if ( answer == null ) {
answer = defaultValue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -