KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > FilterKey


1 //$Id: FilterKey.java,v 1.3 2005/06/02 22:53:32 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.hibernate.EntityMode;
12 import org.hibernate.engine.TypedValue;
13 import org.hibernate.impl.FilterImpl;
14 import org.hibernate.type.Type;
15
16 /**
17  * Allows cached queries to be keyed by enabled filters.
18  *
19  * @author Gavin King
20  */

21 public final class FilterKey implements Serializable JavaDoc {
22     private String JavaDoc filterName;
23     private Map JavaDoc filterParameters = new HashMap JavaDoc();
24     
25     public FilterKey(String JavaDoc name, Map JavaDoc params, Map JavaDoc types, EntityMode entityMode) {
26         filterName = name;
27         Iterator JavaDoc iter = params.entrySet().iterator();
28         while ( iter.hasNext() ) {
29             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
30             Type type = (Type) types.get( me.getKey() );
31             filterParameters.put( me.getKey(), new TypedValue( type, me.getValue(), entityMode ) );
32         }
33     }
34     
35     public int hashCode() {
36         int result = 13;
37         result = 37 * result + filterName.hashCode();
38         result = 37 * result + filterParameters.hashCode();
39         return result;
40     }
41     
42     public boolean equals(Object JavaDoc other) {
43         if ( !(other instanceof FilterKey) ) return false;
44         FilterKey that = (FilterKey) other;
45         if ( !that.filterName.equals(filterName) ) return false;
46         if ( !that.filterParameters.equals(filterParameters) ) return false;
47         return true;
48     }
49     
50     public String JavaDoc toString() {
51         return "FilterKey[" + filterName + filterParameters + ']';
52     }
53     
54     public static Set JavaDoc createFilterKeys(Map JavaDoc enabledFilters, EntityMode entityMode) {
55         if ( enabledFilters.size()==0 ) return null;
56         Set JavaDoc result = new HashSet JavaDoc();
57         Iterator JavaDoc iter = enabledFilters.values().iterator();
58         while ( iter.hasNext() ) {
59             FilterImpl filter = (FilterImpl) iter.next();
60             FilterKey key = new FilterKey(
61                     filter.getName(),
62                     filter.getParameters(),
63                     filter.getParameterTypes(),
64                     entityMode
65                 );
66             result.add(key);
67         }
68         return result;
69     }
70 }
71
Popular Tags