001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.algebra;
007:
008: /**
009: * The SLICE operator, as defined in <a
010: * href="http://www.w3.org/TR/rdf-sparql-query/#defn_algSlice">SPARQL Query
011: * Language for RDF</a>. The SLICE operator selects specific results from the
012: * underlying tuple expression based onan offset and limit value (both
013: * optional).
014: *
015: * @author Arjohn Kampman
016: */
017: public class Slice extends UnaryTupleOperator {
018:
019: /*-----------*
020: * Variables *
021: *-----------*/
022:
023: private int offset;
024:
025: private int limit;
026:
027: /*--------------*
028: * Constructors *
029: *--------------*/
030:
031: public Slice() {
032: }
033:
034: public Slice(TupleExpr arg) {
035: this (arg, 0, -1);
036: }
037:
038: public Slice(TupleExpr arg, int offset, int limit) {
039: super (arg);
040: setOffset(offset);
041: setLimit(limit);
042: }
043:
044: /*---------*
045: * Methods *
046: *---------*/
047:
048: public int getOffset() {
049: return offset;
050: }
051:
052: public void setOffset(int offset) {
053: this .offset = offset;
054: }
055:
056: /**
057: * Checks whether the row selection has a (valid) offset.
058: *
059: * @return <tt>true</tt> when <tt>offset > 0</tt>
060: */
061: public boolean hasOffset() {
062: return offset > 0;
063: }
064:
065: public int getLimit() {
066: return limit;
067: }
068:
069: public void setLimit(int limit) {
070: this .limit = limit;
071: }
072:
073: /**
074: * Checks whether the row selection has a (valid) limit.
075: *
076: * @return <tt>true</tt> when <tt>offset >= 0</tt>
077: */
078: public boolean hasLimit() {
079: return limit >= 0;
080: }
081:
082: public <X extends Exception> void visit(QueryModelVisitor<X> visitor)
083: throws X {
084: visitor.meet(this );
085: }
086:
087: @Override
088: public String getSignature() {
089: StringBuilder sb = new StringBuilder(256);
090:
091: sb.append(super .getSignature());
092: sb.append(" ( ");
093:
094: if (hasLimit()) {
095: sb.append("limit=").append(getLimit());
096: }
097: if (hasOffset()) {
098: sb.append("offset=").append(getOffset());
099: }
100:
101: sb.append(" )");
102:
103: return sb.toString();
104: }
105:
106: @Override
107: public Slice clone() {
108: return (Slice) super.clone();
109: }
110: }
|