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    * QueryResultUtil.java
6    *
7    * Created on 8-Dec-2004 at 4:18:04 PM
8    */
9   package ca.uhn.cache.util;
10  
11  import java.util.HashMap;
12  import java.util.Iterator;
13  import java.util.Map;
14  
15  import org.apache.commons.collections.IteratorUtils;
16  import org.apache.commons.collections.Predicate;
17  
18  import ca.uhn.cache.IDataItem;
19  import ca.uhn.cache.IQuery;
20  import ca.uhn.cache.IQueryParam;
21  import ca.uhn.cache.IQueryResult;
22  import ca.uhn.cache.impl.QueryResult;
23  
24  
25  /***
26   * Provides utility methods for {@link ca.uhn.cache.IQueryResult} instances.
27   * 
28   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
29   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:49 $ by $Author: bryan_tripp $
30   */
31  public final class QueryResultUtil {
32  
33      /***
34       */
35      private QueryResultUtil() {
36      }
37      
38      /***
39       * Filter the query result by applying a query to each element. 
40       * 
41       * @param theQueryResult The query result.
42       * @param theQuery The query.
43       * @return The query result matching the query.
44       * 
45       * @precondition theQueryResult != null
46       * @precondition theQuery != null
47       * 
48       * @postcondition return != null
49       */
50      public static final IQueryResult filter( IQueryResult theQueryResult, IQuery theQuery ) {
51          IQueryResult retVal = new QueryResult();
52          
53          Iterator filteredIterator = 
54              IteratorUtils.filteredIterator( theQueryResult.iterator(), new MatchQueryPredicate( theQuery ) );
55          
56          for (; filteredIterator.hasNext(); ) {
57              IDataItem dataItem = (IDataItem) filteredIterator.next();
58              retVal.add(dataItem);
59          }
60          
61          return retVal;
62      }
63  
64      ///////////// HELPERS ///////////////
65      
66      /***
67       * A predicate indicating whether an IDataItem falls within an IQuery. 
68       *  
69       * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
70       * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:49 $ by $Author: bryan_tripp $
71       */
72      private static class MatchQueryPredicate implements Predicate {
73  
74          private Map myQueryBoundaries;
75          
76          public MatchQueryPredicate(IQuery theQuery) {
77              myQueryBoundaries = new HashMap(10);
78              Iterator params = theQuery.getParameters().iterator();
79              while (params.hasNext()) {
80                  IQueryParam param = (IQueryParam) params.next();
81                  myQueryBoundaries.put(param.getDimension(), param);
82              }
83          }
84          
85          /*** 
86           * @param theItem must be a IDataItem
87           * @return true iff the data item falls within our query boundaries in each 
88           *      dimension 
89           *    
90           * @see org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
91           */
92          public boolean evaluate(Object theItem) {
93              boolean result = true;
94              
95              if ( !(theItem instanceof IDataItem) ) {
96                  throw new IllegalArgumentException("Expected an IDataItem");
97              }            
98              IDataItem item = (IDataItem) theItem;
99              
100             Iterator itemParams = item.getProjection().getParameters().iterator();
101             paramLoop : while (itemParams.hasNext()) {
102                 IQueryParam itemParam = (IQueryParam) itemParams.next(); 
103                 IQueryParam queryParam 
104                     = (IQueryParam) myQueryBoundaries.get(itemParam.getDimension());
105 
106                 if ( !itemParam.intersects(queryParam) ) {
107                     result = false;
108                     break paramLoop;
109                 }
110             }
111             
112             return result;
113         }
114         
115     }
116 
117 
118 }