KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > store > impl > JispKey


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.store.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInput JavaDoc;
21 import java.io.ObjectOutput JavaDoc;
22
23 import com.coyotegulch.jisp.KeyObject;
24
25 /**
26  * Wrapper class for Keys to be compatible with the
27  * Jisp KeyObject.
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  * @version CVS $Id: JispKey.java,v 1.4 2004/02/28 11:47:31 cziegeler Exp $
31  */

32 public final class JispKey extends KeyObject
33 {
34     final static long serialVersionUID = -1216913992804571313L;
35
36     protected Object JavaDoc m_Key;
37
38     static protected JispKey NULL_KEY = new JispKey("");
39     
40     public JispKey() {
41         this("");
42     }
43     
44     /**
45      * Constructor for the JispKey object
46      *
47      * @param keyValue the key
48      */

49     public JispKey(Object JavaDoc keyValue)
50     {
51         m_Key = keyValue;
52     }
53
54     /**
55      * Compares two Keys
56      *
57      * @param key the KeyObject to be compared
58      * @return 0 if equal, 1 if greater, -1 if less
59      */

60
61     public int compareTo(KeyObject key)
62     {
63         if (key instanceof JispKey)
64         {
65             final JispKey other = (JispKey)key;
66             if ( other.m_Key.hashCode() == m_Key.hashCode() )
67             {
68                 if ( m_Key == other.m_Key || m_Key.equals(other.m_Key) )
69                 {
70                     return KEY_EQUAL;
71                 }
72                 // we have the same hashcode, but different keys
73
// this is usually an error condition, but we deal
74
// with it anway
75
// if they would have the same classname, they
76
// can only have the same hashCode if they are equal:
77
int comp = m_Key.getClass().getName().compareTo(other.m_Key.getClass().getName());
78                 if ( comp < 0 )
79                 {
80                     return KEY_LESS;
81                 }
82                 return KEY_MORE;
83             }
84             else
85             {
86                 if ( m_Key.hashCode() < other.m_Key.hashCode() )
87                 {
88                     return KEY_LESS;
89                 }
90                 return KEY_MORE;
91             }
92         }
93         else
94         {
95             return KEY_ERROR;
96         }
97     }
98
99     /**
100      * Composes a null Kewy
101      *
102      * @return a null Key
103      */

104     public KeyObject makeNullKey()
105     {
106         return NULL_KEY;
107     }
108
109     /**
110      * The object implements the writeExternal method to save its contents
111      * by calling the methods of DataOutput for its primitive values or
112      * calling the writeObject method of ObjectOutput for objects, strings,
113      * and arrays.
114      *
115      * @param out the stream to write the object to
116      * @exception IOException
117      */

118     public void writeExternal(ObjectOutput JavaDoc out)
119     throws IOException JavaDoc
120     {
121         out.writeObject(m_Key);
122     }
123
124     /**
125      * The object implements the readExternal method to restore its contents
126      * by calling the methods of DataInput for primitive types and readObject
127      * for objects, strings and arrays. The readExternal method must read the
128      * values in the same sequence and with the same types as were written by writeExternal.
129      *
130      * @param in the stream to read data from in order to restore the object
131      * @exception IOException
132      * @exception ClassNotFoundException
133      */

134     public void readExternal(ObjectInput JavaDoc in)
135     throws IOException JavaDoc, ClassNotFoundException JavaDoc
136     {
137         m_Key = in.readObject();
138     }
139
140     /**
141      * Return the real key
142      */

143     public Object JavaDoc getKey()
144     {
145         return m_Key;
146     }
147 }
148
Popular Tags