Home » Source Code » groovy表达式解析

groovy表达式解析

ophelia_fupx
2016-12-09 04:57:01
The author
View(s):
Download(s): 0
Point (s): 1 
Category Category:
java,shelljava,shell AllAll

Description

public class ExprSupport {
    
    private static final Object lock = new Object();
    private static final GroovyShell shell;
    private static Hashtable cache = new Hashtable();
    
    static {
        CompilerConfiguration cfg = new CompilerConfiguration();
        cfg.setScriptBaseClass(MyBasicScript.class.getName());
        shell = new GroovyShell(cfg);
    }
 
    public static Object parseExpr(String expr) {
        Script s = getScriptFromCache(expr);
        return s.run();
    }
 
    public 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.contains(expr)) {
            return cache.get(expr);
        }
        synchronized (lock) {
            if (cache.contains(expr)) {
                return cache.get(expr);
            }
            Script script = shell.parse(expr);
            cache.put(expr, script);
            return script;
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        //
        Map row = new HashMap();
        row.put("id", 42);
        row.put("name", "");
         
        //带绑定数据参数的调用方式
        System.out.println(ExprSupport.parseExpr("nvl(id,0)", row));
        System.out.println(ExprSupport.parseExpr("nvl(name,'anonymous')", row));
         
        //不带绑定数据参数的调用方式,这个是groovy的内置能力
        //+、-、<、>、<=、>=、!=、||、&&、
        System.out.println(ExprSupport.parseExpr("1>2&&5>2"));
 
    }
}
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
Sponsored links

Comments

(Add your comment, get 0.1 Point)
Minimum:15 words, Maximum:160 words
  • 1
  • Page 1
  • Total 1

groovy表达式解析 (1.31 kB)

Need 1 Point(s)
Your Point (s)

Your Point isn't enough.

Get 22 Point immediately by PayPal

Point will be added to your account automatically after the transaction.

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more Points

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D