org.eclipse.persistence.jpa.jpql.tools.model.query.LikeExpressionStateObject 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 180e602
/*******************************************************************************
* Copyright (c) 2011, 2014 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 v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* 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.LikeExpression;
import static org.eclipse.persistence.jpa.jpql.parser.AbstractExpression.*;
import static org.eclipse.persistence.jpa.jpql.parser.Expression.*;
/**
* The LIKE
condition is used to specify a search for a pattern.
*
* The string_expression
must have a string value. The pattern_value
is a
* string literal or a string-valued input parameter in which an underscore (_) stands for any
* single character, a percent (%) character stands for any sequence of characters (including the
* empty sequence), and all other characters stand for themselves. The optional escape_character
* is a single-character string literal or a character-valued input parameter (i.e., char or
* Character) and is used to escape the special meaning of the underscore and percent characters in
* pattern_value
.
*
*
BNF: like_expression ::= string_expression [NOT] LIKE pattern_value [ESCAPE escape_character]
*
* @see LikeExpression
*
* @version 2.4
* @since 2.4
* @author Pascal Filion
*/
@SuppressWarnings({"nls", "unused"}) // unused used for the import statement: see bug 330740
public class LikeExpressionStateObject extends AbstractStateObject {
/**
*
*/
private StringLiteralStateObject escapeCharacter;
/**
* Determines whether the NOT
identifier is part of the expression or not.
*/
private boolean not;
/**
*
*/
private StateObject patternValue;
/**
*
*/
private StateObject stringStateObject;
/**
* Notifies the escape character property has changed.
*/
public static final String ESCAPE_CHARACTER_PROPERTY = "escapeCharacter";
/**
* Notifies the visibility of the NOT
identifier has changed.
*/
public static String NOT_PROPERTY = "not";
/**
* Notifies the pattern value property has changed.
*/
public static final String PATTERN_VALUE_PROPERTY = "patternValue";
/**
* Notifies the string state object property has changed.
*/
public static final String STRING_STATE_OBJECT_PROPERTY = "stringStateObject";
/**
* Creates a new LikeExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @exception NullPointerException The given parent cannot be null
*/
public LikeExpressionStateObject(StateObject parent) {
super(parent);
}
/**
* Creates a new LikeExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param stringStateObject The {@link StateObject} representing the string expression
* @exception NullPointerException The given parent cannot be null
*/
public LikeExpressionStateObject(StateObject parent,
StateObject stringStateObject) {
this(parent, stringStateObject, false, null, null);
}
/**
* Creates a new LikeExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param stringStateObject The {@link StateObject} representing the string expression
* @param not Determines whether the NOT
identifier is part of the expression
* or not
* @param patternValue A string literal or a string-valued input parameter in which an underscore
* (_) stands for any single character, a percent (%) character stands for any sequence of
* characters (including the empty sequence), and all other characters stand for themselves
* @param escapeCharacter A single-character string literal or a character-valued input parameter
* (i.e., char or Character) and is used to escape the special meaning of the underscore and
* percent characters in pattern_value
* @exception NullPointerException The given parent cannot be null
*/
public LikeExpressionStateObject(StateObject parent,
StateObject stringStateObject,
boolean not,
StateObject patternValue,
String escapeCharacter) {
super(parent);
this.stringStateObject = parent(stringStateObject);
this.not = not;
this.patternValue = parent(patternValue);
this.escapeCharacter.setTextInternally(escapeCharacter);
}
/**
* Creates a new LikeExpressionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param stringStateObject The {@link StateObject} representing the string expression
* @param patternValue a string literal or a string-valued input parameter in which an underscore
* (_) stands for any single character, a percent (%) character stands for any sequence of
* characters (including the empty sequence), and all other characters stand for themselves
* @exception NullPointerException The given parent cannot be null
*/
public LikeExpressionStateObject(StateObject parent,
StateObject stringStateObject,
StateObject patternValue) {
this(parent, stringStateObject, false, patternValue, null);
}
/**
* {@inheritDoc}
*/
public void accept(StateObjectVisitor visitor) {
visitor.visit(this);
}
/**
* {@inheritDoc}
*/
@Override
protected void addChildren(List children) {
super.addChildren(children);
if (stringStateObject != null) {
children.add(stringStateObject);
}
if (patternValue != null) {
children.add(patternValue);
}
}
/**
* Makes sure the NOT
identifier is specified.
*
* @return This object
*/
public LikeExpressionStateObject addNot() {
if (!not) {
setNot(true);
}
return this;
}
public String getEscapeCharacter() {
return escapeCharacter.getText();
}
/**
* {@inheritDoc}
*/
@Override
public LikeExpression getExpression() {
return (LikeExpression) super.getExpression();
}
public StateObject getPatternValue() {
return patternValue;
}
public StateObject getStringStateObject() {
return stringStateObject;
}
public boolean hasEscapeCharacter() {
return escapeCharacter.hasText();
}
/**
* 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;
}
public boolean hasPatternValue() {
return patternValue != null;
}
public boolean hasStringStateObject() {
return stringStateObject != null;
}
/**
* {@inheritDoc}
*/
@Override
protected void initialize() {
super.initialize();
escapeCharacter = new StringLiteralStateObject(this);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEquivalent(StateObject stateObject) {
if (super.isEquivalent(stateObject)) {
LikeExpressionStateObject like = (LikeExpressionStateObject) stateObject;
return not == like.not &&
areEquivalent(patternValue, like.patternValue) &&
areEquivalent(escapeCharacter, like.escapeCharacter) &&
areEquivalent(stringStateObject, like.stringStateObject);
}
return false;
}
/**
* Makes sure the NOT
identifier is not specified.
*/
public void removeNot() {
if (not) {
setNot(false);
}
}
/**
*
*/
public void setEscapeCharacter(String escapeCharacter) {
String oldEscapeCharacter = getEscapeCharacter();
this.escapeCharacter.setText(escapeCharacter);
firePropertyChanged(ESCAPE_CHARACTER_PROPERTY, oldEscapeCharacter, escapeCharacter);
}
/**
* Keeps a reference of the {@link LikeExpression 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 LikeExpression parsed object} representing a LIKE
* expression
*/
public void setExpression(LikeExpression 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);
}
public void setPatternValue(StateObject patternValue) {
StateObject oldPatternValue = this.patternValue;
this.patternValue = parent(patternValue);
firePropertyChanged(PATTERN_VALUE_PROPERTY, oldPatternValue, patternValue);
}
public void setStringStateObject(StateObject stringStateObject) {
StateObject oldStringStateObject = this.stringStateObject;
this.stringStateObject = parent(stringStateObject);
firePropertyChanged(STRING_STATE_OBJECT_PROPERTY, oldStringStateObject, stringStateObject);
}
/**
* Changes the visibility state of the NOT
identifier.
*/
public void toggleNot() {
setNot(!not);
}
/**
* {@inheritDoc}
*/
@Override
protected void toTextInternal(Appendable writer) throws IOException {
if (stringStateObject != null) {
stringStateObject.toString(writer);
writer.append(SPACE);
}
writer.append(not ? NOT_LIKE : LIKE);
if (patternValue != null) {
writer.append(SPACE);
patternValue.toString(writer);
}
if (hasEscapeCharacter()) {
writer.append(SPACE);
writer.append(ESCAPE);
writer.append(SPACE);
escapeCharacter.toTextInternal(writer);
}
}
}