java常用工具类 UUID、Map工具类

时间:2021-05-19

本文实例为大家分享了Java常用工具类 的具体代码,供大家参考,具体内容如下

UUID工具类

package com.jarvis.base.util;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;/** * A class that represents an immutable universally unique identifier (UUID). * A UUID represents a 128-bit value. * <p/> * <p>There exist different variants of these global identifiers. The methods * of this class are for manipulating the Leach-Salz variant, although the * constructors allow the creation of any variant of UUID (described below). * <p/> * <p>The layout of a variant 2 (Leach-Salz) UUID is as follows: * <p/> * The most significant long consists of the following unsigned fields: * <pre> * 0xFFFFFFFF00000000 time_low * 0x00000000FFFF0000 time_mid * 0x000000000000F000 version * 0x0000000000000FFF time_hi * </pre> * The least significant long consists of the following unsigned fields: * <pre> * 0xC000000000000000 variant * 0x3FFF000000000000 clock_seq * 0x0000FFFFFFFFFFFF node * </pre> * <p/> * <p>The variant field contains a value which identifies the layout of * the <tt>UUID</tt>. The bit layout described above is valid only for * a <tt>UUID</tt> with a variant value of 2, which indicates the * Leach-Salz variant. * <p/> * <p>The version field holds a value that describes the type of this * <tt>UUID</tt>. There are four different basic types of UUIDs: time-based, * DCE security, name-based, and randomly generated UUIDs. These types * have a version value of 1, 2, 3 and 4, respectively. * <p/> * <p>For more information including algorithms used to create <tt>UUID</tt>s, * see the Internet-Draft <a href="http://.jarvis.base.util * @Description:Map工具类 * @version V1.0 */public class MapHelper { /** * 获得字串值 * * @param name * 键值名称 * @return 若不存在,则返回空字串 */ public static String getString(Map<?, ?> map, String name) { if (name == null || name.equals("")) { return ""; } String value = ""; if (map.containsKey(name) == false) { return ""; } Object obj = map.get(name); if (obj != null) { value = obj.toString(); } obj = null; return value; } /** * 返回整型值 * * @param name * 键值名称 * @return 若不存在,或转换失败,则返回0 */ public static int getInt(Map<?, ?> map, String name) { if (name == null || name.equals("")) { return 0; } int value = 0; if (map.containsKey(name) == false) { return 0; } Object obj = map.get(name); if (obj == null) { return 0; } if (!(obj instanceof Integer)) { try { value = Integer.parseInt(obj.toString()); } catch (Exception ex) { ex.printStackTrace(); System.err.println("name[" + name + "]对应的值不是数字,返回0"); value = 0; } } else { value = ((Integer) obj).intValue(); obj = null; } return value; } /** * 获取长整型值 * * @param name * 键值名称 * @return 若不存在,或转换失败,则返回0 */ public static long getLong(Map<?, ?> map, String name) { if (name == null || name.equals("")) { return 0; } long value = 0; if (map.containsKey(name) == false) { return 0; } Object obj = map.get(name); if (obj == null) { return 0; } if (!(obj instanceof Long)) { try { value = Long.parseLong(obj.toString()); } catch (Exception ex) { ex.printStackTrace(); System.err.println("name[" + name + "]对应的值不是数字,返回0"); value = 0; } } else { value = ((Long) obj).longValue(); obj = null; } return value; } /** * 获取Float型值 * * @param name * 键值名称 * @return 若不存在,或转换失败,则返回0 */ public static float getFloat(Map<?, ?> map, String name) { if (name == null || name.equals("")) { return 0; } float value = 0; if (map.containsKey(name) == false) { return 0; } Object obj = map.get(name); if (obj == null) { return 0; } if (!(obj instanceof Float)) { try { value = Float.parseFloat(obj.toString()); } catch (Exception ex) { ex.printStackTrace(); System.err.println("name[" + name + "]对应的值不是数字,返回0"); value = 0; } } else { value = ((Float) obj).floatValue(); obj = null; } return value; } /** * 获取Double型值 * * @param name * 键值名称 * @return 若不存在,或转换失败,则返回0 */ public static double getDouble(Map<?, ?> map, String name) { if (name == null || name.equals("")) { return 0; } double value = 0; if (map.containsKey(name) == false) { return 0; } Object obj = map.get(name); if (obj == null) { return 0; } if (!(obj instanceof Double)) { try { value = Double.parseDouble(obj.toString()); } catch (Exception ex) { ex.printStackTrace(); System.err.println("name[" + name + "]对应的值不是数字,返回0"); value = 0; } } else { value = ((Double) obj).doubleValue(); obj = null; } return value; } /** * 获取Bool值 * * @param name * 键值名称 * @return 若不存在,或转换失败,则返回false */ public static boolean getBoolean(Map<?, ?> map, String name) { if (name == null || name.equals("")) { return false; } boolean value = false; if (map.containsKey(name) == false) { return false; } Object obj = map.get(name); if (obj == null) { return false; } if (obj instanceof Boolean) { return ((Boolean) obj).booleanValue(); } value = Boolean.valueOf(obj.toString()).booleanValue(); obj = null; return value; }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章