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    * QueryResult.java
6    *
7    * Created on 3-Dec-2004 at 4:26:06 PM
8    */
9   package ca.uhn.cache.impl;
10  
11  import java.util.HashSet;
12  import java.util.Iterator;
13  import java.util.Set;
14  
15  import org.apache.commons.collections.IteratorUtils;
16  import org.apache.commons.collections.SetUtils;
17  
18  import ca.uhn.cache.IDataItem;
19  import ca.uhn.cache.IQueryResult;
20  
21  
22  
23  /***
24   * TODO complete javadoc for 
25   * TODO in future versions these might not be full when they are returned (although this isn't 
26   *   supported now).  At that time we will have to account for exceptions that arise during iteration, 
27   *   and also for add() called while iterating.  One solution would be to use a typed iterator that 
28   *   throws an exception on next(), if there has been an exception in the underlying data retrieval. 
29   *   Another is to use NoSuchElementException and/or getException() on this class.   
30   * 
31   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
32   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:39 $ by $Author: bryan_tripp $
33   */
34  public class QueryResult extends CommonsLangObject implements IQueryResult {
35      
36      private Set myItems;
37  
38      /***
39       * Constructs an empty query result.
40       */
41      public QueryResult() {
42          //myItems = SetUtils.typedSet( new HashSet(), IDataItem.class );
43          myItems = new HashSet();
44      }
45      
46      /***
47       * Constructs query result with the specified data items.
48       * 
49       * @param theItems The data items.
50       *  
51       * @throws ClassCastException If any of the elements of theItems is not an <code>IDataItem</code>.  
52       */
53      public QueryResult( Set theItems ) {
54          assert theItems != null;
55          
56          myItems = SetUtils.typedSet( new HashSet( theItems.size() ), IDataItem.class );
57          myItems.addAll( theItems );
58      }
59  
60      /***
61       * @see ca.uhn.cache.IQueryResult#append(biz.mobiled.cache.IQueryResult)
62       */
63      public IQueryResult append(IQueryResult theQueryResult) {
64          IQueryResult retVal;
65          
66          Set newItems = 
67              SetUtils.typedSet( 
68                      new HashSet( size() + theQueryResult.size() ), 
69                      IDataItem.class );
70          
71          newItems.addAll( myItems );
72          newItems.addAll( IteratorUtils.toList( theQueryResult.iterator() ) );
73          
74          retVal = new QueryResult( newItems );
75          
76          return retVal;
77      }
78  
79      
80      /***
81       * @see ca.uhn.cache.IQueryResult#iterator()
82       */
83      public Iterator iterator() {
84          return myItems.iterator();
85      }
86  
87      /***
88       * @see ca.uhn.cache.IQueryResult#add(ca.uhn.cache.IDataItem)
89       */
90      public void add( IDataItem theItem ) {
91          myItems.add( theItem );
92      }
93  
94      /***
95       * @see ca.uhn.cache.IQueryResult#isEmpty()
96       */
97      public boolean isEmpty() {
98          return myItems.isEmpty();
99      }
100 
101     /***
102      * @see ca.uhn.cache.IQueryResult#size()
103      */
104     public int size() {
105         return myItems.size();
106     }
107     
108 }