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

org.eclipse.persistence.jpa.jpql.tools.model.query.ExistsExpressionStateObject Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2011, 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.tools.model.query;

import java.io.IOException;
import org.eclipse.persistence.jpa.jpql.parser.ExistsExpression;
import org.eclipse.persistence.jpa.jpql.parser.SubqueryBNF;
import static org.eclipse.persistence.jpa.jpql.parser.AbstractExpression.*;

/**
 * An EXISTS expression is a predicate that is true only if the
 * result of the subquery consists of one or more values and that is false otherwise.
 *
 * 

BNF: exists_expression ::= [NOT] EXISTS(subquery)

* * @see ExistsExpression * * @version 2.4 * @since 2.4 * @author Pascal Filion */ @SuppressWarnings({"nls", "unused"}) // unused used for the import statement: see bug 330740 public class ExistsExpressionStateObject extends AbstractSingleEncapsulatedExpressionStateObject { /** * Determines whether the NOT identifier is part of the expression or not. */ private boolean not; /** * Notifies the visibility of the NOT identifier has changed. */ public static final String NOT_PROPERTY = "not"; /** * Creates a new ExistsExpressionStateObject. * * @param parent The parent of this state object, which cannot be null * @exception NullPointerException The given parent cannot be null */ public ExistsExpressionStateObject(StateObject parent) { super(parent); } /** * Creates a new ExistsExpressionStateObject. * * @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 subquery * @exception NullPointerException The given parent cannot be null */ public ExistsExpressionStateObject(StateObject parent, boolean not, StateObject stateObject) { super(parent, stateObject); this.not = not; } /** * Creates a new ExistsExpressionStateObject. * * @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 jpqlFragment The portion of the query representing the encapsulated expression * @exception NullPointerException The given parent cannot be null */ public ExistsExpressionStateObject(StateObject parent, boolean not, String jpqlFragment) { super(parent, jpqlFragment); this.not = not; } /** * Creates a new ExistsExpressionStateObject. * * @param parent The parent of this state object, which cannot be null * @param stateObject The {@link StateObject} representing the subquery * @exception NullPointerException The given parent cannot be null */ public ExistsExpressionStateObject(StateObject parent, StateObject stateObject) { this(parent, false, stateObject); } /** * Creates a new ExistsExpressionStateObject. * * @param parent The parent of this state object, which cannot be null * @param jpqlFragment The portion of the query representing the encapsulated expression * @exception NullPointerException The given parent cannot be null */ public ExistsExpressionStateObject(StateObject parent, String jpqlFragment) { super(parent, jpqlFragment); } @Override public void accept(StateObjectVisitor visitor) { visitor.visit(this); } /** * Makes sure the NOT identifier is specified. * * @return This object */ public ExistsExpressionStateObject addNot() { if (!not) { setNot(true); } return this; } @Override public ExistsExpression getExpression() { return (ExistsExpression) super.getExpression(); } @Override public String getIdentifier() { return EXISTS; } @Override protected String getQueryBNFId() { return SubqueryBNF.ID; } /** * 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; } @Override public boolean isEquivalent(StateObject stateObject) { if (super.isEquivalent(stateObject)) { ExistsExpressionStateObject exists = (ExistsExpressionStateObject) stateObject; return not == exists.not; } return false; } /** * Makes sure the NOT identifier is not specified. */ public void removeNot() { if (not) { setNot(false); } } /** * Keeps a reference of the {@link ExistsExpression 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 ExistsExpression parsed object} representing an EXISTS * expression */ public void setExpression(ExistsExpression 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); } @Override public void setStateObject(StateObject stateObject) { super.setStateObject(stateObject); } /** * Changes the visibility state of the NOT identifier. */ public void toggleNot() { setNot(!not); } @Override protected void toTextEncapsulatedExpression(Appendable writer) throws IOException { writer.append(not ? NOT_EXISTS : EXISTS); writer.append(LEFT_PARENTHESIS); super.toTextEncapsulatedExpression(writer); writer.append(RIGHT_PARENTHESIS); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy