/**
* Copyright 1999-2009 The Pegadi Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pegadi.sqlsearch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* This is an abstract class representing a SearchTerm that is composed of two
* or more SearchTerms.
*
* @author Eirik Bjorsnos <bjorsnos@underdusken.no>
*/
public abstract class CompoundTerm extends SearchTerm
implements java.io.Serializable {
/**
* A vector containing the terms currently added
*/
private List<SearchTerm> terms;
/**
* Default constructor.
*/
public CompoundTerm() {
terms = new ArrayList<SearchTerm>();
}
/**
* Constructor that takes one SearchTerm. I know that a compound term
* is not very compund if it has only one term, but you can add terms with
* the addTerm() methor.
*
* @param term the SearchTerm to start with
*/
public CompoundTerm(SearchTerm term) {
this();
terms.add(term);
}
/**
* Constructor that takes an array of SearchTerms.
*
* @param terms an array of SearchTerms
*/
public CompoundTerm(SearchTerm... terms) {
this();
Collections.addAll(this.terms, terms);
}
public CompoundTerm(List<SearchTerm> terms) {
this.terms = terms;
}
/**
* Adds a SearchTerm to this compundterm.
*
* @param term the term to add
*/
public void addTerm(SearchTerm term) {
if (term != null) terms.add(term);
}
/**
* Returns this SearchTerm's sub-SearchTerms.
*
* @return the terms that the compund term consists off
*/
public List<SearchTerm> getTerms() {
return terms;
}
/**
* Returns a List<String> of tables that this SearchTerm and it's subterms
* involves. It is used to construct the FROM part of the SQL query.
*
* @return the table names used
*/
public List<String> getTables() {
List<String> tables = new ArrayList<String>();
for (SearchTerm term : terms) {
tables.addAll(term.getTables());
}
return tables;
}
/**
* Returns a List<String> of tables that subterms tell us to ignore
*
* @return the table names to be ignored
*/
public List<String> getIgnoreTables() {
List<String> tables = new ArrayList<String>();
for (SearchTerm term : terms) {
tables.addAll(term.getIgnoreTables());
}
return tables;
}
}