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    * Created on 6-Dec-2004
6    */
7   package ca.uhn.cache.impl;
8   
9   import java.util.Arrays;
10  import java.util.Collection;
11  import java.util.HashSet;
12  import java.util.Set;
13  
14  import org.apache.commons.collections.CollectionUtils;
15  import org.apache.commons.collections.SetUtils;
16  
17  import ca.uhn.cache.IDimension;
18  import ca.uhn.cache.IGroupQueryParam;
19  import ca.uhn.cache.IQueryParam;
20  
21  /***
22   * A <code>IQueryParam</code> that selects data items with values along 
23   * a certain dimension exactly equal to one of a set of String values.  
24   * 
25   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
26   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:32 $ by $Author: bryan_tripp $
27   */
28  public class StringSetParam extends AbstractQueryParam implements IGroupQueryParam {
29      
30      private final Set myValues;
31  
32      /***
33       * Creates a new <code>StringSetParam</code>.
34       * 
35       * @param theDimension The dimension where this parameter is difined.
36       * @param theValues The set of String values.
37       */
38      public StringSetParam( IDimension theDimension, Set theValues ) {
39          super( theDimension );
40          assert theValues != null;
41          
42          myValues = new HashSet( theValues );
43      }
44      
45      /***
46       * Creates a new <code>StringSetParam</code>.
47       * 
48       * @param theDimension The dimension where this parameter is difined.
49       * @param theValues The set of String values as a String[].
50       */
51      public StringSetParam( IDimension theDimension, String[] theValues ) {
52          super( theDimension );
53          assert theValues != null;
54          
55          myValues = new HashSet( Arrays.asList( theValues ) );
56      }
57      
58  
59      /***
60       * {@inheritDoc}
61       */
62      public boolean intersects(IQueryParam theParam) {
63          boolean retVal = false;
64          
65          if (super.compatibleWith(theParam)) {
66              StringSetParam ssp = null;
67              if (theParam instanceof StringParam) {
68                  String val = ((StringParam) theParam).getValue();
69                  ssp = new StringSetParam(this.getDimension(), new String[] {val});
70              } else if (theParam instanceof StringSetParam) {
71                  ssp = (StringSetParam) theParam;
72              }
73              
74              retVal = intersects(this, ssp);            
75          }
76          
77          return retVal;
78      }
79      
80      /***
81       * @return StringParam.class
82       */
83      public Class getPointParamClass() {
84          return StringParam.class;
85      }
86      
87      /***
88       * @param theOne a StringSetParam
89       * @param theOther a StringSetParam
90       * @return true iff any value is shared by both, or they are both empty 
91       */
92      protected static boolean intersects(StringSetParam theOne, StringSetParam theOther) {
93          boolean retVal = false;
94         
95          if ( theOne.getValues().isEmpty() && theOther.getValues().isEmpty() ) {
96              retVal = true;
97          }
98          else {
99              Collection intersection = CollectionUtils.intersection( theOne.getValues(), theOther.getValues() );
100             
101             if ( !intersection.isEmpty() ) {
102                 retVal = true;
103             }
104         }
105         
106         return retVal;
107     }
108     
109     /***
110      * @param theParam a StringParam or StringSetParam
111      * @return its value(s) 
112      */
113     protected static Set getValues(IQueryParam theParam) {
114         Set result = null;
115         if (theParam instanceof StringParam) {
116             result = new HashSet();
117             result.add( ((StringParam) theParam).getValue() );
118         } else if (theParam instanceof StringSetParam) {
119             result = ((StringSetParam) theParam).getValues();
120         } else {
121             throw new IllegalArgumentException("Expected a StringParam or StringSetParam");
122         }
123         
124         return result;
125     }
126     
127     /***
128      * @return Returns the values.
129      */
130     public Set getValues() {
131         return SetUtils.unmodifiableSet( myValues );
132     }
133     
134     /***
135      * Strings are treated as unordered uncategories rather than as with alphabetical
136      * order.  
137      * 
138      * @param theParam must be a StringParam or StringSetParam
139      * @param theSaturationPoint ignored 
140      * 
141      * @return 0 if the given param intersects this one, or 0.5 if it doesn't 
142      *      (0.5 is similar to the mean distance between random values in an 
143      *      ordered dimension)   
144      *    
145      * @see ca.uhn.cache.IQueryParam#getDistance(ca.uhn.cache.IQueryParam, ca.uhn.cache.IQueryParam)
146      */
147     public float getDistance(IQueryParam theParam, IQueryParam theSaturationPoint) {
148         if (!compatibleWith(theParam)) {
149             throw new IllegalArgumentException("Incompatible IQueryParam (different dimension" +
150                     "or incompatible type");
151         }
152         
153         float result = 0;
154         if ( !intersects(theParam) ) {
155             result = 0.5f;
156         }
157         
158         return result;
159     }
160 
161     /*** 
162      * @param theParam must be a StringParam or StringSetParam
163      * @return a StringSetParam containing this and the given value(s) 
164      * 
165      * @see ca.uhn.cache.IQueryParam#merge(ca.uhn.cache.IQueryParam)
166      */
167     public IQueryParam merge(IQueryParam theParam) {
168         if (!compatibleWith(theParam)) {
169             throw new IllegalArgumentException("Incompatible IQueryParam (different dimension" +
170                     "or incompatible type");
171         }
172         
173         Set all = new HashSet(CollectionUtils.union(StringSetParam.getValues(this), 
174                 StringSetParam.getValues(theParam)));
175         return new StringSetParam(this.getDimension(), all);
176     }
177 
178 }