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.HashSet;
10  import java.util.Set;
11  
12  import org.apache.commons.collections.CollectionUtils;
13  
14  import ca.uhn.cache.IDimension;
15  import ca.uhn.cache.IPointQueryParam;
16  import ca.uhn.cache.IQueryParam;
17  
18  /***
19   * A <code>IPointQueryParam</code> that selects data items with values along 
20   * a certain dimension exactly equal to a certain <code>String</code> value.  
21   * 
22   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
23   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a> 
24   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:32 $ by $Author: bryan_tripp $
25   */
26  public class StringParam extends AbstractQueryParam implements IPointQueryParam {
27  
28      private final String myValue;
29  
30      /***
31       * Creates a new <code>StringParam</code>.
32       * 
33       * @param theDimension The dimension where this parameter is difined.
34       * @param theValue The value of this parameter. 
35       */
36      public StringParam( IDimension theDimension, String theValue ) {
37          super( theDimension );
38          
39          assert theValue != null;
40          
41          myValue = theValue;
42      }
43  
44      /***
45       * {@inheritDoc}
46       */
47      public boolean intersects(IQueryParam theParam) {
48          boolean retVal = false;
49          
50          if (super.compatibleWith(theParam)) {
51              if (theParam instanceof StringParam) {
52                  StringParam stringParam = (StringParam) theParam;
53                  if ( getValue().equals( stringParam.getValue() ) ) {
54                      retVal = true;
55                  }
56              } else if (theParam instanceof StringSetParam) {
57                  StringSetParam thisVal 
58                      = new StringSetParam(this.getDimension(), new String[] {this.getValue()});
59                  StringSetParam otherVals = (StringSetParam) theParam;
60                  retVal = StringSetParam.intersects(thisVal, otherVals);
61              }
62          }
63          
64          return retVal;
65      }
66  
67      /***
68       * @return Returns the value.
69       */
70      public String getValue() {
71          return myValue;
72      }
73  
74      /***
75       * Strings are treated as unordered uncategories rather than as with alphabetical
76       * order.  
77       * 
78       * @param theParam must be a StringParam or StringSetParam
79       * @param theSaturationPoint ignored 
80       * 
81       * @return 0 if the given param is the same, or is contained in a given StringSetParam,
82       *      0.5 if it is not (0.5 is similar to the mean distance between random values in 
83       *      an ordered dimension)   
84       *    
85       * @see ca.uhn.cache.IQueryParam#getDistance(ca.uhn.cache.IQueryParam, ca.uhn.cache.IQueryParam)
86       */
87      public float getDistance(IQueryParam theParam, IQueryParam theSaturationPoint) {
88          if (!compatibleWith(theParam)) {
89              throw new IllegalArgumentException("Incompatible IQueryParam (different dimension" +
90                      "or incompatible type");
91          }
92          
93          float result = 0;
94          if ( !intersects(theParam) ) {
95              result = 0.5f;
96          }
97          
98          return result;
99      }
100 
101     /*** 
102      * @param theParam must be a StringParam or StringSetParam
103      * @return a StringSetParam containing this and the given value(s) 
104      * 
105      * @see ca.uhn.cache.IQueryParam#merge(ca.uhn.cache.IQueryParam)
106      */
107     public IQueryParam merge(IQueryParam theParam) {
108         if (!compatibleWith(theParam)) {
109             throw new IllegalArgumentException("Incompatible IQueryParam (different dimension" +
110                     "or incompatible type");
111         }
112         
113         Set all = new HashSet(CollectionUtils.union(StringSetParam.getValues(this), 
114                 StringSetParam.getValues(theParam)));
115         return new StringSetParam(this.getDimension(), all);
116     }
117     
118 }