📄 sequence.java
字号:
package cn.myapps.util.sequence;
import java.util.UUID;
import cn.myapps.constans.Framework;
import cn.myapps.core.counter.ejb.CounterProcess;
import cn.myapps.util.ProcessFactory;
import cn.myapps.util.StringUtil;
/**
* The system sequence utility.
*/
public class Sequence {
private final static int MAX_DEPTS_PER_LEVEL = 100;
private final static int MAX_GROUP_PER_LEVEL = 100;
private final static int MAX_PERSONS_PER_LEVEL = 1000;
private static final int base = 100000;
private static long millis = 0, old = 0;
private final static int MAX_TOPIC_NUMBER = 1000000;
private final static int MAX_TOPICCHILD_NUMBER = 1000;
private final static int MAX_TITLE_COUNT = 10000;
/**
* Get the base Sequence.
*
* @return the Sequence
* @throws SequenceException
*/
public static synchronized String getSequence() throws SequenceException {
return UUID.randomUUID().toString();
}
public static synchronized String getTimeSequence()
throws SequenceException {
long result = System.currentTimeMillis();
if (result == millis) {
old++;
if (old >= base)
throw new SequenceException(
"It had exceed the maxium sequence in this moment.");
result = millis * base + old;
} else {
millis = result;
result *= base;
old = 0;
}
return result + "";
}
/**
* Get the next department number
*
* @param superiorid
* The superior department number.
* @return The next department number
* @throws SequenceException
*/
public static synchronized String getDeptId(String superiorid,
String applicationid) throws SequenceException {
if (superiorid == null || superiorid.trim().length() == 0) {
return null;
}
int counter;
try {
CounterProcess delegate = (CounterProcess) ProcessFactory
.createProcess(CounterProcess.class);
counter = delegate.getNextValue(superiorid, applicationid);
} catch (Exception ex) {
throw new SequenceException("Cann't get next department number : "
+ superiorid + ",detail error message:" + ex.getMessage());
}
if (counter >= MAX_DEPTS_PER_LEVEL) {
throw new SequenceException(
"It had exceed the maxium department number: " + superiorid);
}
return superiorid + "-"
+ StringUtil.leftPad(String.valueOf(counter), 2, "0");
}
/**
* Get the next group number
*
* @param superiorid
* The superior group number
* @return The next group number
* @throws SequenceException
*/
public static synchronized String getGroupId(String superiorid,
String applicationid) throws SequenceException {
if (superiorid == null || superiorid.trim().length() == 0) {
return null;
}
superiorid = StringUtil.replaceOnce(superiorid,
Framework.ROOT_DEPARTMENT, Framework.INTERNAL_USERGROUP_PREFIX);
int counter;
try {
CounterProcess delegate = (CounterProcess) ProcessFactory
.createProcess(CounterProcess.class);
counter = delegate.getNextValue(superiorid, applicationid);
} catch (Exception ex) {
throw new SequenceException("Cann't get next group number : "
+ superiorid + ",detail error message:" + ex.getMessage());
}
if (counter >= MAX_GROUP_PER_LEVEL) {
throw new SequenceException(
"It had exceed the maxium group number: " + superiorid);
}
return superiorid + "-"
+ StringUtil.leftPad(String.valueOf(counter), 2, "0");
}
/**
* Get the next user number
*
* @param superiorid
* The superior user number
* @return The next user number
* @throws SequenceException
*/
public static synchronized String getUserId(String superiorid,
String applicationid) throws SequenceException {
if (superiorid == null || superiorid.trim().length() == 0) {
return null;
}
superiorid = StringUtil.replaceOnce(superiorid,
Framework.ROOT_DEPARTMENT, Framework.INTERNAL_USER_PREFIX);
int counter;
try {
CounterProcess delegate = (CounterProcess) ProcessFactory
.createProcess(CounterProcess.class);
counter = delegate.getNextValue(superiorid, applicationid);
} catch (Exception ex) {
throw new SequenceException("Cann't get next user number : "
+ superiorid + ",detail error message:" + ex.getMessage());
}
if (counter >= MAX_PERSONS_PER_LEVEL) {
throw new SequenceException(
"It had exceed the maxium user number: " + superiorid);
}
return superiorid + "-"
+ StringUtil.leftPad(String.valueOf(counter), 3, "0");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -