zoukankan      html  css  js  c++  java
  • 自定义流水号。

    import java.text.Format;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
     
    /**
     * 
     * @author Phonnie
     *
     */
    public class StreamId {
         
        private static final int MAX_VALUE=9999;
        private static final String FORMAT = "yyMMdd";
        private static final Format DF= new SimpleDateFormat(FORMAT);
        private static final byte[] lock = new byte[0];
        private String prefix = null;
        private Date date = null;
        private int number=1;
        private static Map<String, StreamId> map = new HashMap<String, StreamId>();
         
        private StreamId(String prefix,Date date){
            this.prefix = prefix;
            this.date = date;
        }
         
        public static StreamId newInstance(String prefix){
            Date date = new Date();
            return newInstance(prefix,date);
        } 
         
        public static StreamId newInstance(String prefix,Date date){
            StreamId o = null;
            synchronized (lock) {
                String key = getKey(prefix, date);
                if(map.containsKey(key)){
                    o = map.get(key);
                    int number = o.getNumber();
                    if(number<MAX_VALUE){
                        o.setNumber(number+1);
                    }else {
                        o.setNumber(1);
                    }
                     
                } else {
                     o = new StreamId(prefix,date);
                     map.put(key, o);
                }
            }
            return o;
        }
         
         
         
        private static String getKey(String prefix,Date date){
            return prefix+format(date);
        }
     
        private static String format(Date date){
            return DF.format(date);
        }
         
        public String toString(){
            return  prefix+ format(date) + String.format("%04d", number);
        }
     
        public void setNumber(int number) {
            this.number = number;
        }
     
        public int getNumber() {
            return number;
        }
        
        public static void main(String[] args){
            StreamId sid=newInstance("AA") ;
            System.out.println("StreamId.main().sid="+sid.toString());
            StreamId sida=newInstance("AA") ;
            System.out.println("StreamId.main().sid="+sida.toString());
            
            
        }
    }
  • 相关阅读:
    linux查看存储盘
    aix中hd5对应什么设备?
    (转)Python 操作 Windows 粘贴板
    eclipse非主窗口的停靠(正常), 恢复, 最小化, 最大化的切换
    (转)HTML5 本地数据库(SQLite) 示例
    [译] 如何像 Python 高手一样编程?
    scrapy递归下载网站
    eclipse快捷键以及使用技巧大全
    python任意编码转utf8或者unicode
    apache快速配置简易网站
  • 原文地址:https://www.cnblogs.com/harry335/p/3957807.html
Copyright © 2011-2022 走看看