| New file | 
|  |  |  | 
|---|
|  |  |  | package com.dy.pipIrrWebFile.fm; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Calendar; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public class FileNameIdUtil { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static int add = 0 ; | 
|---|
|  |  |  | private static int chengShu = 1000 ; | 
|---|
|  |  |  | private static int maxAdd = 999 ; | 
|---|
|  |  |  | private static long last = 0 ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //后缀 | 
|---|
|  |  |  | private static String suffix = "0" ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | static { | 
|---|
|  |  |  | last = current() ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 为自实现程序提供的ID生成器 | 
|---|
|  |  |  | * 15长度ID,年度取两位,如果是17位长度ID,年度取四位,那17位数字超出了javascript的表数范围 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public  String generate(){ | 
|---|
|  |  |  | return doGenerate() ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 设置后缀,不同子系统设置不同的后缀 | 
|---|
|  |  |  | * @param suffix | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static void setSuffix(String suffix)throws Exception{ | 
|---|
|  |  |  | if(suffix == null || suffix.trim().equals("")){ | 
|---|
|  |  |  | throw new Exception("后缀不能为空") ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | FileNameIdUtil.suffix = suffix.trim() ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 执行生成 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private synchronized String doGenerate(){ | 
|---|
|  |  |  | Long id = null ; | 
|---|
|  |  |  | long now = current() ; | 
|---|
|  |  |  | if(now != last){ | 
|---|
|  |  |  | //上次生成ID 与本次生成ID 不在同一秒内 | 
|---|
|  |  |  | last = now ; | 
|---|
|  |  |  | add = 0 ; | 
|---|
|  |  |  | id = last * chengShu + add ++; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //上次生成ID 与本次生成ID 在同一秒内 | 
|---|
|  |  |  | if(add == maxAdd){ | 
|---|
|  |  |  | //附加量已经用尽 | 
|---|
|  |  |  | waitNextSecond(last) ;//等到下一秒 | 
|---|
|  |  |  | id = last * chengShu + add ++ ;//返回上一秒生成的ID | 
|---|
|  |  |  | add = 0 ;//附加量归零,为下一秒准备 | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | //附加量未用尽 | 
|---|
|  |  |  | id = last * chengShu + add ++ ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return id + suffix ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 等待下一秒到来 | 
|---|
|  |  |  | * @param last | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void waitNextSecond(Long last){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(10); | 
|---|
|  |  |  | } catch (InterruptedException e) { | 
|---|
|  |  |  | }finally{ | 
|---|
|  |  |  | long now = current() ; | 
|---|
|  |  |  | if(now == last){ | 
|---|
|  |  |  | waitNextSecond(last) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 格式为 150516010203 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private static long current(){ | 
|---|
|  |  |  | Calendar cal = Calendar.getInstance(); | 
|---|
|  |  |  | long d = (cal.get(Calendar.YEAR) % 100) * 10000000000L | 
|---|
|  |  |  | + (cal.get(Calendar.MONTH) + 1) * 100000000L | 
|---|
|  |  |  | + cal.get(Calendar.DAY_OF_MONTH) * 1000000L | 
|---|
|  |  |  | + cal.get(Calendar.HOUR_OF_DAY) * 10000L | 
|---|
|  |  |  | + cal.get(Calendar.MINUTE) * 100L | 
|---|
|  |  |  | + cal.get(Calendar.SECOND)  ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return d ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String args[]){ | 
|---|
|  |  |  | FileNameIdUtil o = new FileNameIdUtil() ; | 
|---|
|  |  |  | int total = 800 ; | 
|---|
|  |  |  | long start = System.currentTimeMillis() ; | 
|---|
|  |  |  | for(int i = 0 ; i < total ; i++){ | 
|---|
|  |  |  | System.out.println((String)(o.generate())) ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | long end = System.currentTimeMillis() ; | 
|---|
|  |  |  | System.out.println("产生" + total + "ID用时" + (end - start) + "毫秒"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|