KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > entry > StructuredMapCacheEntry


1 //$Id: StructuredMapCacheEntry.java,v 1.1 2005/02/13 12:46:58 oneovthafew Exp $
2
package org.hibernate.cache.entry;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.hibernate.engine.SessionFactoryImplementor;
10
11 /**
12  * @author Gavin King
13  */

14 public class StructuredMapCacheEntry implements CacheEntryStructure {
15
16     public Object JavaDoc structure(Object JavaDoc item) {
17         CollectionCacheEntry entry = (CollectionCacheEntry) item;
18         Serializable JavaDoc[] state = entry.getState();
19         Map JavaDoc map = new HashMap JavaDoc(state.length);
20         for ( int i=0; i<state.length; ) {
21             map.put( state[i++], state[i++] );
22         }
23         return map;
24     }
25     
26     public Object JavaDoc destructure(Object JavaDoc item, SessionFactoryImplementor factory) {
27         Map JavaDoc map = (Map JavaDoc) item;
28         Serializable JavaDoc[] state = new Serializable JavaDoc[ map.size()*2 ];
29         int i=0;
30         Iterator JavaDoc iter = map.entrySet().iterator();
31         while ( iter.hasNext() ) {
32             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
33             state[i++] = (Serializable JavaDoc) me.getKey();
34             state[i++] = (Serializable JavaDoc) me.getValue();
35         }
36         return new CollectionCacheEntry(state);
37     }
38
39 }
40
Popular Tags