zoukankan      html  css  js  c++  java
  • Groovy自定义函数实现时间表达式解析

    class RecScript extends Script {
        @Override
        Object run() {
            return null
        }
    
        static Object addYear(String str, int num){
            def (localDate, formatter) = formatLocalTate(str)
            def months = localDate.plusYears(num)
            return formatter.format(months)
        }
        static Object addMonth(String str, int num){
            def (localDate, formatter) = formatLocalTate(str)
            def months = localDate.plusMonths(num)
            return formatter.format(months)
        }
        static Object addDay(String str, int num){
            def (localDate, formatter) = formatLocalTate(str)
            def months = localDate.plusDays(num)
            return formatter.format(months)
        }
        private static List formatLocalTate(String str) {
            def formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
            def localDate = LocalDateTime.parse(str, formatter)
            [localDate, formatter]
        }
    
    }
    

      

    class RecTimeSupport {
    
        private static final Object lock = new Object()
        private static final GroovyShell shell
        private static HashMap<String, Script> cache = new HashMap<String, Script>()
        static {
            CompilerConfiguration cfg = new CompilerConfiguration()
            cfg.setScriptBaseClass(RecScript.class.getName())
    
            shell = new GroovyShell(cfg)
        }
    
        static Object parseExpr(String expr) {
            Script s = getScriptFromCache(expr)
            return s.run()
        }
    
        static Object parseExpr(String expr, Map<?, ?> map) {
            Binding binding = new Binding(map)
            Script script = getScriptFromCache(expr)
            script.setBinding(binding)
            return script.run()
        }
        private static Script getScriptFromCache(String expr) {
            if (cache.containsKey(expr)) {
                return cache.get(expr)
            }
            synchronized (lock) {
                if (cache.containsKey(expr)) {
                    return cache.get(expr)
                }
                Script script = shell.parse(expr)
                cache.put(expr, script)
                return script
            }
        }
    
        static void main(String[] args) {
            def str = "ORDER_TIME == addMonth(ACT_TIME,2)"
            def map = [:]
            map.'ORDER_TIME' = '20190613121212'
            map.'ACT_TIME' = '20190413121212'
    
            // 绑定参数实例
            println parseExpr(str,map)
            // 未绑定参数
            println parseExpr("3+4")
    
        }
    }


    一个脚本总是编译成class。Groovy编译器将为您编译该类,并将脚本主体复制到run方法中

      

  • 相关阅读:
    快速找到由程序员到CTO发展道路上的问路石
    从大师身上反思
    你真的了解企业虚拟化吗?
    “驱网核心技术丛书”创作团队访谈
    程序员到CTO需要准备什么
    深入搜索引擎的关键——索引
    程序员到CTO必须注意的几个关键点
    微软全球MVP教你如何规划程序人生
    “碟中碟”虚拟光驱软件开发者——万春 读《寒江独钓——Windows内核安全编程 》有感
    常用jar包之commonscollection使用
  • 原文地址:https://www.cnblogs.com/jsersudo/p/12695683.html
Copyright © 2011-2022 走看看