⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keygenerator.java

📁 j2ee项目开发
💻 JAVA
字号:
package com.util;

import java.net.InetAddress;

/**
 * Key Generator
 * <p>Description: 产生唯一id</p>
 * @author FWX
 * @version 1.0
 */
public class KeyGenerator {
  private static final long IP;
  static {
    long ipadd;
    try {
      ipadd = toLong(InetAddress.getLocalHost().getAddress());
    }
    catch (Exception e) {
      ipadd = 0;
    }
    IP = ipadd;
  }

  private static short counter = (short) 0;
  private static final int JVM = (int) (System.currentTimeMillis()>>>8);

  private KeyGenerator() {
  }

  public static Long generateKey() {
    long keyValue;
    keyValue = IP + JVM + getCount() + System.currentTimeMillis();
    return new Long(keyValue);
  }

  private static short getCount() {
    synchronized (KeyGenerator.class) {
      if (counter < 0) {
        counter = 0;
      }
      return counter++;
    }
  }

  /**
   *
   * @param bytes byte[]
   * @return int
   */
  private static long toLong( byte[] bytes ) {
      long result = 0;
      for (int i=0; i<bytes.length; i++) {
          result = ( result << 8 ) - Byte.MIN_VALUE + (int) bytes[i];
      }
      return result;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -