JAVA區(qū)間值判斷[10,20)的實(shí)現(xiàn)
IntervalUtil
package com.utils;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class IntervalUtil {
public static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
/**
* @Description: 比較dataValue與interval結(jié)果
* @Param: [dataValue:比較值, interval:如 ≤0、≥0 、>0 、<0 、==0]
* @return: boolean
*/
public static boolean compareNumValue(String dataValue,String interval) {
//轉(zhuǎn)換比較符號≤轉(zhuǎn)換為<=,≥轉(zhuǎn)換為>=
String comparator = interval.replace("≤", "<=").replace("≥", ">=");
//拼接表達(dá)式
StringBuffer formula = new StringBuffer();
formula.append("(");
formula.append(dataValue);
formula.append(comparator);
formula.append(")");
try {
//計(jì)算表達(dá)式
return (Boolean) jse.eval(formula.toString());
} catch (Exception t) {
return false;
}
}
/**
* 判斷data_value是否在interval區(qū)間范圍內(nèi)
* @param data_value 數(shù)值類型的
* @param interval 正常的數(shù)學(xué)區(qū)間,包括無窮大等,如:(1,3)、>5%、(-∞,6]、(125%,135%)U(70%,80%)
* @return true:表示data_value在區(qū)間interval范圍內(nèi),false:表示data_value不在區(qū)間interval范圍內(nèi)
*/
public static boolean isInTheInterval(String data_value,String interval) {
//將區(qū)間和data_value轉(zhuǎn)化為可計(jì)算的表達(dá)式
String formula = getFormulaByAllInterval(data_value,interval,"||");
ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
try {
//計(jì)算表達(dá)式
return (Boolean) jse.eval(formula);
} catch (Exception t) {
return false;
}
}
public static String eval(String el){
try {
return new DecimalFormat("0.00").format(jse.eval(el));
} catch (ScriptException e) {
e.printStackTrace();
return "0.00";
}
}
/**
* 將所有閥值區(qū)間轉(zhuǎn)化為公式:如
* [75,80) =》 date_value < 80 && date_value >= 75
* (125%,135%)U(70%,80%) =》 (date_value < 1.35 && date_value > 1.25) || (date_value < 0.8 && date_value > 0.7)
* @param date_value
* @param interval 形式如:(125%,135%)U(70%,80%)
* @param connector 連接符 如:") || ("
*/
private static String getFormulaByAllInterval(String date_value, String interval, String connector) {
StringBuffer buff = new StringBuffer();
for(String limit:interval.split("U")){//如:(125%,135%)U (70%,80%)
buff.append("(").append(getFormulaByInterval(date_value, limit," && ")).append(")").append(connector);
}
String allLimitInvel = buff.toString();
int index = allLimitInvel.lastIndexOf(connector);
allLimitInvel = allLimitInvel.substring(0,index);
return allLimitInvel;
}
/**
* 將整個閥值區(qū)間轉(zhuǎn)化為公式:如
* 145) =》 date_value < 145
* [75,80) =》 date_value < 80 && date_value >= 75
* @param date_value
* @param interval 形式如:145)、[75,80)
* @param connector 連接符 如:&&
*/
private static String getFormulaByInterval(String date_value, String interval, String connector) {
StringBuffer buff = new StringBuffer();
for(String halfInterval:interval.split(",")){//如:[75,80)、≥80
buff.append(getFormulaByHalfInterval(halfInterval, date_value)).append(connector);
}
String limitInvel = buff.toString();
int index = limitInvel.lastIndexOf(connector);
limitInvel = limitInvel.substring(0,index);
return limitInvel;
}
/**
* 將半個閥值區(qū)間轉(zhuǎn)化為公式:如
* 145) =》 date_value < 145
* ≥80% =》 date_value >= 0.8
* [130 =》 date_value >= 130
* <80% =》 date_value < 0.8
* @param halfInterval 形式如:145)、≥80%、[130、<80%
* @param date_value
* @return date_value < 145
*/
private static String getFormulaByHalfInterval(String halfInterval, String date_value) {
halfInterval = halfInterval.trim();
if(halfInterval.contains("∞")){//包含無窮大則不需要公式
return "1 == 1";
}
StringBuffer formula = new StringBuffer();
String data = "";
String opera = "";
if(halfInterval.matches("^([<>≤≥\\[\\(]{1}(-?\\d+.?\\d*\\%?))$")){//表示判斷方向(如>)在前面 如:≥80%
opera = halfInterval.substring(0,1);
data = halfInterval.substring(1);
}else{//[130、145)
opera = halfInterval.substring(halfInterval.length()-1);
data = halfInterval.substring(0,halfInterval.length()-1);
}
double value = dealPercent(data);
formula.append(date_value).append(" ").append(opera).append(" ").append(value);
String a = formula.toString();
//轉(zhuǎn)化特定字符
return a.replace("[", ">=").replace("(", ">").replace("]", "<=").replace(")", "<").replace("≤", "<=").replace("≥", ">=");
}
/**
* 去除百分號,轉(zhuǎn)為小數(shù)
* @param str 可能含百分號的數(shù)字
* @return
*/
private static double dealPercent(String str){
double d = 0.0;
if(str.contains("%")){
str = str.substring(0,str.length()-1);
d = Double.parseDouble(str)/100;
}else{
d = Double.parseDouble(str);
}
return d;
}
public static boolean isOverlap(List<String> laplist){
List<ReqRespResearchProductQuestionnaireItem> list = new ArrayList<>();
for (String lap:laplist
) {
list.add(convertlaps(lap));
}
return SectionUtil.compareSection(list);
}
public static ReqRespResearchProductQuestionnaireItem convertlaps(String lap){
// 7-全閉區(qū)間/8-左閉右開區(qū)間/9-左開右閉區(qū)間/10-全開區(qū)間
String min = "";
String max = "";
String el = "";
el = String.valueOf(lap.charAt(0))+ String.valueOf(lap.charAt(lap.length()-1));
Byte type = new Byte("0");
if(el.equals("[]")){
type = new Byte("7");
}
else if(el.equals("[)")){
type = new Byte("8");
}
else if(el.equals("(]")){
type = new Byte("9");
}
else if(el.equals("()")){
type = new Byte("10");
}else{
throw new RuntimeException("區(qū)間表達(dá)式格式異常------->"+ lap);
}
String[] s1s = lap.split(",");
min = s1s[0].substring(1);
max = s1s[1].substring(0,s1s[1].length()-1);
System.out.println(min+"---"+max+"---"+el+"-- 7-全閉區(qū)間/8-左閉右開區(qū)間/9-左開右閉區(qū)間/10-全開區(qū)間 --"+type);
return new ReqRespResearchProductQuestionnaireItem(min,max,type);
}
public static void main(String[] args) {
IntervalUtil a = new IntervalUtil();
//無窮小
//System.out.println(a.isInTheInterval("5", "(-∞,6]"));
//判斷區(qū)間范圍
System.out.println(a.isInTheInterval("3.1", "(1,3]"));
//比較dataValue與interval結(jié)果
String eval = "≥0";
System.out.println(a.compareNumValue("5", eval));
eval = "(2*2*2*2)/3-2";
System.out.println(eval(eval));
String s1 = "(0.23,90.88]";
convertlaps(s1);
String el = "(2>1)&&(3<2)";
String el1 = "(#score>1)||(#check_status<2)";
String el2 = "(1,2) U (2,5)U (3,10)";
try {
System.out.println("el-------------->"+ jse.eval(el));
System.out.println("el-------------->"+ ElUtil.pick(el1));
System.out.println(a.isInTheInterval("13.1", el2));
} catch (ScriptException e) {
throw new RuntimeException(e);
}
}
}ReqRespResearchProductQuestionnaireItem
package com.utils;
import java.io.Serializable;
public class ReqRespResearchProductQuestionnaireItem implements Serializable {
private String minValue;
private String maxValue;
//"范圍符號:1-大于/2-小于/3-等于/4-不等于/5-大于等于/6-小于等于/7-全閉區(qū)間/8-左閉右開區(qū)間/9-左開右閉區(qū)間/10-全開區(qū)間"
private Byte symbol;
public ReqRespResearchProductQuestionnaireItem( String minValue, String maxValue, Byte symbol) {
this.minValue = minValue;
this.maxValue = maxValue;
this.symbol = symbol;
}
public String getMinValue() {
return minValue;
}
public void setMinValue(String minValue) {
this.minValue = minValue;
}
public String getMaxValue() {
return maxValue;
}
public void setMaxValue(String maxValue) {
this.maxValue = maxValue;
}
public Byte getSymbol() {
return symbol;
}
public void setSymbol(Byte symbol) {
this.symbol = symbol;
}
}SectionUtil
package com.utils;
import com.alibaba.fastjson.JSONObject;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class SectionUtil {
//最小值
private String min_entity;
//最大值
private String max_entity;
//左側(cè)括號狀態(tài):false -開區(qū)間 true-- 閉區(qū)間
private boolean left_sate = false;
//右側(cè)括號狀態(tài):false -開區(qū)間 true-- 閉區(qū)間
private boolean right_sate = false;
private SectionUtil() {
}
public SectionUtil(String min_entity, String max_entity, boolean left_sate, boolean right_sate) {
this.min_entity = min_entity;
this.max_entity = max_entity;
this.left_sate = left_sate;
this.right_sate = right_sate;
}
public String getMin_entity() {
return min_entity;
}
public String getMax_entity() {
return max_entity;
}
public boolean isLeft_sate() {
return left_sate;
}
public boolean isRight_sate() {
return right_sate;
}
/**
* @description: 創(chuàng)建負(fù)區(qū)間((負(fù)無窮,X])
* @param value 區(qū)間最大值
* @param right_sate 區(qū)間開閉狀態(tài)
*
*/
public static SectionUtil creatFu(String value, boolean right_sate) {
return new SectionUtil("", value, false, right_sate);
}
/**
* @description: 創(chuàng)建正區(qū)間[X,正無窮))
* @param value 區(qū)間最小值
* @param left_sate 區(qū)間開閉狀態(tài)
*
*/
public static SectionUtil creatZheng(String value, boolean left_sate) {
return new SectionUtil(value, "", left_sate, false);
}
/**
* @description: 創(chuàng)建閉合區(qū)間([X,Y])
* @param min 區(qū)間最小值
* @param max 區(qū)間最大值
* @param left_sate 區(qū)間左側(cè)開閉狀態(tài)
* @param right_sate 區(qū)間右側(cè)開閉狀態(tài)
* @return
*
*/
public static SectionUtil creat(String min, boolean left_sate, String max, boolean right_sate) {
return new SectionUtil(min, max, left_sate, right_sate);
}
/**
* @description: 將實(shí)體類轉(zhuǎn)換成區(qū)間集合
* @param record 待轉(zhuǎn)換的實(shí)體類
* @return 轉(zhuǎn)換后的區(qū)間集合類(不等于時轉(zhuǎn)換后為2個區(qū)間,所以采用集合)
*
*/
public static List<SectionUtil> getSections(ReqRespResearchProductQuestionnaireItem record) {
List<SectionUtil> list = new ArrayList<>();
String record_max = record.getMaxValue();
String record_min = record.getMinValue();
switch (record.getSymbol()) {
case 1:
list.add(creatZheng(record_max, false));
break;
case 2:
list.add(creatFu(record_max, false));
break;
case 3:
list.add(creat(record_max, true, record_max, true));
break;
case 4:
list.add(creatFu(record_max, false));
list.add(creatZheng(record_max, false));
break;
case 5:
list.add(creatZheng(record_max, true));
break;
case 6:
list.add(creatFu(record_max, true));
break;
case 7:
list.add(creat(record_min, true, record_max, true));
break;
case 8:
list.add(creat(record_min, true, record_max, false));
break;
case 9:
list.add(creat(record_min, false, record_max, true));
break;
case 10:
list.add(creat(record_min, false, record_max, false));
break;
}
return list;
}
public int compareTo(String first_value, String second_value) {
//first_value為空表示為正無窮,second_value為空表示為負(fù)無窮
if (isBlank(first_value) || isBlank(second_value)) {
return 1;
}
return compareToValue(first_value,second_value);
}
//判斷字符串是否為空
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
/**
* @param record 判斷區(qū)間是否有重合
* @return true-有重合 false -無重合
* @description: 判斷當(dāng)前區(qū)間是否和指定區(qū)間重合
*
*/
public boolean isChonghe(SectionUtil record) {
String min_entity = record.getMin_entity();
String max_entity = record.getMax_entity();
boolean left_sate = record.isLeft_sate();
boolean right_sate = record.isRight_sate();
boolean left_isok = false;
boolean right_isok = false;
//重合條件,第一個區(qū)間最大值大于第二個區(qū)間最小值并且第一個區(qū)間的最小值小于第二個區(qū)間的最大值
//注意傳值順序,第一個值為第一個區(qū)間的最大值(此處不能反)
int first_result = compareTo(this.max_entity, min_entity);
if ((first_result == 0 && this.right_sate && left_sate) || (first_result > 0)) {
left_isok = true;
}
//注意傳值順序,第一個值為第二個區(qū)間的最大值(此處不能反)
int second_result = compareTo(max_entity, this.min_entity);
//此處本應(yīng)該是second_result<0,但由于上一步參數(shù)傳遞時時反正傳遞,故此此處為second_result>0
if ((second_result == 0 && this.left_sate && right_sate) || second_result > 0) {
right_isok = true;
}
return left_isok && right_isok;
}
/**
* @description: 比較集合中區(qū)間是否有重疊
* @param list1 待比較集合1
* @param list2 待比較集合2
* @return
*
*/
public static boolean isChonghe(List<SectionUtil> list1, List<SectionUtil> list2) {
boolean chonghed = false;
for (SectionUtil item1 : list1) {
for (SectionUtil item2 : list2) {
chonghed = item1.isChonghe(item2);
if (chonghed) {
return true;
}
}
}
return chonghed;
}
//比較大小
public static int compareToValue(String value1, String value2) {
BigDecimal b1 = new BigDecimal(value1);
BigDecimal b2 = new BigDecimal(value2);
return b1.compareTo(b2);
}
/**
* @description: 判斷集合中區(qū)間是否重疊
* @param list 待判斷集合
* @return fasle-無重疊 true-有重疊
*
*/
public static boolean compareSection(List<ReqRespResearchProductQuestionnaireItem> list) {
for (int i = 0; i < list.size(); i++) {
ReqRespResearchProductQuestionnaireItem record = list.get(i);
for (int j = i + 1; j < list.size(); j++) {
ReqRespResearchProductQuestionnaireItem item = list.get(j);
//判斷區(qū)間是否有交叉
List<SectionUtil> records = SectionUtil.getSections(record);
List<SectionUtil> items = SectionUtil.getSections(item);
boolean chonghe = SectionUtil.isChonghe(records, items);
if (chonghe) {
System.out.println(JSONObject.toJSONString(records));
System.out.println(JSONObject.toJSONString(items));
return true;
}
}
}
return false;
}
public static void main(String[] args) {
List<ReqRespResearchProductQuestionnaireItem> list = new ArrayList<>();
list.add(new ReqRespResearchProductQuestionnaireItem("1","5",new Byte("10")));
list.add(new ReqRespResearchProductQuestionnaireItem("5","10",new Byte("8")));
// list.add(new ReqRespResearchProductQuestionnaireItem("10","20",new Byte("9")));
// list.add(new ReqRespResearchProductQuestionnaireItem("20","2",new Byte("10")));
System.out.println(compareSection(list));
}
}調(diào)用區(qū)間判斷
List<String> laps = new ArrayList<>();
laps.add("[1,5]");
laps.add("[2,4]");
laps.add("[11,15]");
if(IntervalUtil.isOverlap(laps)){
throw new Exception("區(qū)間存在沖突") ;
}到此這篇關(guān)于JAVA區(qū)間值判斷[10,20)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)JAVA區(qū)間值判斷[10,20)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java工具類之實(shí)現(xiàn)java獲取文件行數(shù)
這篇文章主要介紹了一個java工具類,可以取得當(dāng)前項(xiàng)目中所有java文件總行數(shù),代碼行數(shù),注釋行數(shù),空白行數(shù),需要的朋友可以參考下2014-03-03
Java并發(fā)之原子性 有序性 可見性及Happen Before原則
一提到happens-before原則,就讓人有點(diǎn)“丈二和尚摸不著頭腦”。這個涵蓋了整個JMM中可見性原則的規(guī)則,究竟如何理解,把我個人一些理解記錄下來。下面可以和小編一起學(xué)習(xí)Java 并發(fā)四個原則2021-09-09
java 在圖片上寫字,兩個圖片合并的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava 在圖片上寫字,兩個圖片合并的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
Java中static和static?final的區(qū)別詳解
這篇文章主要介紹了Java中static和static?final的區(qū)別詳解,開發(fā)時我們經(jīng)常用到static以及static?final來修飾我們的字段變量,那么他們到底有什么區(qū)別呢?其實(shí)他們的區(qū)別可以用使用字節(jié)碼文件來解析,需要的朋友可以參考下2023-10-10
SpringBoot 文件或圖片上傳與下載功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot 文件或圖片上傳與下載功能的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Java中Lambda表達(dá)式和函數(shù)式接口的使用和特性
Java Lambda表達(dá)式是一種函數(shù)式編程的特性,可簡化匿名內(nèi)部類的寫法,與函數(shù)式接口搭配使用,實(shí)現(xiàn)代碼簡潔、可讀性高、易于維護(hù)的特點(diǎn),適用于集合操作、多線程編程等場景2023-04-04
Java基于線程實(shí)現(xiàn)帶有滾動效果的Label標(biāo)簽實(shí)例
這篇文章主要介紹了Java基于線程實(shí)現(xiàn)帶有滾動效果的Label標(biāo)簽,實(shí)例分析了java線程的使用技巧及l(fā)abel標(biāo)簽的實(shí)現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07

