KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > cache > adapter > NameSpaceKeyWrapper


1 /*
2  * Created on 11-May-2005
3  *
4  */

5 package com.jofti.cache.adapter;
6
7
8 import com.jofti.api.NameSpaceKey;
9
10 /**
11  *
12  *
13  *Used in the JBossCacheAdapter index. The wrapper provides comparableness and ordering for
14  *the JBoss Fqn key. Specifically, the namespace object is checked first. If they are of the same type and equal then the key is checked.
15  * Otherwise if both objects are of the same type and comparable then natual ordering occurs on these objects. However, if they are not the same type
16  * then ordering of the toString() method is taken (while this may not provide consistency
17  * across time, we do not care as this is reasonable to provide key ordering in the index - which is not visible to the caller).
18  * <p>
19  * Key Comparison is done in a similar manner.<p>
20  *
21  * @author Steve Woodcock
22  */

23 public class NameSpaceKeyWrapper implements Comparable JavaDoc, NameSpaceKey {
24
25     
26     
27     /**
28      * @return Returns the key.
29      */

30     public Object JavaDoc getKey() {
31         return key;
32     }
33
34     private static String JavaDoc KEY_DEFAULT = "KEY";
35     
36     protected Object JavaDoc nameSpace = null;
37     
38     protected Class JavaDoc nameSpaceClass;
39     
40     protected Object JavaDoc key =null;
41     protected Class JavaDoc keyClass = null;
42     int hashCode =0;
43     
44     private String JavaDoc nameSpaceCompareString = null;
45     
46     /**
47      * Constructs a wrapper for a namespace and key object. Null keys are given the String
48      * value "KEY".
49      * @param nameSpace
50      * @param key
51      */

52     public NameSpaceKeyWrapper(Object JavaDoc nameSpace, Object JavaDoc key){
53         this.nameSpace = nameSpace;
54         nameSpaceClass = this.nameSpace.getClass();
55         if (key != null){
56             this.key = key;
57             keyClass = key.getClass();
58         } else{
59             key = KEY_DEFAULT;
60             keyClass = this.key.getClass();
61         }
62         this.nameSpaceCompareString = nameSpace.toString() + "."+key;
63         hashCode =nameSpace.hashCode() + key.hashCode();
64     }
65     
66     public NameSpaceKeyWrapper(Object JavaDoc nameSpace){
67         this(nameSpace, KEY_DEFAULT);
68     }
69     
70     
71     public int compareTo(Object JavaDoc o) {
72         
73         
74         try{
75             
76             NameSpaceKeyWrapper temp = (NameSpaceKeyWrapper)o;
77
78             
79             // if the namespaces are the same type & comparable
80
if (nameSpaceClass.equals(temp.nameSpaceClass)
81                     && nameSpace.equals(temp.nameSpace))
82             {
83                 // the name spaces are equal
84
// compare the keys if they are comparable
85
if (keyClass.equals(temp.keyClass)
86                             && key instanceof Comparable JavaDoc){
87                         return ((Comparable JavaDoc)key).compareTo(temp.key);
88                     }
89                     // otherwise we have mismatched key types - so devolve to String
90
return key.toString().compareTo(temp.key.toString());
91
92             }else{
93                 // do a compare to on String
94
if (nameSpaceClass.equals(temp.nameSpaceClass)
95                         && nameSpace instanceof Comparable JavaDoc)
96                 {
97                     return ((Comparable JavaDoc)nameSpace).compareTo((Comparable JavaDoc)temp.nameSpace);
98                 }else{
99                     return nameSpace.toString().compareTo(temp.nameSpace.toString());
100                 }
101                 
102             }
103         } catch (Throwable JavaDoc t){
104             
105         }
106             return -1;
107     }
108     
109     
110     
111     public boolean equals(Object JavaDoc o){
112         try {
113             NameSpaceKeyWrapper temp = (NameSpaceKeyWrapper)o;
114             // see if name spaces equal
115
if (nameSpaceClass.equals(temp.nameSpaceClass)){
116                 // name spaces are equal
117
if (nameSpace.equals(temp.nameSpace)){
118                     // see if keys are equals
119
if (keyClass.equals(temp.keyClass)){
120                         return key.equals(temp.key);
121                     }
122                 }
123             }
124         } catch (Exception JavaDoc e){
125             
126         }
127         return false;
128     }
129     
130     public int hashCode(){
131         return hashCode;
132     }
133     
134     public String JavaDoc toString(){
135         return nameSpaceCompareString;
136     }
137     /* (non-Javadoc)
138      * @see com.jofti.core.NameSpaceKey#getNameSpace()
139      */

140     public Object JavaDoc getNameSpace() {
141         // TODO Auto-generated method stub
142
return nameSpace;
143     }
144     
145     protected void setNameSpace(Object JavaDoc nameSpace){
146         this.nameSpace = nameSpace;
147         this.nameSpaceClass = nameSpace.getClass();
148         this.nameSpaceCompareString = nameSpace.toString() + "."+key;
149     }
150 }
151
Popular Tags