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