package com.system.bus;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class ReactionTimeUtil {
private static final DecimalFormat decimalFormat = new DecimalFormat("#0.00");
public static final String HOUR = "H";
public static final String MINUTE = "M";
public static final String SECOND = "S";
public static String ConvertReactionTimePhaseValue(String reactionTimeStr,String toTimeUnit,Boolean isLeft) {
StringBuffer resultBuffer = new StringBuffer();
String gtMatchExpressionStr = "≥>>﹥≧";
String ltMatchExpressionStr = "<≤﹤≦";
String startTimeStr = "";
String endTimeStr = "";
String gtTimeUnit = "";
String ltTimeUnit = "";
reactionTimeStr = reactionTimeStr.replaceAll(" ", "");
if(reactionTimeStr.contains("-")) {
String[] reactionTimeArray = reactionTimeStr.split("-");
startTimeStr = reactionTimeArray[0];
endTimeStr = reactionTimeArray[1];
String startTimeUnit = getTimeUint(startTimeStr);
String endTimeUnit = getTimeUint(endTimeStr);
if("".equals(startTimeUnit) && "".equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,HOUR ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,HOUR ,toTimeUnit);
}
if("".equals(startTimeUnit) && HOUR.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,HOUR ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,HOUR ,toTimeUnit);
}
if("".equals(startTimeUnit) && MINUTE.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,MINUTE ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,MINUTE ,toTimeUnit);
}
if("".equals(startTimeUnit) && SECOND.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,SECOND ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,SECOND ,toTimeUnit);
}
if(HOUR.equals(startTimeUnit) && "".equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,HOUR ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,HOUR ,toTimeUnit);
}
if(HOUR.equals(startTimeUnit) && HOUR.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,HOUR ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,HOUR ,toTimeUnit);
}
if(MINUTE.equals(startTimeUnit) && "".equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,MINUTE ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,HOUR ,toTimeUnit);
}
if(SECOND.equals(startTimeUnit) && "".equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,SECOND ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,HOUR ,toTimeUnit);
}
if(MINUTE.equals(startTimeUnit) && HOUR.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,MINUTE ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,HOUR ,toTimeUnit);
}
if(SECOND.equals(startTimeUnit) && HOUR.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,SECOND ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,HOUR ,toTimeUnit);
}
if(SECOND.equals(startTimeUnit) && SECOND.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,SECOND ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,SECOND ,toTimeUnit);
}
if(SECOND.equals(startTimeUnit) && MINUTE.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,SECOND ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,MINUTE ,toTimeUnit);
}
if(MINUTE.equals(startTimeUnit) && MINUTE.equals(endTimeUnit) ) {
startTimeStr = convertAnyTime(startTimeStr,MINUTE ,toTimeUnit);
endTimeStr = convertAnyTime(endTimeStr,MINUTE ,toTimeUnit);
}
if(isLeft == null) {
resultBuffer.append(startTimeStr);
resultBuffer.append("-");
resultBuffer.append(endTimeStr);
}else {
if(isLeft) {
resultBuffer.append(startTimeStr);
}else {
resultBuffer.append(endTimeStr);
}
}
}else {
for(int i = 0; i < gtMatchExpressionStr.length(); i++) {
char matchSimple = gtMatchExpressionStr.charAt(i);
if(reactionTimeStr.indexOf(matchSimple) > -1) {
String currentSimple = String.valueOf(matchSimple);
if(isLeft == null) {
resultBuffer.append(currentSimple);
}
reactionTimeStr = reactionTimeStr.replaceAll(currentSimple, "");
gtTimeUnit = getTimeUint(reactionTimeStr);
break;
}
}
for(int i = 0; i < ltMatchExpressionStr.length(); i++) {
char matchSimple = ltMatchExpressionStr.charAt(i);
if(reactionTimeStr.indexOf(matchSimple) > -1) {
String currentSimple = String.valueOf(matchSimple);
if(isLeft == null) {
resultBuffer.append(currentSimple);
}
reactionTimeStr = reactionTimeStr.replaceAll(currentSimple, "");
ltTimeUnit = getTimeUint(reactionTimeStr);
break;
}
}
if(HOUR.equals(gtTimeUnit) || "".equals(gtTimeUnit) ) {
resultBuffer.append(convertAnyTime(reactionTimeStr,HOUR ,toTimeUnit));
}
if(MINUTE.equals(gtTimeUnit)) {
resultBuffer.append(convertAnyTime(reactionTimeStr,MINUTE ,toTimeUnit));
}
if(SECOND.equals(gtTimeUnit)) {
resultBuffer.append(convertAnyTime(reactionTimeStr,SECOND ,toTimeUnit));
}
if(HOUR.equals(ltTimeUnit)) {
resultBuffer.append(convertAnyTime(reactionTimeStr,HOUR ,toTimeUnit));
}
if(MINUTE.equals(ltTimeUnit)) {
resultBuffer.append(convertAnyTime(reactionTimeStr,MINUTE ,toTimeUnit));
}
if(SECOND.equals(ltTimeUnit)) {
resultBuffer.append(convertAnyTime(reactionTimeStr,SECOND ,toTimeUnit));
}
}
return resultBuffer.toString();
}
public static String getTimeUint(String timeStr) {
String currentTimeUnit = "";
HashMap<String,String> timeUnitHashMap = new HashMap<>();
timeUnitHashMap.put("h", HOUR);
timeUnitHashMap.put("hour", HOUR);
timeUnitHashMap.put("hours", HOUR);
timeUnitHashMap.put("H", HOUR);
timeUnitHashMap.put("m", MINUTE);
timeUnitHashMap.put("min", MINUTE);
timeUnitHashMap.put("minute", MINUTE);
timeUnitHashMap.put("minutes", MINUTE);
timeUnitHashMap.put("M", MINUTE);
timeUnitHashMap.put("s", SECOND);
timeUnitHashMap.put("second", SECOND);
timeUnitHashMap.put("seconds", SECOND);
timeUnitHashMap.put("S", SECOND);
for(String timeUnit:timeUnitHashMap.keySet()) {
if(timeStr.contains(timeUnit)) {
currentTimeUnit = timeUnitHashMap.get(timeUnit);
break;
}
}
return currentTimeUnit;
}
public static String convertAnyTime(String reactionTime,String currentUnit,String convertUnit) {
String result = "";
if(HOUR.equals(currentUnit)) {
reactionTime = reactionTime.replaceAll("hours", "").replaceAll("hour", "").replaceAll("h", "").replaceAll("H", "");
Long reactionTimeLongValue = Long.parseLong(reactionTime);
if(HOUR.equals(convertUnit)) {
result = String.valueOf(reactionTimeLongValue);
}
if(MINUTE.equals(convertUnit)) {
result = String.valueOf(TimeUnit.HOURS.toMinutes(reactionTimeLongValue));
}
if(SECOND.equals(convertUnit)) {
result = String.valueOf(TimeUnit.HOURS.toSeconds(reactionTimeLongValue));
}
}
if(MINUTE.equals(currentUnit)) {
reactionTime = reactionTime.replaceAll("minute", "").replaceAll("min", "").replaceAll("m", "");
Long reactionTimeLongValue = Long.parseLong(reactionTime);
if(HOUR.equals(convertUnit)) {
result = decimalFormat.format(reactionTimeLongValue/60d);
}
if(MINUTE.equals(convertUnit)) {
result = String.valueOf(reactionTimeLongValue);
}
if(SECOND.equals(convertUnit)) {
result = String.valueOf(TimeUnit.MINUTES.toSeconds(reactionTimeLongValue));
}
}
if(SECOND.equals(currentUnit)) {
reactionTime = reactionTime.replaceAll("second", "").replaceAll("s", "").replaceAll("S", "");
Long reactionTimeLongValue = Long.parseLong(reactionTime);
if(HOUR.equals(convertUnit)) {
result = decimalFormat.format(reactionTimeLongValue/3600d);
}
if(MINUTE.equals(convertUnit)) {
result = decimalFormat.format(reactionTimeLongValue/60d);
}
if(SECOND.equals(convertUnit)) {
result = String.valueOf(reactionTimeLongValue);
}
}
return result;
}
public static void main(String[] args) {
List<String> checkList = new ArrayList<>();
checkList.add("1 - 1");
checkList.add(" 1 -2 h ");
checkList.add("1-2H");
checkList.add("1-2hour");
checkList.add("5min-2h");
checkList.add("5s-2h");
checkList.add("5min-2min");
checkList.add("5m-2min");
checkList.add(">10min");
checkList.add("≥26");
for(String timePhase:checkList) {
System.out.println(ConvertReactionTimePhaseValue(timePhase,ReactionTimeUtil.HOUR,false));
}
}
}