Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.collection.internal;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.loader.CollectionAliases;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.type.Type;
/**
* A persistent wrapper for a java.util.Map. Underlying collection
* is a HashMap.
*
* @see java.util.HashMap
* @author Gavin King
*/
public class PersistentMap extends AbstractPersistentCollection implements Map {
protected Map map;
/**
* Empty constructor.
*
* Note: this form is not ever ever ever used by Hibernate; it is, however,
* needed for SOAP libraries and other such marshalling code.
*/
public PersistentMap() {
// intentionally empty
}
/**
* Instantiates a lazy map (the underlying map is un-initialized).
*
* @param session The session to which this map will belong.
*/
public PersistentMap(SharedSessionContractImplementor session) {
super( session );
}
/**
* Instantiates a lazy map (the underlying map is un-initialized).
*
* @param session The session to which this map will belong.
* @deprecated {@link #PersistentMap(SharedSessionContractImplementor)} should be used instead.
*/
@Deprecated
public PersistentMap(SessionImplementor session) {
this( (SharedSessionContractImplementor) session );
}
/**
* Instantiates a non-lazy map (the underlying map is constructed
* from the incoming map reference).
*
* @param session The session to which this map will belong.
* @param map The underlying map data.
*/
public PersistentMap(SharedSessionContractImplementor session, Map map) {
super( session );
this.map = map;
setInitialized();
setDirectlyAccessible( true );
}
/**
* Instantiates a non-lazy map (the underlying map is constructed
* from the incoming map reference).
*
* @param session The session to which this map will belong.
* @param map The underlying map data.
* @deprecated {@link #PersistentMap(SharedSessionContractImplementor, Map)} should be used instead.
*/
@Deprecated
public PersistentMap(SessionImplementor session, Map map) {
this( (SharedSessionContractImplementor) session, map );
}
@Override
@SuppressWarnings( {"unchecked"})
public Serializable getSnapshot(CollectionPersister persister) throws HibernateException {
final HashMap clonedMap = new HashMap( map.size() );
for ( Object o : map.entrySet() ) {
final Entry e = (Entry) o;
final Object copy = persister.getElementType().deepCopy( e.getValue(), persister.getFactory() );
clonedMap.put( e.getKey(), copy );
}
return clonedMap;
}
@Override
public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException {
final Map sn = (Map) snapshot;
return getOrphans( sn.values(), map.values(), entityName, getSession() );
}
@Override
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
final Type elementType = persister.getElementType();
final Map snapshotMap = (Map) getSnapshot();
if ( snapshotMap.size() != this.map.size() ) {
return false;
}
for ( Object o : map.entrySet() ) {
final Entry entry = (Entry) o;
if ( elementType.isDirty( entry.getValue(), snapshotMap.get( entry.getKey() ), getSession() ) ) {
return false;
}
}
return true;
}
@Override
public boolean isSnapshotEmpty(Serializable snapshot) {
return ( (Map) snapshot ).isEmpty();
}
@Override
public boolean isWrapper(Object collection) {
return map==collection;
}
@Override
public void beforeInitialize(CollectionPersister persister, int anticipatedSize) {
this.map = (Map) persister.getCollectionType().instantiate( anticipatedSize );
}
@Override
public int size() {
return readSize() ? getCachedSize() : map.size();
}
@Override
public boolean isEmpty() {
return readSize() ? getCachedSize()==0 : map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
final Boolean exists = readIndexExistence( key );
return exists == null ? map.containsKey( key ) : exists;
}
@Override
public boolean containsValue(Object value) {
final Boolean exists = readElementExistence( value );
return exists == null
? map.containsValue( value )
: exists;
}
@Override
public Object get(Object key) {
final Object result = readElementByIndex( key );
return result == UNKNOWN
? map.get( key )
: result;
}
@Override
@SuppressWarnings("unchecked")
public Object put(Object key, Object value) {
if ( isPutQueueEnabled() ) {
final Object old = readElementByIndex( key );
if ( old != UNKNOWN ) {
queueOperation( new Put( key, value, old ) );
return old;
}
}
initialize( true );
final Object old = map.put( key, value );
// would be better to use the element-type to determine
// whether the old and the new are equal here; the problem being
// we do not necessarily have access to the element type in all
// cases
if ( value != old ) {
dirty();
}
return old;
}
@Override
@SuppressWarnings("unchecked")
public Object remove(Object key) {
if ( isPutQueueEnabled() ) {
final Object old = readElementByIndex( key );
if ( old != UNKNOWN ) {
elementRemoved = true;
queueOperation( new Remove( key, old ) );
return old;
}
}
// TODO : safe to interpret "map.remove(key) == null" as non-dirty?
initialize( true );
if ( map.containsKey( key ) ) {
elementRemoved = true;
dirty();
}
return map.remove( key );
}
@Override
@SuppressWarnings("unchecked")
public void putAll(Map puts) {
if ( puts.size() > 0 ) {
initialize( true );
for ( Object o : puts.entrySet() ) {
final Entry entry = (Entry) o;
put( entry.getKey(), entry.getValue() );
}
}
}
@Override
@SuppressWarnings("unchecked")
public void clear() {
if ( isClearQueueEnabled() ) {
queueOperation( new Clear() );
}
else {
initialize( true );
if ( ! map.isEmpty() ) {
dirty();
map.clear();
}
}
}
@Override
@SuppressWarnings("unchecked")
public Set keySet() {
read();
return new SetProxy( map.keySet() );
}
@Override
@SuppressWarnings("unchecked")
public Collection values() {
read();
return new SetProxy( map.values() );
}
@Override
@SuppressWarnings("unchecked")
public Set entrySet() {
read();
return new EntrySetProxy( map.entrySet() );
}
@Override
@SuppressWarnings("unchecked")
public boolean empty() {
return map.isEmpty();
}
@Override
@SuppressWarnings("unchecked")
public String toString() {
read();
return map.toString();
}
private transient List