001/*
002 * (C) Copyright 2006-2012 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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.query.sql.model;
021
022import org.apache.commons.lang.StringUtils;
023
024/**
025 * A named reference to a variable (this can be a field or table).
026 * <p>
027 * Can also include a cast.
028 */
029public class Reference implements Operand {
030
031    private static final long serialVersionUID = -1725102431543210430L;
032
033    public final String name;
034
035    public final String cast;
036
037    public final EsHint esHint;
038
039    public Reference(String name) {
040        this.name = name;
041        cast = null;
042        esHint = null;
043    }
044
045    /** @since 5.6 */
046    public Reference(String name, String cast) {
047        this.name = name;
048        this.cast = cast;
049        esHint = null;
050    }
051
052    /** @since 5.6 */
053    public Reference(Reference other, String cast) {
054        this.name = other.name;
055        this.cast = cast;
056        esHint = null;
057    }
058
059    /** @since 7.3 */
060    public Reference(Reference other, EsHint hint) {
061        this.name = other.name;
062        cast = null;
063        this.esHint = hint;
064    }
065
066    @Override
067    public void accept(IVisitor visitor) {
068        visitor.visitReference(this);
069    }
070
071    @Override
072    public String toString() {
073        if (cast != null) {
074            return cast + '(' + name + ')';
075        } else if (esHint != null) {
076            return esHint.toString() + " " + name;
077        }
078        return name;
079    }
080
081    @Override
082    public boolean equals(Object obj) {
083        if (obj == this) {
084            return true;
085        }
086        if (!(obj instanceof Reference)) {
087            return false;
088        }
089        return equals((Reference) obj);
090    }
091
092    private boolean equals(Reference other) {
093        if (!name.equals(other.name)) {
094            return false;
095        }
096        if (cast != null || other.cast != null) {
097            return StringUtils.equals(cast, other.cast);
098        }
099        if (esHint != null) {
100            return esHint.equals(other.esHint);
101        } else if (other.esHint != null) {
102            return false;
103        }
104        return true;
105    }
106
107    @Override
108    public int hashCode() {
109        int result = 31 + (cast == null ? 0 : cast.hashCode()) + (esHint == null ? 0 : esHint.hashCode());
110        return 31 * result + name.hashCode();
111    }
112
113    public boolean isPathReference() {
114        return false;
115    }
116
117}