View Javadoc

1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 29-Dec-2004
6    */
7   package ca.uhn.cache.util;
8   
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  
12  import ca.uhn.cache.IParamSpace;
13  import ca.uhn.cache.IQuery;
14  import ca.uhn.cache.IQueryParam;
15  import ca.uhn.cache.impl.Query;
16  
17  /***
18   * Provides utility methods for {@link ca.uhn.cache.IQuery} instances.
19   * 
20   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
21   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:49 $ by $Author: bryan_tripp $
22   */
23  public class QueryUtil {
24  
25      /***
26       */
27      private QueryUtil() {
28      }
29      
30      /***
31       * @param theQuery a query to be divided into multiple queries according to chunk boundaries
32       * @param theParamSpace the parameter space that defines the chunk boundaries  
33       * @return a list of queries covering the same region as the original query, but respecting 
34       *      chunk boundaries 
35       */
36      public static final IQuery[] chunk(IQuery theQuery, IParamSpace theParamSpace) {
37          ArrayList chunks = new ArrayList();
38  
39          Iterator params = theQuery.getParameters().iterator();
40          IQueryParam[] firstDim = theParamSpace.chunk((IQueryParam) params.next());        
41          for (int i = 0; i < firstDim.length; i++) {
42              chunks.add(makeQuery(firstDim[i]));
43          }
44          
45          while (params.hasNext()) {
46              ArrayList newChunks = new ArrayList();
47              IQueryParam[] thisDim = theParamSpace.chunk((IQueryParam) params.next());
48              for (int i = 0; i < chunks.size(); i++) {
49                  for (int j = 0; j < thisDim.length; j++) {
50                      newChunks.add(combine((IQuery) chunks.get(i), thisDim[j]));
51                  }
52              }
53              chunks = newChunks;
54          }
55          
56          return (IQuery[]) chunks.toArray(new IQuery[chunks.size()]);
57      }
58      
59      private static IQuery combine(IQuery theQuery, IQueryParam theParam) {
60          IQuery result = new Query();
61          
62          Iterator it = theQuery.getParameters().iterator();
63          while (it.hasNext()) {
64              result.addParameter((IQueryParam) it.next());            
65          }
66          
67          result.addParameter(theParam);
68          
69          return result;
70      }
71      
72      private static IQuery makeQuery(IQueryParam theParam) {
73          IQuery result = new Query();
74          result.addParameter(theParam);
75          return result;
76      }
77      
78  }