package org.hibernate.cache.spi.entry;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.entity.EntityPersister;
public class StructuredCacheEntry implements CacheEntryStructure {
public static final String SUBCLASS_KEY = "_subclass";
public static final String VERSION_KEY = "_version";
private EntityPersister persister;
public StructuredCacheEntry(EntityPersister persister) {
this.persister = persister;
}
@Override
@SuppressWarnings("unchecked")
public Object destructure(Object structured, SessionFactoryImplementor factory) {
final Map map = (Map) structured;
final String subclass = (String) map.get( SUBCLASS_KEY );
final Object version = map.get( VERSION_KEY );
final EntityPersister subclassPersister = factory.getEntityPersister( subclass );
final String[] names = subclassPersister.getPropertyNames();
final Serializable[] disassembledState = new Serializable[names.length];
for ( int i = 0; i < names.length; i++ ) {
disassembledState[i] = (Serializable) map.get( names[i] );
}
return new StandardCacheEntryImpl(
disassembledState,
subclass,
version
);
}
@Override
@SuppressWarnings("unchecked")
public Object structure(Object item) {
final CacheEntry entry = (CacheEntry) item;
final String[] names = persister.getPropertyNames();
final Map map = new HashMap( names.length + 3, 1f );
map.put( SUBCLASS_KEY, entry.getSubclass() );
map.put( VERSION_KEY, entry.getVersion() );
for ( int i=0; i<names.length; i++ ) {
map.put( names[i], entry.getDisassembledState()[i] );
}
return map;
}
}