Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » type » [javadoc | source]
    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.type;
   26   
   27   import java.io.Serializable;
   28   import java.util.HashMap;
   29   import java.util.Iterator;
   30   import java.util.Map;
   31   
   32   import org.dom4j.Element;
   33   import org.hibernate.EntityMode;
   34   import org.hibernate.HibernateException;
   35   import org.hibernate.collection.PersistentCollection;
   36   import org.hibernate.collection.PersistentMap;
   37   import org.hibernate.collection.PersistentMapElementHolder;
   38   import org.hibernate.engine.SessionImplementor;
   39   import org.hibernate.persister.collection.CollectionPersister;
   40   
   41   
   42   public class MapType extends CollectionType {
   43   
   44   	public MapType(String role, String propertyRef, boolean isEmbeddedInXML) {
   45   		super(role, propertyRef, isEmbeddedInXML);
   46   	}
   47   
   48   	public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) {
   49   		if ( session.getEntityMode()==EntityMode.DOM4J ) {
   50   			return new PersistentMapElementHolder(session, persister, key);
   51   		}
   52   		else {
   53   			return new PersistentMap(session);
   54   		}
   55   	}
   56   
   57   	public Class getReturnedClass() {
   58   		return Map.class;
   59   	}
   60   
   61   	public Iterator getElementsIterator(Object collection) {
   62   		return ( (java.util.Map) collection ).values().iterator();
   63   	}
   64   
   65   	public PersistentCollection wrap(SessionImplementor session, Object collection) {
   66   		if ( session.getEntityMode()==EntityMode.DOM4J ) {
   67   			return new PersistentMapElementHolder( session, (Element) collection );
   68   		}
   69   		else {
   70   			return new PersistentMap( session, (java.util.Map) collection );
   71   		}
   72   	}
   73   	
   74   	public Object instantiate(int anticipatedSize) {
   75   		return anticipatedSize <= 0 
   76   		       ? new HashMap()
   77   		       : new HashMap( anticipatedSize + (int)( anticipatedSize * .75f ), .75f );
   78   	}
   79   
   80   	public Object replaceElements(
   81   		final Object original,
   82   		final Object target,
   83   		final Object owner, 
   84   		final java.util.Map copyCache, 
   85   		final SessionImplementor session)
   86   		throws HibernateException {
   87   
   88   		CollectionPersister cp = session.getFactory().getCollectionPersister( getRole() );
   89   		
   90   		java.util.Map result = (java.util.Map) target;
   91   		result.clear();
   92   		
   93   		Iterator iter = ( (java.util.Map) original ).entrySet().iterator();
   94   		while ( iter.hasNext() ) {
   95   			java.util.Map.Entry me = (java.util.Map.Entry) iter.next();
   96   			Object key = cp.getIndexType().replace( me.getKey(), null, session, owner, copyCache );
   97   			Object value = cp.getElementType().replace( me.getValue(), null, session, owner, copyCache );
   98   			result.put(key, value);
   99   		}
  100   		
  101   		return result;
  102   		
  103   	}
  104   	
  105   	public Object indexOf(Object collection, Object element) {
  106   		Iterator iter = ( (Map) collection ).entrySet().iterator();
  107   		while ( iter.hasNext() ) {
  108   			Map.Entry me = (Map.Entry) iter.next();
  109   			//TODO: proxies!
  110   			if ( me.getValue()==element ) return me.getKey();
  111   		}
  112   		return null;
  113   	}
  114   
  115   }

Save This Page
Home » hibernate-distribution-3.3.1.GA-dist » org.hibernate » type » [javadoc | source]