All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rbmhtechnology.vind.parser.queryparser.MultiBooleanClause Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.rbmhtechnology.vind.parser.queryparser;

import com.rbmhtechnology.vind.SearchServerException;
import com.rbmhtechnology.vind.api.query.filter.Filter;
import com.rbmhtechnology.vind.model.DocumentFactory;

import java.util.ArrayList;
import java.util.List;

public class MultiBooleanClause extends BooleanClause{
    private final List ops = new ArrayList<>();
    private final List clauses = new ArrayList<>();

    public MultiBooleanClause(String op, Clause leftClause, Clause rightClause){
        this.ops.add(op) ;
        this.clauses.add(leftClause);
        this.clauses.add(rightClause);
    }

    public void addClause(String op, Clause rightClause) {
        this.ops.add(op) ;
        this.clauses.add(rightClause);
    }

    public List getOps() {
        return ops;
    }

    public List getClauses() {
        return clauses;
    }


    @Override
    public Filter toVindFilter(DocumentFactory factory) {
        final List sortedFilters = new ArrayList<>();
        for (int i = 0; i < ops.size(); i++) {
            final String op = ops.get(i);
            if (op.equalsIgnoreCase("AND")) {
                if (i == 0) {
                    sortedFilters.add(new Filter.AndFilter(clauses.get(i).toVindFilter(factory), clauses.get(i + 1).toVindFilter(factory)));
                } else {
                    final Filter lasClause = sortedFilters.toArray(new Filter[sortedFilters.size()])[sortedFilters.size()-1];
                    sortedFilters.remove(lasClause);
                    sortedFilters.add(new Filter.AndFilter(lasClause, clauses.get(i + 1).toVindFilter(factory)));
                }
            } else if (op.equalsIgnoreCase("OR")) {
                if (i == 0) {
                    sortedFilters.add(clauses.get(i).toVindFilter(factory));
                }
                sortedFilters.add(clauses.get(i + 1).toVindFilter(factory));
            } else {
                throw new SearchServerException("Unsuported binary boolean operation '" + op + "' on fields");
            }
        }
        return sortedFilters.stream().collect(Filter.OrCollector);
    }

    @Override
    public String toString() {
        String booleanExp = "(";
        for (int i = 0 ; i < ops.size();i++ ) {
            if( i == 0){
                booleanExp = String.join(" ", booleanExp, clauses.get(i).toString());
            }
            booleanExp = String.join(" ", booleanExp, ops.get(i), clauses.get(i+1).toString());
        }
        return booleanExp + ")";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy