java常用工具类 UUID、Map工具类
程序员文章站
2023-11-05 17:27:28
本文实例为大家分享了java常用工具类 的具体代码,供大家参考,具体内容如下
uuid工具类
package com.jarvis.base.util;...
本文实例为大家分享了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://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-03.txt" rel="external nofollow" >uuids and guids</a> * or the standards body definition at * <a href="http://www.iso.ch/cate/d2229.html" rel="external nofollow" >iso/iec 11578:1996</a>. * * @version 1.14, 07/12/04 * @since 1.5 */ @deprecated public final class uuid implements java.io.serializable { /** * explicit serialversionuid for interoperability. */ private static final long serialversionuid = -4856846361193249489l; /* * the most significant 64 bits of this uuid. * * @serial */ private final long mostsigbits; /** * the least significant 64 bits of this uuid. * * @serial */ private final long leastsigbits; /* * the version number associated with this uuid. computed on demand. */ private transient int version = -1; /* * the variant number associated with this uuid. computed on demand. */ private transient int variant = -1; /* * the timestamp associated with this uuid. computed on demand. */ private transient volatile long timestamp = -1; /* * the clock sequence associated with this uuid. computed on demand. */ private transient int sequence = -1; /* * the node number associated with this uuid. computed on demand. */ private transient long node = -1; /* * the hashcode of this uuid. computed on demand. */ private transient int hashcode = -1; /* * the random number generator used by this class to create random * based uuids. */ private static volatile securerandom numbergenerator = null; // constructors and factories /* * private constructor which uses a byte array to construct the new uuid. */ private uuid(byte[] data) { long msb = 0; long lsb = 0; for (int i = 0; i < 8; i++) msb = (msb << 8) | (data[i] & 0xff); for (int i = 8; i < 16; i++) lsb = (lsb << 8) | (data[i] & 0xff); this.mostsigbits = msb; this.leastsigbits = lsb; } /** * constructs a new <tt>uuid</tt> using the specified data. * <tt>mostsigbits</tt> is used for the most significant 64 bits * of the <tt>uuid</tt> and <tt>leastsigbits</tt> becomes the * least significant 64 bits of the <tt>uuid</tt>. * * @param mostsigbits * @param leastsigbits */ public uuid(long mostsigbits, long leastsigbits) { this.mostsigbits = mostsigbits; this.leastsigbits = leastsigbits; } /** * static factory to retrieve a type 4 (pseudo randomly generated) uuid. * <p/> * the <code>uuid</code> is generated using a cryptographically strong * pseudo random number generator. * * @return a randomly generated <tt>uuid</tt>. */ @suppresswarnings("unused") public static uuid randomuuid() { securerandom ng = numbergenerator; if (ng == null) { numbergenerator = ng = new securerandom(); } byte[] randombytes = new byte[16]; ng.nextbytes(randombytes); randombytes[6] &= 0x0f; /* clear version */ randombytes[6] |= 0x40; /* set to version 4 */ randombytes[8] &= 0x3f; /* clear variant */ randombytes[8] |= 0x80; /* set to ietf variant */ uuid result = new uuid(randombytes); return new uuid(randombytes); } /** * static factory to retrieve a type 3 (name based) <tt>uuid</tt> based on * the specified byte array. * * @param name a byte array to be used to construct a <tt>uuid</tt>. * @return a <tt>uuid</tt> generated from the specified array. */ public static uuid nameuuidfrombytes(byte[] name) { messagedigest md; try { md = messagedigest.getinstance("md5"); } catch (nosuchalgorithmexception nsae) { throw new internalerror("md5 not supported"); } byte[] md5bytes = md.digest(name); md5bytes[6] &= 0x0f; /* clear version */ md5bytes[6] |= 0x30; /* set to version 3 */ md5bytes[8] &= 0x3f; /* clear variant */ md5bytes[8] |= 0x80; /* set to ietf variant */ return new uuid(md5bytes); } /** * creates a <tt>uuid</tt> from the string standard representation as * described in the {@link #tostring} method. * * @param name a string that specifies a <tt>uuid</tt>. * @return a <tt>uuid</tt> with the specified value. * @throws illegalargumentexception if name does not conform to the * string representation as described in {@link #tostring}. */ public static uuid fromstring(string name) { string[] components = name.split("-"); if (components.length != 5) throw new illegalargumentexception("invalid uuid string: " + name); for (int i = 0; i < 5; i++) components[i] = "0x" + components[i]; long mostsigbits = long.decode(components[0]).longvalue(); mostsigbits <<= 16; mostsigbits |= long.decode(components[1]).longvalue(); mostsigbits <<= 16; mostsigbits |= long.decode(components[2]).longvalue(); long leastsigbits = long.decode(components[3]).longvalue(); leastsigbits <<= 48; leastsigbits |= long.decode(components[4]).longvalue(); return new uuid(mostsigbits, leastsigbits); } // field accessor methods /** * returns the least significant 64 bits of this uuid's 128 bit value. * * @return the least significant 64 bits of this uuid's 128 bit value. */ public long getleastsignificantbits() { return leastsigbits; } /** * returns the most significant 64 bits of this uuid's 128 bit value. * * @return the most significant 64 bits of this uuid's 128 bit value. */ public long getmostsignificantbits() { return mostsigbits; } /** * the version number associated with this <tt>uuid</tt>. the version * number describes how this <tt>uuid</tt> was generated. * <p/> * the version number has the following meaning:<p> * <ul> * <li>1 time-based uuid * <li>2 dce security uuid * <li>3 name-based uuid * <li>4 randomly generated uuid * </ul> * * @return the version number of this <tt>uuid</tt>. */ public int version() { if (version < 0) { // version is bits masked by 0x000000000000f000 in ms long version = (int) ((mostsigbits >> 12) & 0x0f); } return version; } /** * the variant number associated with this <tt>uuid</tt>. the variant * number describes the layout of the <tt>uuid</tt>. * <p/> * the variant number has the following meaning:<p> * <ul> * <li>0 reserved for ncs backward compatibility * <li>2 the leach-salz variant (used by this class) * <li>6 reserved, microsoft corporation backward compatibility * <li>7 reserved for future definition * </ul> * * @return the variant number of this <tt>uuid</tt>. */ public int variant() { if (variant < 0) { // this field is composed of a varying number of bits if ((leastsigbits >>> 63) == 0) { variant = 0; } else if ((leastsigbits >>> 62) == 2) { variant = 2; } else { variant = (int) (leastsigbits >>> 61); } } return variant; } /** * the timestamp value associated with this uuid. * <p/> * <p>the 60 bit timestamp value is constructed from the time_low, * time_mid, and time_hi fields of this <tt>uuid</tt>. the resulting * timestamp is measured in 100-nanosecond units since midnight, * october 15, 1582 utc.<p> * <p/> * the timestamp value is only meaningful in a time-based uuid, which * has version type 1. if this <tt>uuid</tt> is not a time-based uuid then * this method throws unsupportedoperationexception. * * @throws unsupportedoperationexception if this uuid is not a * version 1 uuid. */ public long timestamp() { if (version() != 1) { throw new unsupportedoperationexception("not a time-based uuid"); } long result = timestamp; if (result < 0) { result = (mostsigbits & 0x0000000000000fffl) << 48; result |= ((mostsigbits >> 16) & 0xffffl) << 32; result |= mostsigbits >>> 32; timestamp = result; } return result; } /** * the clock sequence value associated with this uuid. * <p/> * <p>the 14 bit clock sequence value is constructed from the clock * sequence field of this uuid. the clock sequence field is used to * guarantee temporal uniqueness in a time-based uuid.<p> * <p/> * the clocksequence value is only meaningful in a time-based uuid, which * has version type 1. if this uuid is not a time-based uuid then * this method throws unsupportedoperationexception. * * @return the clock sequence of this <tt>uuid</tt>. * @throws unsupportedoperationexception if this uuid is not a * version 1 uuid. */ public int clocksequence() { if (version() != 1) { throw new unsupportedoperationexception("not a time-based uuid"); } if (sequence < 0) { sequence = (int) ((leastsigbits & 0x3fff000000000000l) >>> 48); } return sequence; } /** * the node value associated with this uuid. * <p/> * <p>the 48 bit node value is constructed from the node field of * this uuid. this field is intended to hold the ieee 802 address * of the machine that generated this uuid to guarantee spatial * uniqueness.<p> * <p/> * the node value is only meaningful in a time-based uuid, which * has version type 1. if this uuid is not a time-based uuid then * this method throws unsupportedoperationexception. * * @return the node value of this <tt>uuid</tt>. * @throws unsupportedoperationexception if this uuid is not a * version 1 uuid. */ public long node() { if (version() != 1) { throw new unsupportedoperationexception("not a time-based uuid"); } if (node < 0) { node = leastsigbits & 0x0000ffffffffffffl; } return node; } // object inherited methods /** * returns a <code>string</code> object representing this * <code>uuid</code>. * <p/> * <p>the uuid string representation is as described by this bnf : * <pre> * uuid = <time_low> "-" <time_mid> "-" * <time_high_and_version> "-" * <variant_and_sequence> "-" * <node> * time_low = 4*<hexoctet> * time_mid = 2*<hexoctet> * time_high_and_version = 2*<hexoctet> * variant_and_sequence = 2*<hexoctet> * node = 6*<hexoctet> * hexoctet = <hexdigit><hexdigit> * hexdigit = * "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" * | "a" | "b" | "c" | "d" | "e" | "f" * | "a" | "b" | "c" | "d" | "e" | "f" * </pre> * * @return a string representation of this <tt>uuid</tt>. */ public string tostring() { return (digits(mostsigbits >> 32, 8) + "-" + digits(mostsigbits >> 16, 4) + "-" + digits(mostsigbits, 4) + "-" + digits(leastsigbits >> 48, 4) + "-" + digits(leastsigbits, 12)); } /** * returns val represented by the specified number of hex digits. */ private static string digits(long val, int digits) { long hi = 1l << (digits * 4); return long.tohexstring(hi | (val & (hi - 1))).substring(1); } /** * returns a hash code for this <code>uuid</code>. * * @return a hash code value for this <tt>uuid</tt>. */ public int hashcode() { if (hashcode == -1) { hashcode = (int) ((mostsigbits >> 32) ^ mostsigbits ^ (leastsigbits >> 32) ^ leastsigbits); } return hashcode; } /** * compares this object to the specified object. the result is * <tt>true</tt> if and only if the argument is not * <tt>null</tt>, is a <tt>uuid</tt> object, has the same variant, * and contains the same value, bit for bit, as this <tt>uuid</tt>. * * @param obj the object to compare with. * @return <code>true</code> if the objects are the same; * <code>false</code> otherwise. */ public boolean equals(object obj) { if (!(obj instanceof uuid)) return false; if (((uuid) obj).variant() != this.variant()) return false; uuid id = (uuid) obj; return (mostsigbits == id.mostsigbits && leastsigbits == id.leastsigbits); } // comparison operations /** * compares this uuid with the specified uuid. * <p/> * <p>the first of two uuids follows the second if the most significant * field in which the uuids differ is greater for the first uuid. * * @param val <tt>uuid</tt> to which this <tt>uuid</tt> is to be compared. * @return -1, 0 or 1 as this <tt>uuid</tt> is less than, equal * to, or greater than <tt>val</tt>. */ public int compareto(uuid val) { // the ordering is intentionally set up so that the uuids // can simply be numerically compared as two numbers return (this.mostsigbits < val.mostsigbits ? -1 : (this.mostsigbits > val.mostsigbits ? 1 : (this.leastsigbits < val.leastsigbits ? -1 : (this.leastsigbits > val.leastsigbits ? 1 : 0)))); } /** * reconstitute the <tt>uuid</tt> instance from a stream (that is, * deserialize it). this is necessary to set the transient fields * to their correct uninitialized value so they will be recomputed * on demand. */ private void readobject(java.io.objectinputstream in) throws java.io.ioexception, classnotfoundexception { in.defaultreadobject(); // set "cached computation" fields to their initial values version = -1; variant = -1; timestamp = -1; sequence = -1; node = -1; hashcode = -1; } }
map工具类
package com.jarvis.base.util; import java.util.map; /** * * * @title: maphelper.java * @package com.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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 久闻网作家后台操作说明