【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“PersistentBag.java”一文,供大家参考学习
//$Id: PersistentBag.java 7714 2005-08-01 16:29:33Z oneovthafew $
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.Iterator;
import java.util.List;
import java.util.ListIterator;
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;
/**
* An unordered, unkeyed collection that can contain the same element
* multiple times. The Java collections API, curiously, has no <tt>Bag</tt>.
* Most developers seem to use <tt>List</tt>s to represent bag semantics,
* so Hibernate follows this practice.
*
* @author Gavin King
*/
public class PersistentBag extends AbstractPersistentCollection implements List {
protected List bag;
public PersistentBag(SessionImplementor session) {
super(session);
}
public PersistentBag(SessionImplementor session, Collection coll) {
super(session);
if (coll instanceof List) {
bag = (List) coll;
}
else {
bag = new ArrayList();
Iterator iter = coll.iterator();
while ( iter.hasNext() ) {
bag.add( iter.next() );
}
}
setInitialized();
setDirectlyAccessible(true);
}
public PersistentBag() {} //needed for SOAP libraries, etc
public boolean isWrapper(Object collection) {
return bag==collection;
}
public boolean empty() {
return bag.isEmpty();
}
public Iterator entries(CollectionPersister persister) {
return bag.iterator();
}
public Object readFrom(ResultSet rs, CollectionPersister persister, CollectionAliases descriptor, Object owner)
throws HibernateException, SQLException {
// note that if we load this collection from a cartesian product
// the multiplicity would be broken ... so use an idbag instead
Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ) ;
if (element!=null) bag.add(element);
return element;
}
public void beforeInitialize(CollectionPersister persister) {
this.bag = (List) persister.getCollectionType().instantiate();
}
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
Type elementType = persister.getElementType();
EntityMode entityMode = getSession().getEntityMode();
List sn = (List) getSnapshot();
if ( sn.size()!=bag.size() ) return false;
Iterator iter = bag.iterator();
while ( iter.hasNext() ) {
Object elt = iter.next();
final boolean unequal = countOccurrences(elt, bag, elementType, entityMode) !=
countOccurrences(elt, sn, elementType, entityMode);
if ( unequal ) return false;
}
return true;
}
public boolean isSnapshotEmpty(Serializable snapshot) {
return ( (Collection) snapshot ).isEmpty();
}
private int countOccurrences(Object element, List list, Type elementType, EntityMode entityMode)
throws HibernateException {
Iterator iter = list.iterator();
int result=0;
while ( iter.hasNext() ) {
if ( elementType.isSame( element, iter.next(), entityMode ) ) result++;
}
return result;
}
public Serializable getSnapshot(CollectionPersister persister)
throws HibernateException {
EntityMode entityMode = getSession().getEntityMode();
ArrayList clonedList = new ArrayList( bag.size() );
Iterator iter = bag.iterator();
while ( iter.hasNext() ) {
clonedList.add( persister.getElementType().deepCopy( iter.next(), entityMode, persister.getFactory() ) );
}
return clonedList;
}
public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException {
List sn = (List) snapshot;
return getOrphans( sn, bag, entityName, getSession() );
}
public Serializable disassemble(CollectionPersister persister)
throws HibernateException {
int length = bag.size();
Serializable[] result = new Serializable[length];
for ( int i=0; i<length; i++ ) {
result[i] = persister.getElementType().disassemble( bag.get(i), getSession(), null );
}
return result;
}
public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner)
throws HibernateException {
beforeInitiali