Java Doc for JoSQLComparator.java in  » Development » JoSQL » org » josql » utils » 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.utils 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.josql.utils.JoSQLComparator

JoSQLComparator
public class JoSQLComparator implements Comparator(Code)
This class allows the ORDER BY clause of a JoSQL SQL clause to be used as a Comparator. It should be noted that is the same as performing: Query.execute(List) but there are times when having a separate comparator is desirable. The EXECUTE ON ALL clause is supported but you must call: JoSQLComparator.doExecuteOn(List) first to ensure that they are executed.

This class is basically just a thin wrapper around using the comparator gained by calling: Query.getOrderByComparator .

A note on performance, for small numbers of objects (around 1000) this comparator has (for vanilla accessors, no function calls) pretty comparable performance against a hand-coded Java Comparator that performs the same function. However start to scale the numbers of objects and performance degrades, in testing for ~34000 FileWrapper objects to order by: path DESC, lastModified, name, length took around: 1300ms. The hand-coded Java Comparator took around: 180ms! The upshot is, if you need flexibility and do not need to order large numbers of objects then use this kind of Comparator, if performance and numbers of objects is an issue then hand-rolling your own Comparator is probably best. As a side-note, to perform the following order by: lower(path) DESC, lastModified, name, length using a JoSQLComparator took: about: 1400ms. However modifying the hand-coded Comparator to use: String.compareToIgnoreCase(String) then took about 860ms! And if you using: String.toLowerCase for each string instead, it then takes about: 1800ms! (Meaning that in certain circumstances JoSQL can be faster!)

Caching

It is not uncommon for a Comparator (even using the effecient merge-sort implementation of java.util.Collections.sort(ListComparator) ) to perform thousands (even millions!) of comparisons.

However since JoSQL does not automatically cache the results of calls to functions and results of accessor accesses the performance of this kind of "dynamic" Comparator can quickly degrade. To mitigate this it is possible to turn "caching" on whereby the Comparator will "remember" the results of the functions on a per object basis and use those values instead of calling them again. This is not without it's downside however. Firstly since a reference to the object will be held it is important (if caching is used that you call: JoSQLComparator.clearCache() once the Comparator has been used to free up those references (it was considered using a java.util.WeakHashMap but that doesn't provide exactly what's needed here).

It is recommended that caching is turned on when the Comparator is to be used in a sort operation , i.e. calling: java.util.Collections.sort(ListComparator) or similar (however careful consideration needs to be given to the amount of memory that this may consume, i.e. 4 bytes = 1 object reference, plus 1 List, plus 4 bytes per order by "column" it soon adds up)

If the comparator is to be used in a java.util.TreeMap or java.util.TreeSet then caching should not be used since the values may (and perhaps should) change over time but due to caching the order won't change.




Constructor Summary
public  JoSQLComparator(String q)
     Init this filter with the query.
public  JoSQLComparator(Query q)
     Init this file filter with the query already built and parsed.

Method Summary
public  voidclearCache()
     Clear the cache, it is VITAL that you call this method before you use the comparator (if it has been used before) otherwise data objects will be "left around" and preventing the GC from cleaning them up.
public  intcompare(Object o1, Object o2)
     Compares the objects as according to the ORDER BY clause.
public  voiddoExecuteOn(List l)
     Execute the EXECUTE ON ALL expressions.
public  ExceptiongetException()
     The Comparator.compare(ObjectObject) method does not allow for any exceptions to be thrown however since the execution of the ORDER BY clause on the objects can cause the throwing of a QueryParseException it should be captured.
public  QuerygetQuery()
     Get the Query we are using to process objects.
public  booleanisCaching()
     Return whether this comparator uses caching to improve performance.
public  voidsetCaching(boolean b)
     Set whether the comparator should use caching to improve performance.
public  voidsetQuery(String q)
     Set a new Query (string form) for use in this filter.
public  voidsetQuery(Query q)
     Set a new Query object for use in this filter.


Constructor Detail
JoSQLComparator
public JoSQLComparator(String q) throws QueryParseException(Code)
Init this filter with the query.
Parameters:
  q - The query.
throws:
  QueryParseException - If there is an issue with the parsing of the query.



JoSQLComparator
public JoSQLComparator(Query q) throws IllegalStateException, QueryParseException(Code)
Init this file filter with the query already built and parsed.
Parameters:
  q - The query.
throws:
  IllegalStateException - If the Query object has not been parsed.
throws:
  QueryParseException - If the FROM class is not as expected.




Method Detail
clearCache
public void clearCache()(Code)
Clear the cache, it is VITAL that you call this method before you use the comparator (if it has been used before) otherwise data objects will be "left around" and preventing the GC from cleaning them up.



compare
public int compare(Object o1, Object o2)(Code)
Compares the objects as according to the ORDER BY clause.
Parameters:
  o1 - The first object.
Parameters:
  o2 - The second object.



doExecuteOn
public void doExecuteOn(List l) throws QueryExecutionException(Code)
Execute the EXECUTE ON ALL expressions.
Parameters:
  l - The list to execute the expressions on.



getException
public Exception getException()(Code)
The Comparator.compare(ObjectObject) method does not allow for any exceptions to be thrown however since the execution of the ORDER BY clause on the objects can cause the throwing of a QueryParseException it should be captured. If the exception is thrown then this method will return it. The exception thrown by the execution of the ORDER BY clause in JoSQLComparator.compare(Object,Object)or by sub-class/interface specific methods, this may be null if no exception was thrown.



getQuery
public Query getQuery()(Code)
Get the Query we are using to process objects. The Query.



isCaching
public boolean isCaching() throws IllegalStateException(Code)
Return whether this comparator uses caching to improve performance. true if caching is on.
throws:
  IllegalStateException - If the query has not yet been parsed or set.



setCaching
public void setCaching(boolean b) throws IllegalStateException(Code)
Set whether the comparator should use caching to improve performance.
Parameters:
  b - Set to true to turn caching on.
throws:
  IllegalStateException - If the query has not yet been parsed or set.



setQuery
public void setQuery(String q) throws QueryParseException(Code)
Set a new Query (string form) for use in this filter.
Parameters:
  q - The Query to use.
throws:
  QueryParseException - If there is an issue with the parsing of the query, or if the FROM class is not as expected.



setQuery
public void setQuery(Query q) throws IllegalStateException, QueryParseException(Code)
Set a new Query object for use in this filter.
Parameters:
  q - The Query to use.
throws:
  IllegalStateException - If the Query object has not been parsed.
throws:
  QueryParseException - If the FROM class is not as expected.



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.