001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.query.sql.model;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027public class Function implements Operand {
028
029    private static final long serialVersionUID = -6107133982072616209L;
030
031    public final String name;
032
033    public final OperandList args;
034
035    public Function(String name) {
036        this(name, null);
037    }
038
039    public Function(String name, OperandList args) {
040        this.name = name;
041        this.args = args;
042    }
043
044    @Override
045    public String toString() {
046        return args == null ? name + "()" : name + '(' + args + ')';
047    }
048
049    @Override
050    public boolean equals(Object obj) {
051        if (this == obj) {
052            return true;
053        }
054        if (obj instanceof Function) {
055            Function func = (Function) obj;
056            if (args == null) {
057                return func.args == null;
058            }
059            return name.equals(func.name) && args.equals(func.args);
060        }
061        return false;
062    }
063
064    @Override
065    public int hashCode() {
066        int result = name.hashCode();
067        result = 31 * result + (args != null ? args.hashCode() : 0);
068        return result;
069    }
070
071    @Override
072    public void accept(IVisitor visitor) {
073        visitor.visitFunction(this);
074    }
075
076}