View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.query.model;
25  
26  import net.jcip.annotations.Immutable;
27  import org.modeshape.common.util.CheckArg;
28  
29  /**
30   * 
31   */
32  @Immutable
33  public class Limit implements LanguageObject {
34      private static final long serialVersionUID = 1L;
35  
36      public static final Limit NONE = new Limit(Integer.MAX_VALUE, 0);
37  
38      private final int offset;
39      private final int rowLimit;
40  
41      /**
42       * Create a limit on the number of rows.
43       * 
44       * @param rowLimit the maximum number of rows
45       * @throws IllegalArgumentException if the row limit is negative
46       */
47      public Limit( int rowLimit ) {
48          CheckArg.isNonNegative(rowLimit, "rowLimit");
49          this.rowLimit = rowLimit;
50          this.offset = 0;
51      }
52  
53      /**
54       * Create a limit on the number of rows and the number of initial rows to skip.
55       * 
56       * @param rowLimit the maximum number of rows
57       * @param offset the number of rows to skip before beginning the results
58       * @throws IllegalArgumentException if the row limit is negative, or if the offset is negative
59       */
60      public Limit( int rowLimit,
61                    int offset ) {
62          CheckArg.isNonNegative(rowLimit, "rowLimit");
63          CheckArg.isNonNegative(offset, "offset");
64          this.rowLimit = rowLimit;
65          this.offset = offset;
66      }
67  
68      /**
69       * Get the number of rows skipped before the results begin.
70       * 
71       * @return the offset; always 0 or a positive number
72       */
73      public final int offset() {
74          return offset;
75      }
76  
77      /**
78       * Get the maximum number of rows that are to be returned.
79       * 
80       * @return the maximum number of rows; never negative, or equal to {@link Integer#MAX_VALUE} if there is no limit
81       */
82      public final int rowLimit() {
83          return rowLimit;
84      }
85  
86      /**
87       * Determine whether this limit clause is necessary.
88       * 
89       * @return true if the number of rows is not limited and there is no offset, or false otherwise
90       */
91      public final boolean isUnlimited() {
92          return rowLimit == Integer.MAX_VALUE && offset == 0;
93      }
94  
95      /**
96       * Determine whether this limit clause defines a maximum limit
97       * 
98       * @return true if the number of rows are limited, or false if there is no limit to the number of rows
99       */
100     public final boolean hasRowLimited() {
101         return rowLimit != Integer.MAX_VALUE;
102     }
103 
104     /**
105      * Determine whether this limit clause defines an offset.
106      * 
107      * @return true if there is an offset, or false if there is no offset
108      */
109     public final boolean isOffset() {
110         return offset > 0;
111     }
112 
113     /**
114      * {@inheritDoc}
115      * 
116      * @see java.lang.Object#toString()
117      */
118     @Override
119     public String toString() {
120         return Visitors.readable(this);
121     }
122 
123     /**
124      * {@inheritDoc}
125      * 
126      * @see java.lang.Object#hashCode()
127      */
128     @Override
129     public int hashCode() {
130         return rowLimit;
131     }
132 
133     /**
134      * {@inheritDoc}
135      * 
136      * @see java.lang.Object#equals(java.lang.Object)
137      */
138     @Override
139     public boolean equals( Object obj ) {
140         if (obj == this) return true;
141         if (obj instanceof Limit) {
142             Limit that = (Limit)obj;
143             return this.offset == that.offset && this.rowLimit == that.rowLimit;
144         }
145         return false;
146     }
147 
148     /**
149      * {@inheritDoc}
150      * 
151      * @see org.modeshape.graph.query.model.Visitable#accept(org.modeshape.graph.query.model.Visitor)
152      */
153     public void accept( Visitor visitor ) {
154         visitor.visit(this);
155     }
156 
157     public Limit withRowLimit( int rowLimit ) {
158         return new Limit(rowLimit, offset);
159     }
160 
161     public Limit withOffset( int offset ) {
162         return new Limit(rowLimit, offset);
163     }
164 
165 }