首页

关于flink-core源码包中PropertiesUtil属性工具类对获取不同数据类型键值源码示例

标签:flink,PropertiesUtil,属性工具类     发布时间:2018-03-29   

一、前言

关于flink-core源码包中的org.apache.flink.util.PropertiesUtils属性工具类,对获取java.util.Properties属性转为不同数据类型getInt、getLong、getBoolean等操作。

二、源码说明

package org.apache.flink.util;@b@@b@import org.slf4j.Logger;@b@import java.util.Properties;@b@@b@public class PropertiesUtil {@b@@b@	/**@b@	 * Get integer from properties.@b@	 * This method throws an exception if the integer is not valid.@b@	 *@b@	 * @param config Properties@b@	 * @param key key in Properties@b@	 * @param defaultValue default value if value is not set@b@	 * @return default or value of key@b@	 * @throws IllegalArgumentException@b@	 */@b@	public static int getInt(Properties config, String key, int defaultValue) {@b@		String val = config.getProperty(key);@b@		if (val == null) {@b@			return defaultValue;@b@		} else {@b@			try {@b@				return Integer.parseInt(val);@b@			} catch (NumberFormatException nfe) {@b@				throw new IllegalArgumentException("Value for configuration key='" + key + "' is not set correctly. " +@b@						"Entered value='" + val + "'. Default value='" + defaultValue + "'");@b@			}@b@		}@b@	}@b@@b@	/**@b@	 * Get long from properties.@b@	 * This method throws an exception if the long is not valid.@b@	 *@b@	 * @param config Properties@b@	 * @param key key in Properties@b@	 * @param defaultValue default value if value is not set@b@	 * @return default or value of key@b@	 * @throws IllegalArgumentException@b@	 */@b@	public static long getLong(Properties config, String key, long defaultValue) {@b@		String val = config.getProperty(key);@b@		if (val == null) {@b@			return defaultValue;@b@		} else {@b@			try {@b@				return Long.parseLong(val);@b@			} catch (NumberFormatException nfe) {@b@				throw new IllegalArgumentException("Value for configuration key='" + key + "' is not set correctly. " +@b@						"Entered value='" + val + "'. Default value='" + defaultValue + "'");@b@			}@b@		}@b@	}@b@@b@	/**@b@	 * Get long from properties.@b@	 * This method only logs if the long is not valid.@b@	 *@b@	 * @param config Properties@b@	 * @param key key in Properties@b@	 * @param defaultValue default value if value is not set@b@	 * @return default or value of key@b@	 * @throws IllegalArgumentException@b@	 */@b@	public static long getLong(Properties config, String key, long defaultValue, Logger logger) {@b@		try {@b@			return getLong(config, key, defaultValue);@b@		} catch(IllegalArgumentException iae) {@b@			logger.warn(iae.getMessage());@b@			return defaultValue;@b@		}@b@	}@b@@b@	/**@b@	 * Get boolean from properties.@b@	 * This method returns {@code true} iff the parsed value is "true".@b@	 *@b@	 * @param config Properties@b@	 * @param key key in Properties@b@	 * @param defaultValue default value if value is not set@b@	 * @return default or value of key@b@	 */@b@	public static boolean getBoolean(Properties config, String key, boolean defaultValue) {@b@		String val = config.getProperty(key);@b@		if (val == null) {@b@			return defaultValue;@b@		} else {@b@			return Boolean.parseBoolean(val);@b@		}@b@	}@b@@b@	// ------------------------------------------------------------------------@b@	@b@	/** Private default constructor to prevent instantiation */@b@	private PropertiesUtil() {}@b@}