
dk.eobjects.metamodel.query.OrderByItem Maven / Gradle / Ivy
/**
* This file is part of MetaModel.
*
* MetaModel is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MetaModel 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MetaModel. If not, see .
*/
package dk.eobjects.metamodel.query;
import org.apache.commons.lang.builder.EqualsBuilder;
/**
* Represents an ORDER BY item. An OrderByItem sorts the resulting DataSet
* according to a SelectItem that may or may not be a part of the query already.
*
* @see OrderByClause
* @see SelectItem
*/
public class OrderByItem implements IQueryItem, Cloneable {
public enum Direction {
ASC, DESC
}
private static final long serialVersionUID = -8397473619828484774L;
private Direction _direction;
private SelectItem _selectItem;
private Query _query;
/**
* Private constructor, used for cloning
*/
private OrderByItem() {
}
/**
* Creates an OrderByItem
*
* @param selectItem
* the select item to order
* @param direction
* the direction to order the select item
*/
public OrderByItem(SelectItem selectItem, Direction direction) {
if (selectItem == null) {
throw new IllegalArgumentException("SelectItem cannot be null");
}
_selectItem = selectItem;
_direction = direction;
}
/**
* Creates an OrderByItem
*
* @param selectItem
* @param ascending
* @deprecated user OrderByItem(SelectItem, Direction) instead
*/
@Deprecated
public OrderByItem(SelectItem selectItem, boolean ascending) {
if (selectItem == null) {
throw new IllegalArgumentException("SelectItem cannot be null");
}
_selectItem = selectItem;
if (ascending) {
_direction = Direction.ASC;
} else {
_direction = Direction.DESC;
}
}
/**
* Creates an ascending OrderByItem
*
* @param selectItem
*/
public OrderByItem(SelectItem selectItem) {
this(selectItem, Direction.ASC);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(_selectItem.getSameQueryAlias() + ' ');
sb.append(_direction);
return sb.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof OrderByItem) {
OrderByItem that = (OrderByItem) obj;
EqualsBuilder eb = new EqualsBuilder();
eb.append(this.getDirection(), that.getDirection());
eb.append(this.getSelectItem(), that.getSelectItem());
return eb.isEquals();
}
return false;
}
public boolean isAscending() {
return (_direction == Direction.ASC);
}
public boolean isDescending() {
return (_direction == Direction.DESC);
}
public Direction getDirection() {
return _direction;
}
public OrderByItem setDirection(Direction direction) {
_direction = direction;
return this;
}
public SelectItem getSelectItem() {
return _selectItem;
}
public Query getQuery() {
return _query;
}
public OrderByItem setQuery(Query query) {
_query = query;
if (_selectItem != null) {
_selectItem.setQuery(query);
}
return this;
}
@Override
protected OrderByItem clone() {
OrderByItem o = new OrderByItem();
o._direction = _direction;
o._selectItem = _selectItem.clone();
return o;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy