import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * * <pre> * Copyright (C): 2012 * * Description: * 仿oracle的sequence的调用接口 * * Revision History: * 2012-10-23 initial version. * * </pre> */ public class SequenceUtil { private Map<String,ISequence> keyMap = new ConcurrentHashMap<String, ISequence>(); private static SequenceUtil _instance = new SequenceUtil(); private SequenceUtil(){}; public static SequenceUtil getInstance(){ return _instance; } /** * getSequence * @param sequenceName * @return */ private ISequence getSequence(String sequenceName){ // return new SequenceImpl(); return new AtomicLongSequence(sequenceName); } /** * 重置序列,其实就是从数据最目前为至最大的序列 * @param sequenceName 序列名称 */ public void resetSequence(String sequenceName){ if(keyMap.containsKey(sequenceName)){ ISequence sequence = keyMap.get(sequenceName); keyMap.remove(sequenceName); sequence = null; } } /** * 取得下一个序列 * @param sequenceName 序列名称 * @return */ public long getNextVal(String sequenceName){ ISequence sequence = null; if(keyMap.containsKey(sequenceName)){ sequence = keyMap.get(sequenceName); } else{ sequence = getSequence(sequenceName); keyMap.put(sequenceName, sequence); } return sequence.getNextVal(); } }
最近下载更多