最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA區(qū)間值判斷[10,20)的實(shí)現(xiàn)

 更新時(shí)間:2023年09月01日 10:34:02   作者:時(shí)代在找碼  
本文主要介紹了JAVA區(qū)間值判斷[10,20)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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)換比較符號(hào)≤轉(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;
    }
    /**
     * 將整個(gè)閥值區(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;
    }
    /**
     * 將半個(gè)閥值區(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("≥", ">=");
    }
    /**
     * 去除百分號(hào),轉(zhuǎn)為小數(shù)
     * @param str 可能含百分號(hào)的數(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;
    //"范圍符號(hào):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è)括號(hào)狀態(tài):false -開區(qū)間  true-- 閉區(qū)間
    private boolean left_sate = false;
    //右側(cè)括號(hào)狀態(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ū)間集合類(不等于時(shí)轉(zhuǎn)換后為2個(gè)區(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;
        //重合條件,第一個(gè)區(qū)間最大值大于第二個(gè)區(qū)間最小值并且第一個(gè)區(qū)間的最小值小于第二個(gè)區(qū)間的最大值
        //注意傳值順序,第一個(gè)值為第一個(gè)區(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;
        }
        //注意傳值順序,第一個(gè)值為第二個(gè)區(qū)間的最大值(此處不能反)
        int second_result = compareTo(max_entity, this.min_entity);
        //此處本應(yīng)該是second_result<0,但由于上一步參數(shù)傳遞時(shí)時(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 將Java程序打包成EXE文件的實(shí)現(xiàn)方式

    將Java程序打包成EXE文件的實(shí)現(xiàn)方式

    這篇文章主要介紹了將Java程序打包成EXE文件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Springboot整合Shiro之加鹽MD5加密的方法

    Springboot整合Shiro之加鹽MD5加密的方法

    這篇文章主要介紹了Springboot整合Shiro之加鹽MD5加密的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-12-12
  • Java中Date日期類的使用方法示例詳解

    Java中Date日期類的使用方法示例詳解

    這篇文章主要介紹了Java中Date日期類的使用方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • 一文帶你深入了解Java的自動(dòng)拆裝箱

    一文帶你深入了解Java的自動(dòng)拆裝箱

    Java推出了對(duì)于基本數(shù)據(jù)類型的對(duì)應(yīng)的對(duì)象,將基本數(shù)據(jù)類型轉(zhuǎn)換為對(duì)象就稱為裝箱,反之則是拆箱,本文主要為大家介紹了Java自動(dòng)拆裝箱的原理與應(yīng)用,需要的可以參考下
    2023-11-11
  • java算法實(shí)現(xiàn)預(yù)測(cè)雙色球中獎(jiǎng)號(hào)碼

    java算法實(shí)現(xiàn)預(yù)測(cè)雙色球中獎(jiǎng)號(hào)碼

    這篇文章主要介紹了java算法實(shí)現(xiàn)預(yù)測(cè)雙色球中獎(jiǎng)號(hào)碼的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 使用spring的restTemplate注意點(diǎn)

    使用spring的restTemplate注意點(diǎn)

    這篇文章主要介紹了使用spring的restTemplate注意點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringMVC集成FastJson使用流程詳解

    SpringMVC集成FastJson使用流程詳解

    如果你使用 Spring MVC 來構(gòu)建 Web 應(yīng)用并對(duì)性能有較高的要求的話,可以使用 Fastjson 提供的FastJsonHttpMessageConverter 來替換 Spring MVC 默認(rèn)的 HttpMessageConverter 以提高 @RestController @ResponseBody @RequestBody 注解的 JSON序列化速度
    2023-02-02
  • 解析Java設(shè)計(jì)模式編程中命令模式的使用

    解析Java設(shè)計(jì)模式編程中命令模式的使用

    這篇文章主要介紹了Java設(shè)計(jì)模式編程中命令模式的使用,在一些處理請(qǐng)求響應(yīng)的場(chǎng)合經(jīng)??梢杂玫矫钅J降木幊趟悸?需要的朋友可以參考下
    2016-02-02
  • 理解java設(shè)計(jì)模式之建造者模式

    理解java設(shè)計(jì)模式之建造者模式

    這篇文章主要幫助大家理解java設(shè)計(jì)模式之建造者模式,對(duì)建造者模式,即生成器模式進(jìn)行實(shí)例講解,感興趣的朋友可以參考一下
    2016-02-02
  • Maven Assembly實(shí)戰(zhàn)教程

    Maven Assembly實(shí)戰(zhàn)教程

    MavenAssembly插件用于創(chuàng)建可分發(fā)包,如JAR、ZIP或TAR文件,通過配置pom.xml,可以生成包含所有依賴的JAR文件或自定義格式的歸檔文件,示例展示了如何使用默認(rèn)描述符和自定義描述符創(chuàng)建JAR包,以及在多模塊項(xiàng)目中使用Assembly插件
    2024-12-12

最新評(píng)論

汪清县| 乌拉特后旗| 周口市| 苍溪县| 昂仁县| 高台县| 绥德县| 闻喜县| 蒲江县| 建平县| 鄂托克旗| 彭水| 南漳县| 扶风县| 文水县| 布拖县| 金川县| 措勤县| 荆州市| 梧州市| 白玉县| 祁阳县| 新乡市| 贵溪市| 江源县| 玉龙| 屯昌县| 铜鼓县| 奉贤区| 库尔勒市| 靖远县| 罗江县| 德昌县| 盐津县| 滁州市| 澜沧| 兴海县| 会昌县| 武功县| 平南县| 樟树市|