001/*
002 * (C) Copyright 2006-2016 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 */
019package org.nuxeo.ecm.core.query.sql.model;
020
021import com.google.common.collect.Iterables;
022
023/**
024 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
025 */
026public class FromClause extends Clause {
027
028    private static final long serialVersionUID = 3563271484181691679L;
029
030    public static final int DOCTYPE = 0;
031
032    public static final int LOCATION = 1;
033
034    public final FromList elements;
035
036    public final int type;
037
038    public FromClause() {
039        this(new FromList());
040    }
041
042    public FromClause(FromList elements) {
043        this(DOCTYPE, elements);
044    }
045
046    public FromClause(int type, FromList elements) {
047        super("FROM");
048        this.elements = elements;
049        this.type = type;
050    }
051
052    public void add(String alias, String element) {
053        elements.put(alias, element);
054    }
055
056    public void add(String element) {
057        elements.put(element, element);
058    }
059
060    public String get(int i) {
061        return Iterables.get(elements.values(), i);
062    }
063
064    public String get(String alias) {
065        return elements.get(alias);
066    }
067
068    public String getAlias(int i) {
069        return Iterables.get(elements.keySet(), i);
070    }
071
072    public int count() {
073        return elements.size();
074    }
075
076    public int getType() {
077        return type;
078    }
079
080    @Override
081    public void accept(IVisitor visitor) {
082        visitor.visitFromClause(this);
083    }
084
085    @Override
086    public boolean equals(Object obj) {
087        if (this == obj) {
088            return true;
089        }
090        if (obj instanceof FromClause) {
091            FromClause sc = (FromClause) obj;
092            return elements.equals(sc.elements);
093        }
094        return false;
095    }
096
097    @Override
098    public int hashCode() {
099        return elements.hashCode();
100    }
101
102    @Override
103    public String toString() {
104        return elements.toString();
105    }
106
107}