
org.ctoolkit.services.storage.criteria.Criteria Maven / Gradle / Ivy
/*
* Copyright (c) 2017 Comvai, s.r.o. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.ctoolkit.services.storage.criteria;
import java.util.ArrayList;
import java.util.List;
/**
* Criteria class is helper class for creating criteria.
*
* Usage:
*
* Criteria<Entity> crit = Criteria.create(Entity.class);
* crit.add(Restrictions.eq("id", 1);
*
*
* @author Jozef Pohorelec
*/
public class Criteria
{
private int maxResults = -1;
private int firstResult = -1;
private Class entity;
private List expressionList = new ArrayList<>();
private List orderRules = new ArrayList<>();
private Criteria( Class entity )
{
this.entity = entity;
}
/**
* Construct criteria object.
*
* @param entity entity on which criteria will be applied
* @return new instance of {@link Criteria}
*/
public static Criteria create( Class entity )
{
return new Criteria<>( entity );
}
public Class getEntityClass()
{
return entity;
}
/**
* Add criteria expression to criteria
*
* @param expression implementation of {@link Expression}
* @return this
*/
public Criteria addCriteria( Expression expression )
{
expressionList.add( expression );
return this;
}
/**
* Returns the list of Expression
.
*
* @return the list of Expression
.
*/
public List getExpressionList()
{
return expressionList;
}
/**
* Returns the maximum number of results to retrieve.
*
* @return the maximum number of results to retrieve.
*/
public int getMaxResults()
{
return maxResults;
}
/**
* Sets the maximum number of results to retrieve.
*
* @param maxResults max results of query
*/
public void setMaxResults( int maxResults )
{
this.maxResults = maxResults;
}
/**
* Returns the position of the first result to retrieve.
*
* @return the position of the first result to retrieve.
*/
public int getFirstResult()
{
return firstResult;
}
/**
* Sets the position of the first result to retrieve.
*
* @param firstResult first result of query
*/
public void setFirstResult( int firstResult )
{
this.firstResult = firstResult;
}
/**
* Add order rule for query. Order rule will be applied as follows:
* order by e.propertyName asc[desc]
*
* @param propertyName name of property on which order will be applied
* @param order type of order asc[desc]
* @return this
*/
public Criteria addOrderRule( String propertyName, Order order )
{
orderRules.add( new OrderRule( propertyName, order ) );
return this;
}
/**
* Returns the list of OrderRule
.
*
* @return the list of OrderRule
.
*/
public List getOrderRules()
{
return orderRules;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy