org.eclipse.persistence.jpa.jpql.tools.model.query.AggregateFunctionStateObject 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 org.eclipse.persistence.jpa.jpql.parser.AggregateFunction;
import static org.eclipse.persistence.jpa.jpql.parser.AbstractExpression.*;
import static org.eclipse.persistence.jpa.jpql.parser.Expression.*;
/**
* In the SELECT
clause the result of a query may be the result of an aggregate
* function applied to a path expression.
*
* BNF: aggregate_expression ::= {AVG|MAX|MIN|SUM}([DISTINCT] state_field_path_expression) |
* COUNT([DISTINCT] identification_variable | state_field_path_expression | single_valued_association_path_expression)
*
* @see org.eclipse.persistence.jpa.jpql.parser.AggregateFunction AggregateFunction
*
* @version 2.4
* @since 2.4
* @author Pascal Filion
*/
@SuppressWarnings({"nls", "unused"}) // unused used for the import statement: see bug 330740
public abstract class AggregateFunctionStateObject extends AbstractSingleEncapsulatedExpressionStateObject {
/**
* Determines whether the DISTINCT
keyword is part of the query, which is
* used to return only distinct (different) values.
*/
private boolean distinct;
/**
* Notifies the visibility of the DISTINCT
identifier has changed.
*/
public static final String DISTINCT_PROPERTY = "distinct";
/**
* Creates a new AggregateFunctionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @exception NullPointerException The given parent cannot be null
*/
protected AggregateFunctionStateObject(StateObject parent) {
super(parent);
}
/**
* Creates a new AggregateStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param distinct true
to add DISTINCT
to the query in order to
* have distinct values; false
if it is not required
* @param stateObject The {@link StateObject} representing the encapsulated expression
* @exception NullPointerException The given parent cannot be null
*/
protected AggregateFunctionStateObject(StateObject parent,
boolean distinct,
StateObject stateObject) {
super(parent, stateObject);
this.distinct = distinct;
}
/**
* Creates a new AggregateFunctionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param distinct true
to add DISTINCT
to the query in order to
* have distinct values; false
if it is not required
* @param path Either the identification variable or the state field path expression
* @exception NullPointerException The given parent cannot be null
*/
protected AggregateFunctionStateObject(StateObject parent, boolean distinct, String path) {
super(parent, path);
this.distinct = distinct;
}
/**
* Creates a new AggregateStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param stateObject The {@link StateObject} representing the encapsulated expression
* @exception NullPointerException The given parent cannot be null
*/
protected AggregateFunctionStateObject(StateObject parent, StateObject stateObject) {
this(parent, false, stateObject);
}
/**
* Creates a new AggregateFunctionStateObject
.
*
* @param parent The parent of this state object, which cannot be null
* @param path Either the identification variable or the state field path expression
* @exception NullPointerException The given parent cannot be null
*/
protected AggregateFunctionStateObject(StateObject parent, String path) {
super(parent, path);
}
/**
* {@inheritDoc}
*/
@Override
public AggregateFunction getExpression() {
return (AggregateFunction) super.getExpression();
}
/**
* Sets whether the DISTINCT
keyword should be part of the query, which is
* used to return only distinct (different) values.
*
* @return true
to add DISTINCT
to the query in order to have
* distinct values; false
if it is not required
*/
public boolean hasDistinct() {
return distinct;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEquivalent(StateObject stateObject) {
if (super.isEquivalent(stateObject)) {
AggregateFunctionStateObject function = (AggregateFunctionStateObject) stateObject;
return distinct == function.distinct;
}
return false;
}
/**
* Sets whether the DISTINCT
keyword should be part of the query, which is
* used to return only distinct (different) values
*
* @param distinct true
to add DISTINCT
to the query in order to
* have distinct values; false
if it is not required
*/
public void setDistinct(boolean distinct) {
boolean oldDistinct = this.distinct;
this.distinct = distinct;
firePropertyChanged(DISTINCT_PROPERTY, oldDistinct, distinct);
}
/**
* {@inheritDoc}
*/
@Override
public void setStateObject(StateObject stateObject) {
super.setStateObject(stateObject);
}
/**
* Reverses the visibility of the DISTINCT
identifier.
*/
public void toggleDistinct() {
setDistinct(!hasDistinct());
}
/**
* {@inheritDoc}
*/
@Override
protected void toTextEncapsulatedExpression(Appendable writer) throws IOException {
if (hasDistinct()) {
writer.append(DISTINCT);
writer.append(SPACE);
}
super.toTextEncapsulatedExpression(writer);
}
}