001 /* 002 // $Id: //open/mondrian/src/main/mondrian/olap/fun/ParenthesesFunDef.java#12 $ 003 // This software is subject to the terms of the Common Public License 004 // Agreement, available at the following URL: 005 // http://www.opensource.org/licenses/cpl.html. 006 // Copyright (C) 2002-2002 Kana Software, Inc. 007 // Copyright (C) 2002-2006 Julian Hyde and others 008 // All Rights Reserved. 009 // You must accept the terms of that agreement to use this software. 010 // 011 // jhyde, 3 March, 2002 012 */ 013 package mondrian.olap.fun; 014 import mondrian.olap.*; 015 import mondrian.olap.type.Type; 016 import mondrian.calc.Calc; 017 import mondrian.calc.ExpCompiler; 018 import mondrian.mdx.ResolvedFunCall; 019 020 import java.io.PrintWriter; 021 022 /** 023 * <code>ParenthesesFunDef</code> implements the parentheses operator as if it 024 * were a function. 025 * 026 * @author jhyde 027 * @since 3 March, 2002 028 * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/ParenthesesFunDef.java#12 $ 029 */ 030 public class ParenthesesFunDef extends FunDefBase { 031 private final int argType; 032 public ParenthesesFunDef(int argType) { 033 super( 034 "()", 035 "(<Expression>)", 036 "Parenthesis enclose an expression and indicate precedence.", 037 Syntax.Parentheses, 038 argType, 039 new int[] {argType}); 040 this.argType = argType; 041 } 042 public void unparse(Exp[] args, PrintWriter pw) { 043 if (args.length != 1) { 044 ExpBase.unparseList(pw, args, "(", ",", ")"); 045 } else { 046 // Don't use parentheses unless necessary. We add parentheses around 047 // expressions because we're not sure of operator precedence, so if 048 // we're not careful, the parentheses tend to multiply ad infinitum. 049 args[0].unparse(pw); 050 } 051 } 052 053 public Type getResultType(Validator validator, Exp[] args) { 054 Util.assertTrue(args.length == 1); 055 return args[0].getType(); 056 } 057 058 public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) { 059 return compiler.compile(call.getArg(0)); 060 } 061 } 062 063 // End ParenthesesFunDef.java