JQL is an extension for
Java that provides support for querying collections of objects. These queries can be run over objects in collections in your program, or even used to check expressions on all instances of specific types at runtime.
Queries provide a powerful abstraction for dealing with sets of objects, allowing the query engine to take care of the implementation details. This allows for shorter, clearer code, and permits the query engine to dynamically optimize query evaluation strategies as the runtime context changes.
Queries can also be cached and that cache incrementally maintained - this greatly increases their efficiency, and can offer improved performance for many common collection operations.
A brief example!
Say we're building a crossword puzzle. We've got a list of candidate words for our puzzle, and a list of the lengths of the gaps we need to fill:
ArrayList<String> words = dict.getWords(Puzzle.MEDIUM);
ArrayList<Integer> gaplengths = puzzle.getGapLengths();
Now we've got a truly marvelous algorithm for building a crossword puzzle (that this webpage is too narrow to contain), which relies on having a list of pairs of [length, word].
Using a JQL query, we can build this list with ease:
List<Object[]> matches = selectAll(String w : words,
Integer i : gaplengths |
w.length() == i);
Contrast with how we could do this in normal
Java:
List<Object[]> matches = new ArrayList<Object[]>();
for(String w : words){
for(Integer i : gaplengths){
if(w.length() == i)
matches.add(new Object[i,w]);
}
}
The JQL query and the
Java code will produce the same list of results - but the JQL version is shorter, neater, and abstracts away the exact method of finding the matches. For a more in-depth look on why this can be helpful, please look at our
Examples page.
下载地址:http://homepages.mcs.vuw.ac.nz/~djp/jql/download.html
posted on 2009-08-06 09:11
周锐 阅读(289)
评论(0) 编辑 收藏 所属分类:
Java