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) 2006-2009 Pentaho and others
008// All Rights Reserved.
009*/
010package mondrian.udf;
011
012import mondrian.olap.Evaluator;
013import mondrian.olap.Syntax;
014import mondrian.olap.type.*;
015import mondrian.spi.UserDefinedFunction;
016
017import java.util.regex.Pattern;
018
019/**
020 * User-defined function <code>MATCHES</code>.
021 *
022 * @author schoi
023 */
024public class MatchesUdf implements UserDefinedFunction {
025
026    public Object execute(Evaluator evaluator, Argument[] arguments) {
027        Object arg0 = arguments[0].evaluateScalar(evaluator);
028        Object arg1 = arguments[1].evaluateScalar(evaluator);
029
030        return Boolean.valueOf(Pattern.matches((String)arg1, (String)arg0));
031    }
032
033    public String getDescription() {
034        return "Returns true if the string matches the regular expression.";
035    }
036
037    public String getName() {
038        return "MATCHES";
039    }
040
041    public Type[] getParameterTypes() {
042        return new Type[] {
043            new StringType(),
044            new StringType()
045        };
046    }
047
048    public String[] getReservedWords() {
049        // This function does not require any reserved words.
050        return null;
051    }
052
053    public Type getReturnType(Type[] parameterTypes) {
054        return new BooleanType();
055    }
056
057    public Syntax getSyntax() {
058        return Syntax.Infix;
059    }
060
061}
062
063// End MatchesUdf.java