org.eclipse.persistence.jpa.jpql.tools.model.query.NullComparisonExpressionStateObject 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 67768906cf
/*
* Copyright (c) 2011, 2018 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.tools.model.query;
import java.io.IOException;
import java.util.List;
import org.eclipse.persistence.jpa.jpql.parser.LiteralBNF;
import org.eclipse.persistence.jpa.jpql.parser.NullComparisonExpression;
import static org.eclipse.persistence.jpa.jpql.parser.AbstractExpression.*;
import static org.eclipse.persistence.jpa.jpql.parser.Expression.*;
/**
* A null comparison tests whether or not the single-valued path expression or input parameter is a
* NULL
value.
*
* BNF: null_comparison_expression ::= {single_valued_path_expression | input_parameter} IS [NOT] NULL
*
* @see NullComparisonExpression
*
* @version 2.4
* @since 2.4
* @author Pascal Filion
*/
@SuppressWarnings({"nls", "unused"}) // unused used for the import statement: see bug 330740
public class NullComparisonExpressionStateObject extends AbstractStateObject {
/**
* Determines whether the NOT
identifier is part of the expression or not.
*/
private boolean not;
/**
* The {@link StateObject} representing the collection-valued path expression.
*/
private StateObject stateObject;
/**
* Notifies the visibility of the NOT
identifier has changed.
*/
public static final String NOT_PROPERTY = "not";
/**
* Notifies the state object property has changed.
*/
public static final String STATE_OBJECT_PROPERTY = "stateObject";
/**
* Creates a new NullComparisonExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @exception NullPointerException The given parent cannot be null
*/
public NullComparisonExpressionStateObject(StateObject parent) {
super(parent);
}
/**
* Creates a new NullComparisonExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param not Determines whether the NOT
identifier is part of the expression
* or not
* @param stateObject The {@link StateObject} representing the collection-valued path expression
* @exception NullPointerException The given parent cannot be null
*/
public NullComparisonExpressionStateObject(StateObject parent,
boolean not,
StateObject stateObject) {
super(parent);
this.not = not;
this.stateObject = parent(stateObject);
}
/**
* Creates a new NullComparisonExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param not Determines whether the NOT
identifier is part of the expression
* or not
* @param path Either the singled-valued path expression or the input parameter
* @exception NullPointerException The given parent cannot be null
*/
public NullComparisonExpressionStateObject(StateObject parent,
boolean not,
String path) {
super(parent);
this.not = not;
parse(path);
}
/**
* Creates a new NullComparisonExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param stateObject The {@link StateObject} representing the collection-valued path expression
* @exception NullPointerException The given parent cannot be null
*/
public NullComparisonExpressionStateObject(StateObject parent, StateObject stateObject) {
this(parent, false, stateObject);
}
/**
* Creates a new NullComparisonExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param path Either the singled-valued path expression or the input parameter
* @exception NullPointerException The given parent cannot be null
*/
public NullComparisonExpressionStateObject(StateObject parent, String path) {
super(parent);
parse(path);
}
/**
* {@inheritDoc}
*/
public void accept(StateObjectVisitor visitor) {
visitor.visit(this);
}
/**
* {@inheritDoc}
*/
@Override
protected void addChildren(List children) {
super.addChildren(children);
if (stateObject != null) {
children.add(stateObject);
}
}
/**
* Makes sure the NOT
identifier is specified.
*
* @return This object
*/
public NullComparisonExpressionStateObject addNot() {
if (!not) {
setNot(true);
}
return this;
}
/**
* {@inheritDoc}
*/
@Override
public NullComparisonExpression getExpression() {
return (NullComparisonExpression) super.getExpression();
}
/**
* Returns the {@link StateObject} representing the collection-valued path expression.
*
* @return The {@link StateObject} representing the collection-valued path expression
*/
public StateObject getStateObject() {
return stateObject;
}
/**
* Determines whether the NOT
identifier is used or not.
*
* @return true
if the NOT
identifier is part of the expression;
* false
otherwise
*/
public boolean hasNot() {
return not;
}
/**
* Determines whether there is a {@link StateObject} representing the collection-valued path
* expression.
*
* @return true
if the {@link StateObject} is not null
; false
* otherwise
*/
public boolean hasStateObject() {
return stateObject != null;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEquivalent(StateObject stateObject) {
if (super.isEquivalent(stateObject)) {
NullComparisonExpressionStateObject comparison = (NullComparisonExpressionStateObject) stateObject;
return not == comparison.not &&
areEquivalent(stateObject, comparison.stateObject);
}
return false;
}
/**
* Parses the given singled-valued path expression or input parameter.
*
* @param path Either a singled-valued path expression or an input parameter
*/
private void parse(String path) {
StateObject stateObject = buildStateObject(path, LiteralBNF.ID);
setStateObject(stateObject);
}
/**
* Makes sure the NOT
identifier is not specified.
*/
public void removeNot() {
if (not) {
setNot(false);
}
}
/**
* Keeps a reference of the {@link NullComparisonExpression parsed object} object, which should only be
* done when this object is instantiated during the conversion of a parsed JPQL query into
* {@link StateObject StateObjects}.
*
* @param expression The {@link NullComparisonExpression parsed object} representing the null
* expression
*/
public void setExpression(NullComparisonExpression expression) {
super.setExpression(expression);
}
/**
* Sets whether the NOT
identifier should be part of the expression or not.
*
* @param not true
if the NOT
identifier should be part of the
* expression; false
otherwise
*/
public void setNot(boolean not) {
boolean oldNot = this.not;
this.not = not;
firePropertyChanged(NOT_PROPERTY, oldNot, not);
}
/**
* Sets the given {@link StateObject} as the collection-valued path expression.
*
* @param stateObject The {@link StateObject} that represents the collection-valued path expression
*/
public void setStateObject(StateObject stateObject) {
StateObject oldStateObject = this.stateObject;
this.stateObject = parent(stateObject);
firePropertyChanged(STATE_OBJECT_PROPERTY, oldStateObject, stateObject);
}
/**
* Changes the visibility state of the NOT
identifier.
*/
public void toggleNot() {
setNot(!not);
}
/**
* {@inheritDoc}
*/
@Override
protected void toTextInternal(Appendable writer) throws IOException {
if (stateObject != null) {
stateObject.toString(writer);
writer.append(SPACE);
}
if (not) {
writer.append(IS_NOT_NULL);
}
else {
writer.append(IS_NULL);
}
}
}