Open Source Repository

Home /hibernate/hibernate-3.2.7.ga | Repository Home



org/hibernate/collection/PersistentMap.java
//$Id: PersistentMap.java 11256 2007-03-07 19:32:58Z [email protected] $
package org.hibernate.collection;

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.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.loader.CollectionAliases;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.type.Type;


/**
 * A persistent wrapper for a <tt>java.util.Map</tt>. Underlying collection
 * is a <tt>HashMap</tt>.
 *
 @see java.util.HashMap
 @author Gavin King
 */
public class PersistentMap extends AbstractPersistentCollection implements Map {

  protected Map map;

  /**
   * Empty constructor.
   <p/>
   * 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(SessionImplementor session) {
    super(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(SessionImplementor session, Map map) {
    super(session);
    this.map = map;
    setInitialized();
    setDirectlyAccessible(true);
  }

  public Serializable getSnapshot(CollectionPersister persisterthrows HibernateException {
    EntityMode entityMode = getSession().getEntityMode();
    HashMap clonedMap = new HashMapmap.size() );
    Iterator iter = map.entrySet().iterator();
    while iter.hasNext() ) {
      Map.Entry e = (Map.Entryiter.next();
      final Object copy = persister.getElementType()
        .deepCopye.getValue(), entityMode, persister.getFactory() );
      clonedMap.pute.getKey(), copy );
    }
    return clonedMap;
  }

  public Collection getOrphans(Serializable snapshot, String entityNamethrows HibernateException {
    Map sn = (Mapsnapshot;
    return getOrphanssn.values(), map.values(), entityName, getSession() );
  }

  public boolean equalsSnapshot(CollectionPersister persisterthrows HibernateException {
    Type elementType = persister.getElementType();
    Map xmap = (MapgetSnapshot();
    if xmap.size()!=this.map.size() ) return false;
    Iterator iter = map.entrySet().iterator();
    while iter.hasNext() ) {
      Map.Entry entry = (Map.Entryiter.next();
      if elementType.isDirtyentry.getValue(), xmap.getentry.getKey() ), getSession() ) ) return false;
    }
    return true;
  }

  public boolean isSnapshotEmpty(Serializable snapshot) {
    return ( (Mapsnapshot ).isEmpty();
  }

  public boolean isWrapper(Object collection) {
    return map==collection;
  }

  public void beforeInitialize(CollectionPersister persister, int anticipatedSize) {
    this.map = Map persister.getCollectionType().instantiateanticipatedSize );
  }


  /**
   @see java.util.Map#size()
   */
  public int size() {
    return readSize() ? getCachedSize() : map.size();
  }

  /**
   @see java.util.Map#isEmpty()
   */
  public boolean isEmpty() {
    return readSize() ? getCachedSize()==: map.isEmpty();
  }

  /**
   @see java.util.Map#containsKey(Object)
   */
  public boolean containsKey(Object key) {
    Boolean exists = readIndexExistence(key);
    return exists==null ? map.containsKey(key: exists.booleanValue();
  }

  /**
   @see java.util.Map#containsValue(Object)
   */
  public boolean containsValue(Object value) {
    Boolean exists = readElementExistence(value);
    return exists==null 
        map.containsValue(value
        exists.booleanValue();
  }

  /**
   @see java.util.Map#get(Object)
   */
  public Object get(Object key) {
    Object result = readElementByIndex(key);
    return result==UNKNOWN ? map.get(key: result;
  }

  /**
   @see java.util.Map#put(Object, Object)
   */
  public Object put(Object key, Object value) {
    if isPutQueueEnabled() ) {
      Object old = readElementByIndexkey );
      if old != UNKNOWN ) {
        queueOperationnew Putkey, value, old ) );
        return old;
      }
    }
    initializetrue );
    Object old = map.putkey, 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;
  }

  /**
   @see java.util.Map#remove(Object)
   */
  public Object remove(Object key) {
    if isPutQueueEnabled() ) {
      Object old = readElementByIndexkey );
      queueOperationnew Removekey, old ) );
      return old;
    }
    else {
      // TODO : safe to interpret "map.remove(key) == null" as non-dirty?
      initializetrue );
      if map.containsKeykey ) ) {
        dirty();
      }
      return map.removekey );
    }
  }

  /**
   @see java.util.Map#putAll(java.util.Map puts)
   */
  public void putAll(Map puts) {
    if puts.size()>) {
      initializetrue );
      Iterator itr = puts.entrySet().iterator();
      while itr.hasNext() ) {
        Map.Entry entry = Entry itr.next();
        putentry.getKey(), entry.getValue() );
      }
    }
  }

  /**
   @see java.util.Map#clear()
   */
  public void clear() {
    if isClearQueueEnabled() ) {
      queueOperationnew Clear() );
    }
    else {
      initializetrue );
      if ! map.isEmpty() ) {
        dirty();
        map.clear();
      }
    }
  }

  /**
   @see java.util.Map#keySet()
   */
  public Set keySet() {
    read();
    return new SetProxymap.keySet() );
  }

  /**
   @see java.util.Map#values()
   */
  public Collection values() {
    read();
    return new SetProxymap.values() );
  }

  /**
   @see java.util.Map#entrySet()
   */
  public Set entrySet() {
    read();
    return new EntrySetProxymap.entrySet() );
  }

  public boolean empty() {
    return map.isEmpty();
  }

  public String toString() {
    read();
    return map.toString();
  }

  public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner)
  throws HibernateException, SQLException {
    Object element = persister.readElementrs, owner, descriptor.getSuffixedElementAliases(), getSession() );
    Object index = persister.readIndexrs, descriptor.getSuffixedIndexAliases(), getSession() );
    if element!=null map.put(index, element);
    return element;
  }

  public Iterator entries(CollectionPersister persister) {
    return map.entrySet().iterator();
  }

  /** a wrapper for Map.Entry sets */
  class EntrySetProxy implements Set {
    private final Set set;
    EntrySetProxy(Set set) {
      this.set=set;
    }
    public boolean add(Object entry) {
      //write(); -- doesn't
      return set.add(entry);
    }
    public boolean addAll(Collection entries) {
      //write(); -- doesn't
      return set.addAll(entries);
    }
    public void clear() {
      write();
      set.clear();
    }
    public boolean contains(Object entry) {
      return set.contains(entry);
    }
    public boolean containsAll(Collection entries) {
      return set.containsAll(entries);
    }
    public boolean isEmpty() {
      return set.isEmpty();
    }
    public Iterator iterator() {
      return new EntryIteratorProxyset.iterator() );
    }
    public boolean remove(Object entry) {
      write();
      return set.remove(entry);
    }
    public boolean removeAll(Collection entries) {
      write();
      return set.removeAll(entries);
    }
    public boolean retainAll(Collection entries) {
      write();
      return set.retainAll(entries);
    }
    public int size() {
      return set.size();
    }
    // amazingly, these two will work because AbstractCollection
    // uses iterator() to fill the array
    public Object[] toArray() {
      return set.toArray();
    }
    public Object[] toArray(Object[] array) {
      return set.toArray(array);
    }
  }
  final class EntryIteratorProxy implements Iterator {
    private final Iterator iter;
    EntryIteratorProxy(Iterator iter) {
      this.iter=iter;
    }
    public boolean hasNext() {
      return iter.hasNext();
    }
    public Object next() {
      return new MapEntryProxy( (Map.Entryiter.next() );
    }
    public void remove() {
      write();
      iter.remove();
    }
  }

  final class MapEntryProxy implements Map.Entry {
    private final Map.Entry me;
    MapEntryProxyMap.Entry me ) {
      this.me = me;
    }
    public Object getKey() { return me.getKey()}
    public Object getValue() { return me.getValue()}
    public boolean equals(Object o) { return me.equals(o)}
    public int hashCode() { return me.hashCode()}
    // finally, what it's all about...
    public Object setValue(Object value) {
      write();
      return me.setValue(value);
    }
  }

  public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner)
  throws HibernateException {
    Serializable[] array = Serializable[] ) disassembled;
    int size = array.length;
    beforeInitializepersister, size );
    for int i = 0; i < size; i+=) {
      map.put(
          persister.getIndexType().assemblearray[i], getSession(), owner ),
          persister.getElementType().assemblearray[i+1], getSession(), owner )
        );
    }
  }

  public Serializable disassemble(CollectionPersister persisterthrows HibernateException {

    Serializable[] result = new Serializablemap.size() ];
    Iterator iter = map.entrySet().iterator();
    int i=0;
    while iter.hasNext() ) {
      Map.Entry e = (Map.Entryiter.next();
      result[i++= persister.getIndexType().disassemblee.getKey(), getSession()null );
      result[i++= persister.getElementType().disassemblee.getValue(), getSession()null );
    }
    return result;

  }

  public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula
  throws HibernateException {
    List deletes = new ArrayList();
    Iterator iter = ( (MapgetSnapshot() ).entrySet().iterator();
    while iter.hasNext() ) {
      Map.Entry e = (Map.Entryiter.next();
      Object key = e.getKey();
      if e.getValue()!=null && map.get(key)==null ) {
        deletes.addindexIsFormula ? e.getValue() : key );
      }
    }
    return deletes.iterator();
  }

  public boolean needsInserting(Object entry, int i, Type elemType
  throws HibernateException {
    final Map sn = (MapgetSnapshot();
    Map.Entry e = (Map.Entryentry;
    return e.getValue()!=null && sn.gete.getKey() )==null;
  }

  public boolean needsUpdating(Object entry, int i, Type elemType
  throws HibernateException {
    final Map sn = (MapgetSnapshot();
    Map.Entry e = (Map.Entryentry;
    Object snValue = sn.gete.getKey() );
    return e.getValue()!=null &&
      snValue!=null &&
      elemType.isDirtysnValue, e.getValue(), getSession() );
  }


  public Object getIndex(Object entry, int i, CollectionPersister persister) {
    return ( (Map.Entryentry ).getKey();
  }

  public Object getElement(Object entry) {
    return ( (Map.Entryentry ).getValue();
  }

  public Object getSnapshotElement(Object entry, int i) {
    final Map sn = (MapgetSnapshot();
    return sn.get( ( (Map.Entryentry ).getKey() );
  }

  public boolean equals(Object other) {
    read();
    return map.equals(other);
  }

  public int hashCode() {
    read();
    return map.hashCode();
  }

  public boolean entryExists(Object entry, int i) {
    return ( (Map.Entryentry ).getValue()!=null;
  }

  final class Clear implements DelayedOperation {
    public void operate() {
      map.clear();
    }
    public Object getAddedInstance() {
      return null;
    }
    public Object getOrphan() {
      throw new UnsupportedOperationException("queued clear cannot be used with orphan delete");
    }
  }

  final class Put implements DelayedOperation {
    private Object index;
    private Object value;
    private Object old;
    
    public Put(Object index, Object value, Object old) {
      this.index = index;
      this.value = value;
      this.old = old;
    }
    public void operate() {
      map.put(index, value);
    }
    public Object getAddedInstance() {
      return value;
    }
    public Object getOrphan() {
      return old;
    }
  }

  final class Remove implements DelayedOperation {
    private Object index;
    private Object old;
    
    public Remove(Object index, Object old) {
      this.index = index;
      this.old = old;
    }
    public void operate() {
      map.remove(index);
    }
    public Object getAddedInstance() {
      return null;
    }
    public Object getOrphan() {
      return old;
    }
  }
}