package org.hibernate.criterion;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Criteria;
public class CountProjection extends AggregateProjection {
private boolean distinct;
protected CountProjection(String prop) {
super( "count", prop );
}
@Override
protected List buildFunctionParameterList(Criteria criteria, CriteriaQuery criteriaQuery) {
final String[] cols = criteriaQuery.getColumns( propertyName, criteria );
return ( distinct ? buildCountDistinctParameterList( cols ) : Arrays.asList( cols ) );
}
@SuppressWarnings("unchecked")
private List buildCountDistinctParameterList(String[] cols) {
final List params = new ArrayList( cols.length + 1 );
params.add( "distinct" );
params.addAll( Arrays.asList( cols ) );
return params;
}
public CountProjection setDistinct() {
distinct = true;
return this;
}
@Override
public String toString() {
if ( distinct ) {
return "distinct " + super.toString();
}
else {
return super.toString();
}
}
}