1 15 package org.josql.internal; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 20 import org.josql.expressions.ValueExpression; 21 22 import org.josql.Query; 23 import org.josql.QueryParseException; 24 import org.josql.QueryExecutionException; 25 26 public class Limit 27 { 28 29 private ValueExpression start = null; 30 private ValueExpression rowsCount = null; 31 32 public Limit () 33 { 34 35 } 36 37 public void init (Query q) 38 throws QueryParseException 39 { 40 41 if (this.start != null) 43 { 44 45 this.start.init (q); 46 47 Class c = this.start.getExpectedReturnType (q); 50 51 if (!Utilities.isNumber (c)) 52 { 53 54 throw new QueryParseException ("The expected return type of the start expression: \"" + 55 this.start + 56 "\" of the LIMIT clause is: " + 57 c.getName () + 58 " however the expression when evaluated must return a numeric result."); 59 60 } 61 62 } 63 64 this.rowsCount.init (q); 65 66 69 Class c = this.rowsCount.getExpectedReturnType (q); 71 72 if (!Utilities.isNumber (c)) 73 { 74 75 throw new QueryParseException ("The expected return type of the rows count expression: \"" + 76 this.rowsCount + 77 "\" of the LIMIT clause is: " + 78 c.getName () + 79 " however the expression when evaluated must return a numeric result."); 80 81 } 82 83 } 84 85 public List getSubList (List objs, 86 Query q) 87 throws QueryExecutionException 88 { 89 90 Object o = this.rowsCount.evaluate (null, 92 q); 93 94 int rows = -1; 95 96 if ((o != null) 98 && 99 (!(o instanceof Number )) 100 ) 101 { 102 103 throw new QueryExecutionException ("Return value of rows count expression: \"" + 104 this.rowsCount + 105 "\" for the LIMIT clause is of type: " + 106 o.getClass ().getName () + 107 " expected it to return a numeric value."); 108 109 } 110 111 if (o != null) 112 { 113 114 rows = ((Number ) o).intValue (); 116 117 } 118 119 int start = 0; 120 121 if (this.start != null) 123 { 124 125 Object s = this.start.evaluate (null, 126 q); 127 128 if ((s != null) 130 && 131 (!(s instanceof Number )) 132 ) 133 { 134 135 throw new QueryExecutionException ("Return value of the start expression: \"" + 136 this.start + 137 "\" for the LIMIT clause is of type: " + 138 s.getClass ().getName () + 139 " expected it to return a numeric value."); 140 141 } 142 143 if (s != null) 144 { 145 146 start = ((Number ) s).intValue (); 148 149 start--; 151 152 } 153 154 } 155 156 int ls = objs.size (); 157 158 if (start > (ls - 1)) 160 { 161 162 return new ArrayList (); 164 165 } 166 167 if (rows > 0) 168 { 169 170 if ((start + rows) > (ls - 1)) 171 { 172 173 return new ArrayList (objs.subList (start, 176 ls)); 177 178 } 179 180 return new ArrayList (objs.subList (start, 182 start + rows)); 183 184 } else { 185 186 return new ArrayList (objs.subList (start, 188 ls)); 189 190 } 191 192 } 193 194 public void setStart (ValueExpression v) 195 { 196 197 this.start = v; 198 199 } 200 201 public void setRowsCount (ValueExpression v) 202 { 203 204 this.rowsCount = v; 205 206 } 207 208 } 209 | Popular Tags |