001/*
002 * (C) Copyright 2015 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 *     Benoit Delbosc
018 */
019
020package org.nuxeo.ecm.core.query.sql.model;
021
022import org.apache.commons.lang.StringUtils;
023
024public class EsHint implements Operand {
025
026    private static final long serialVersionUID = 4590329982296853715L;
027
028    public final String index;
029
030    public final String analyzer;
031
032    public final String operator;
033
034    public EsHint(EsIdentifierList index, String analyzer, String operator) {
035        this.index = (index == null) ? null : index.toString();
036        this.analyzer = analyzer;
037        this.operator = operator;
038    }
039
040    @Override
041    public void accept(IVisitor visitor) {
042    }
043
044    @Override
045    public String toString() {
046        StringBuilder buf = new StringBuilder();
047        buf.append("/*+ES: ");
048        if (index != null) {
049            buf.append(String.format("INDEX(%s) ", index));
050        }
051        if (analyzer != null) {
052            buf.append(String.format("ANALYZER(%s) ", analyzer));
053        }
054        if (operator != null) {
055            buf.append(String.format("OPERATOR(%s) ", operator));
056        }
057        buf.append("*/");
058        return buf.toString();
059    }
060
061    @Override
062    public boolean equals(Object obj) {
063        if (obj == this) {
064            return true;
065        }
066        if (!(obj instanceof EsHint)) {
067            return false;
068        }
069        return equals((EsHint) obj);
070    }
071
072    private boolean equals(EsHint other) {
073        return StringUtils.equals(index, other.index) && StringUtils.equals(analyzer, other.analyzer)
074                && StringUtils.equals(operator, other.operator);
075    }
076
077    public String[] getIndex() {
078        if (index == null) {
079            return null;
080        }
081        return index.split(",");
082    }
083}