net.sf.jsqlparser.util.cnfexpression.CloneHelper Maven / Gradle / Ivy
The newest version!
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2017 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package net.sf.jsqlparser.util.cnfexpression;
import java.util.ArrayList;
import java.util.List;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NotExpression;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
/**
* This class is mainly used for handling the cloning of an expression tree.
* Note this is the shallow copy of the tree. That means I do not modify
* or copy the expression other than these expressions:
* AND, OR, NOT, (), MULTI-AND, MULTI-OR.
* Since the CNF conversion only change the condition part of the tree.
* @author messfish
*
*/
class CloneHelper {
/**
* This method is used for changing the logical structure of the tree.
* The main method is to convert and operator and or operator to let
* them have multiple children (reflected in MultipleExpression.java
* from the conditional package). Note if the value from the conditional
* operator has a not attached to it we need to append an not operator
* ahead of it since the not operator needed to be pushed down during
* the second step. Also, I will leave out all the parenthesis expression
* which is connected to the conditional operator.
* @param express the expression that will be modified
* @return the modified expression.
*
*/
public Expression modify(Expression express) {
if(express instanceof NotExpression) {
return new NotExpression(modify(((NotExpression) express).getExpression()));
}
if(express instanceof Parenthesis) {
Parenthesis parenthesis = (Parenthesis) express;
Expression result = modify(parenthesis.getExpression());
if(parenthesis.isNot()) {
return new NotExpression(result);
}
return result;
}
if(express instanceof AndExpression) {
AndExpression and = (AndExpression) express;
List list = new ArrayList();
list.add(modify(and.getLeftExpression()));
list.add(modify(and.getRightExpression()));
MultiAndExpression result = new MultiAndExpression(list);
if(and.isNot()) {
return new NotExpression(result);
}
return result;
}
if(express instanceof OrExpression) {
OrExpression or = (OrExpression) express;
List list = new ArrayList();
list.add(modify(or.getLeftExpression()));
list.add(modify(or.getRightExpression()));
MultiOrExpression result = new MultiOrExpression(list);
if(or.isNot()) {
return new NotExpression(result);
}
return result;
}
if(express instanceof BinaryExpression) {
BinaryExpression binary = (BinaryExpression) express;
if(binary.isNot()) {
binary.removeNot();
return new NotExpression(modify(binary));
}
}
/* at this stage, there is no need to modify, just simply return. */
return express;
}
/**
* This method is used to copy the expression which happens at
* step four. I only copy the conditional expressions since the
* CNF only changes the conditional part.
* @param express the expression that will be copied.
* @return the copied expression.
*/
public Expression shallowCopy(Expression express) {
if(express instanceof MultipleExpression) {
MultipleExpression multi = (MultipleExpression) express;
List list = new ArrayList();
for(int i=0; i