org.eclipse.persistence.jpa.jpql.parser.NotExpression Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eclipselink Show documentation
Show all versions of eclipselink Show documentation
EclipseLink build based upon Git transaction f2b9fc5
/*
* Copyright (c) 2006, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation
//
package org.eclipse.persistence.jpa.jpql.parser;
import java.util.Collection;
import java.util.List;
import org.eclipse.persistence.jpa.jpql.WordParser;
/**
* BNF: expression ::= NOT conditional_primary
*
* @version 2.5
* @since 2.3
* @author Pascal Filion
*/
public final class NotExpression extends AbstractExpression {
/**
* The expression being negated by this expression.
*/
private AbstractExpression expression;
/**
* Determines whether a space was parsed after NOT.
*/
private boolean hasSpaceAfterNot;
/**
* The actual NOT identifier found in the string representation of the JPQL query.
*/
private String identifier;
/**
* Creates a new NotExpression
.
*
* @param parent The parent of this expression
*/
public NotExpression(AbstractExpression parent) {
super(parent, NOT);
}
@Override
public void accept(ExpressionVisitor visitor) {
visitor.visit(this);
}
@Override
public void acceptChildren(ExpressionVisitor visitor) {
getExpression().accept(visitor);
}
@Override
protected void addChildrenTo(Collection children) {
children.add(getExpression());
}
@Override
protected void addOrderedChildrenTo(List children) {
children.add(buildStringExpression(NOT));
if (hasSpaceAfterNot) {
children.add(buildStringExpression(SPACE));
}
if (expression != null) {
children.add(expression);
}
}
@Override
public JPQLQueryBNF findQueryBNF(Expression expression) {
if ((this.expression != null) && this.expression.isAncestor(expression)) {
return getQueryBNF(ConditionalPrimaryBNF.ID);
}
return super.findQueryBNF(expression);
}
/**
* Returns the actual NOT found in the string representation of the JPQL query, which has
* the actual case that was used.
*
* @return The NOT identifier that was actually parsed
*/
public String getActualIdentifier() {
return identifier;
}
/**
* Returns the {@link Expression} representing the expression that is negated.
*
* @return The expression representing the expression that is negated
*/
public Expression getExpression() {
if (expression == null) {
expression = buildNullExpression();
}
return expression;
}
@Override
public JPQLQueryBNF getQueryBNF() {
return getQueryBNF(ConditionalPrimaryBNF.ID);
}
/**
* Determines whether the expression to negate was parsed.
*
* @return true
if the expression to negate was parsed; false
if it was
* not parsed
*/
public boolean hasExpression() {
return expression != null &&
!expression.isNull();
}
/**
* Determines whether a whitespace was parsed after NOT.
*
* @return true
if a whitespace was parsed after the identifier NOT;
* false
otherwise
*/
public boolean hasSpaceAfterNot() {
return hasSpaceAfterNot;
}
@Override
protected boolean isParsingComplete(WordParser wordParser, String word, Expression expression) {
return wordParser.startsWithIdentifier(AND) ||
wordParser.startsWithIdentifier(OR) ||
super.isParsingComplete(wordParser, word, expression);
}
@Override
protected void parse(WordParser wordParser, boolean tolerant) {
identifier = wordParser.moveForward(NOT);
hasSpaceAfterNot = wordParser.skipLeadingWhitespace() > 0;
expression = parse(wordParser, ConditionalPrimaryBNF.ID, tolerant);
}
@Override
protected void toParsedText(StringBuilder writer, boolean actual) {
// NOT
writer.append(actual ? identifier : getText());
if (hasSpaceAfterNot) {
writer.append(SPACE);
}
// Expression
if (expression != null) {
expression.toParsedText(writer, actual);
}
}
}