Source Code Cross Referenced for Limit.java in  » Development » JoSQL » org » josql » internal » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » JoSQL » org.josql.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2007 Gary Bentley 
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License"); you may 
005:         * not use this file except in compliance with the License. 
006:         * You may obtain a copy of the License at 
007:         *    http://www.apache.org/licenses/LICENSE-2.0 
008:         *
009:         * Unless required by applicable law or agreed to in writing, software 
010:         * distributed under the License is distributed on an "AS IS" BASIS, 
011:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
012:         * See the License for the specific language governing permissions and 
013:         * limitations under the License.
014:         */
015:        package org.josql.internal;
016:
017:        import java.util.List;
018:        import java.util.ArrayList;
019:
020:        import org.josql.expressions.ValueExpression;
021:
022:        import org.josql.Query;
023:        import org.josql.QueryParseException;
024:        import org.josql.QueryExecutionException;
025:
026:        public class Limit {
027:
028:            private ValueExpression start = null;
029:            private ValueExpression rowsCount = null;
030:
031:            public Limit() {
032:
033:            }
034:
035:            public void init(Query q) throws QueryParseException {
036:
037:                // Init the value expressions...
038:                if (this .start != null) {
039:
040:                    this .start.init(q);
041:
042:                    // Should probably check to ensure that accessors aren't used... since
043:                    // we don't have a "current object" to work on!.
044:                    Class c = this .start.getExpectedReturnType(q);
045:
046:                    if (!Utilities.isNumber(c)) {
047:
048:                        throw new QueryParseException(
049:                                "The expected return type of the start expression: \""
050:                                        + this .start
051:                                        + "\" of the LIMIT clause is: "
052:                                        + c.getName()
053:                                        + " however the expression when evaluated must return a numeric result.");
054:
055:                    }
056:
057:                }
058:
059:                this .rowsCount.init(q);
060:
061:                // Should probably check to ensure that accessors aren't used... since
062:                // we don't have a "current object" to work on!.
063:
064:                // Try and determine what the expected type is that we will return.
065:                Class c = this .rowsCount.getExpectedReturnType(q);
066:
067:                if (!Utilities.isNumber(c)) {
068:
069:                    throw new QueryParseException(
070:                            "The expected return type of the rows count expression: \""
071:                                    + this .rowsCount
072:                                    + "\" of the LIMIT clause is: "
073:                                    + c.getName()
074:                                    + " however the expression when evaluated must return a numeric result.");
075:
076:                }
077:
078:            }
079:
080:            public List getSubList(List objs, Query q)
081:                    throws QueryExecutionException {
082:
083:                // Get the row count.
084:                Object o = this .rowsCount.evaluate(null, q);
085:
086:                int rows = -1;
087:
088:                // Ensure that it is a number.
089:                if ((o != null) && (!(o instanceof  Number))) {
090:
091:                    throw new QueryExecutionException(
092:                            "Return value of rows count expression: \""
093:                                    + this .rowsCount
094:                                    + "\" for the LIMIT clause is of type: "
095:                                    + o.getClass().getName()
096:                                    + " expected it to return a numeric value.");
097:
098:                }
099:
100:                if (o != null) {
101:
102:                    // There are rounding issues here, but if the user provides a float/double value...
103:                    rows = ((Number) o).intValue();
104:
105:                }
106:
107:                int start = 0;
108:
109:                // Now get the start value...
110:                if (this .start != null) {
111:
112:                    Object s = this .start.evaluate(null, q);
113:
114:                    // Ensure that it is a number.
115:                    if ((s != null) && (!(s instanceof  Number))) {
116:
117:                        throw new QueryExecutionException(
118:                                "Return value of the start expression: \""
119:                                        + this .start
120:                                        + "\" for the LIMIT clause is of type: "
121:                                        + s.getClass().getName()
122:                                        + " expected it to return a numeric value.");
123:
124:                    }
125:
126:                    if (s != null) {
127:
128:                        // There are rounding issues here, but if the user provides a float/double value...
129:                        start = ((Number) s).intValue();
130:
131:                        // Whilst for the user rows start at 1, for us they start at 0...
132:                        start--;
133:
134:                    }
135:
136:                }
137:
138:                int ls = objs.size();
139:
140:                // Now get our sub-list.
141:                if (start > (ls - 1)) {
142:
143:                    // Return nothing, outside of the range.
144:                    return new ArrayList();
145:
146:                }
147:
148:                if (rows > 0) {
149:
150:                    if ((start + rows) > (ls - 1)) {
151:
152:                        // Just return the rows starting at start...
153:                        // We return a new list to prevent issues with modifications...
154:                        return new ArrayList(objs.subList(start, ls));
155:
156:                    }
157:
158:                    // Here we return start + rows.
159:                    return new ArrayList(objs.subList(start, start + rows));
160:
161:                } else {
162:
163:                    // Just ignore the rows...
164:                    return new ArrayList(objs.subList(start, ls));
165:
166:                }
167:
168:            }
169:
170:            public void setStart(ValueExpression v) {
171:
172:                this .start = v;
173:
174:            }
175:
176:            public void setRowsCount(ValueExpression v) {
177:
178:                this.rowsCount = v;
179:
180:            }
181:
182:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.