View Javadoc

1   /*
2    * Copyright 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Query.java
6    *
7    * Created on 7-Dec-2004 at 12:13:00 PM
8    */
9   package ca.uhn.cache.impl;
10  
11  import java.io.Serializable;
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.Map;
15  import java.util.Set;
16  import java.util.SortedSet;
17  import java.util.TreeSet;
18  
19  import org.apache.commons.collections.SetUtils;
20  
21  import ca.uhn.cache.IDimension;
22  import ca.uhn.cache.IQuery;
23  import ca.uhn.cache.IQueryParam;
24  
25  /***
26   * Generic implementation of the <code>IQuery</code> interface. 
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:52:32 $ by $Author: bryan_tripp $
30   */
31  public class Query extends CommonsLangObject implements IQuery, Serializable {
32      
33      private final SortedSet myQueryParams;
34      private final Map myDimToQueryParamMap;
35  
36      /***
37       * Creates an empty query.
38       */
39      public Query() {
40          myQueryParams = new TreeSet();
41          myDimToQueryParamMap = new HashMap();
42      }
43  
44      /***
45       * Creates a query with the provided parameters.
46       * 
47       * @param theParams The query parameters.
48       */
49      public Query( IQueryParam[] theParams ) {
50          this();
51          for (int i = 0; i < theParams.length; i++) {
52              IQueryParam param = theParams[i];
53              addParameter( param );
54          }
55      }
56  
57      /***
58       * @see ca.uhn.cache.IQuery#addParameter(ca.uhn.cache.IQueryParam)
59       */
60      public void addParameter( IQueryParam theParam ) {
61          assert theParam != null;
62          assert myDimToQueryParamMap.get( theParam.getDimension() ) == null;
63          
64          myQueryParams.add( theParam );
65          myDimToQueryParamMap.put( theParam.getDimension(), theParam );
66          
67      }
68  
69      /***
70       * @see ca.uhn.cache.IQuery#getParameters()
71       */
72      public Set getParameters() {
73          return SetUtils.unmodifiableSet( myQueryParams );
74      }
75  
76      /***
77       * {@inheritDoc}
78       */
79      public boolean isEmpty() {
80          return myQueryParams.isEmpty();
81      }
82  
83      /***
84       * {@inheritDoc}
85       */
86      public IQueryParam getParamByDimension( IDimension theDimension ) {
87          Iterator it = myQueryParams.iterator();
88          while(it.hasNext()){
89              IQueryParam currParam = (IQueryParam)it.next();
90              if (currParam.getDimension().equals(theDimension)){
91                  return currParam;
92              }
93          }
94          return null;
95      }
96  
97  }