Open Source Repository

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



org/hibernate/collection/PersistentSortedMap.java
//$Id: PersistentSortedMap.java 7714 2005-08-01 16:29:33Z oneovthafew $
package org.hibernate.collection;


import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.persister.collection.BasicCollectionPersister;


/**
 * A persistent wrapper for a <tt>java.util.SortedMap</tt>. Underlying
 * collection is a <tt>TreeMap</tt>.
 *
 @see java.util.TreeMap
 @author <a href="mailto:[email protected]">e</a>
 */
public class PersistentSortedMap extends PersistentMap implements SortedMap {

  protected Comparator comparator;

  protected Serializable snapshot(BasicCollectionPersister persister, EntityMode entityModethrows HibernateException {
    TreeMap clonedMap = new TreeMap(comparator);
    Iterator iter = map.entrySet().iterator();
    while iter.hasNext() ) {
      Map.Entry e = (Map.Entryiter.next();
      clonedMap.pute.getKey(), persister.getElementType().deepCopye.getValue(), entityMode, persister.getFactory() ) );
    }
    return clonedMap;
  }

  public PersistentSortedMap(SessionImplementor session) {
    super(session);
  }

  public void setComparator(Comparator comparator) {
    this.comparator = comparator;
  }

  public PersistentSortedMap(SessionImplementor session, SortedMap map) {
    super(session, map);
    comparator = map.comparator();
  }

  public PersistentSortedMap() {} //needed for SOAP libraries, etc

  /**
   @see PersistentSortedMap#comparator()
   */
  public Comparator comparator() {
    return comparator;
  }

  /**
   @see PersistentSortedMap#subMap(Object, Object)
   */
  public SortedMap subMap(Object fromKey, Object toKey) {
    read();
    SortedMap m = ( (SortedMapmap ).subMap(fromKey, toKey);
    return new SortedSubMap(m);
  }

  /**
   @see PersistentSortedMap#headMap(Object)
   */
  public SortedMap headMap(Object toKey) {
    read();
    SortedMap m;
    m = ( (SortedMapmap ).headMap(toKey);
    return new SortedSubMap(m);
  }

  /**
   @see PersistentSortedMap#tailMap(Object)
   */
  public SortedMap tailMap(Object fromKey) {
    read();
    SortedMap m;
    m = ( (SortedMapmap ).tailMap(fromKey);
    return new SortedSubMap(m);
  }

  /**
   @see PersistentSortedMap#firstKey()
   */
  public Object firstKey() {
    read();
    return ( (SortedMapmap ).firstKey();
  }

  /**
   @see PersistentSortedMap#lastKey()
   */
  public Object lastKey() {
    read();
    return ( (SortedMapmap ).lastKey();
  }

  class SortedSubMap implements SortedMap {

    SortedMap submap;

    SortedSubMap(SortedMap m) {
      this.submap = m;
    }
    // from Map
    public int size() {
      return submap.size();
    }
    public boolean isEmpty() {
      return submap.isEmpty();
    }
    public boolean containsKey(Object key) {
      return submap.containsKey(key);
    }
    public boolean containsValue(Object key) {
      return submap.containsValue(key;
    }
    public Object get(Object key) {
      return submap.get(key);
    }
    public Object put(Object key, Object value) {
      write();
      return submap.put(key,  value);
    }
    public Object remove(Object key) {
      write();
      return submap.remove(key);
    }
    public void putAll(Map other) {
      write();
      submap.putAll(other);
    }
    public void clear() {
      write();
      submap.clear();
    }
    public Set keySet() {
      return new SetProxysubmap.keySet() );
    }
    public Collection values() {
      return new SetProxysubmap.values() );
    }
    public Set entrySet() {
      return new EntrySetProxysubmap.entrySet() );
    }
    // from SortedMap
    public Comparator comparator() {
      return submap.comparator();
    }
    public SortedMap subMap(Object fromKey, Object toKey) {
      SortedMap m;
      m = submap.subMap(fromKey, toKey);
      return new SortedSubMap);
    }
    public SortedMap headMap(Object toKey) {
      SortedMap m;
      m = submap.headMap(toKey);
      return new SortedSubMap(m);
    }
    public SortedMap tailMap(Object fromKey) {
      SortedMap m;
      m = submap.tailMap(fromKey);
      return new SortedSubMap(m);
    }
    public Object firstKey() {
      return  submap.firstKey();
    }
    public Object lastKey() {
      return submap.lastKey();
    }

  }

}