【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了PersistentList.java提供大家参考,希望对大家有所帮助!
//$Id: PersistentList.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;
/**
* A persistent wrapper for a <tt>java.util.List</tt>. Underlying
* collection is an <tt>ArrayList</tt>.
*
* @see java.util.ArrayList
* @author Gavin King
*/
public class PersistentList extends AbstractPersistentCollection implements List {
protected List list;
public Serializable getSnapshot(CollectionPersister persister) throws HibernateException {
EntityMode entityMode = getSession().getEntityMode();
ArrayList clonedList = new ArrayList( list.size() );
Iterator iter = list.iterator();
while ( iter.hasNext() ) {
Object deepCopy = persister.getElementType()
.deepCopy( iter.next(), entityMode, persister.getFactory() );
clonedList.add( deepCopy );
}
return clonedList;
}
public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException {
List sn = (List) snapshot;
return getOrphans( sn, list, entityName, getSession() );
}
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
Type elementType = persister.getElementType();
List sn = (List) getSnapshot();
if ( sn.size()!=this.list.size() ) return false;
Iterator iter = list.iterator();
Iterator sniter = sn.iterator();
while ( iter.hasNext() ) {
if ( elementType.isDirty( iter.next(), sniter.next(), getSession() ) ) return false;
}
return true;
}
public boolean isSnapshotEmpty(Serializable snapshot) {
return ( (Collection) snapshot ).isEmpty();
}
public PersistentList(SessionImplementor session) {
super(session);
}
public PersistentList(SessionImplementor session, List list) {
super(session);
this.list = list;
setInitialized();
setDirectlyAccessible(true);
}
public void beforeInitialize(CollectionPersister persister) {
this.list = (List) persister.getCollectionType().instantiate();
}
public boolean isWrapper(Object collection) {
return list==collection;
}
public PersistentList() {} //needed for SOAP libraries, etc
/**
* @see java.util.List#size()
*/
public int size() {
return readSize() ? getCachedSize() : list.size();
}
/**
* @see java.util.List#isEmpty()
*/
public boolean isEmpty() {
return readSize() ? getCachedSize()==0 : list.isEmpty();
}
/**
* @see java.util.List#contains(Object)
*/
public boolean contains(Object object) {
Boolean exists = readElementExistence(object);
return exists==null ?
list.contains(object) :
exists.booleanValue();
}
/**
* @see java.util.List#iterator()
*/
public Iterator iterator() {
read();
return new IteratorProxy( list.iterator() );
}
/**
* @see java.util.List#toArray()
*/
public Object[] toArray() {
read();
return list.toArray();
}
/**
* @see java.util.List#toArray(Object[])
*/
public Object[] toArray(Object[] array) {
read();
return list.toArray(array);
}
/**
* @see java.util.List#add(Object)
*/
public boolean add(Object object) {
if ( !isOperationQueueEnabled() ) {
write();
return list.add(object);
}
else {
queueOperation( new SimpleAdd(object) );
return true;
}
}
/**
* @see java.util.List#remove(Object)
*/
public boolean remove(Object value) {
Boolean exists = isPutQueueEnabled() ?
readElementExistence(value) : null;
if ( exists==null ) {
write();
return list.remove(value);
}
else if ( exists.booleanValue() ) {
queueOperation( new SimpleRemove(value) );
return true;
}
else {
return false;
}
}
/**
* @see java.util.List#containsAll(Collection)
*/
public boolean containsAll(Collection coll) {
read();
return list.containsAll(coll);
}
/**
* @see java.util.List#addAll(Collection)
*/
public boolean addAll(Collection values) {
if ( values.size()==0 ) return false;
if ( !isOperationQueueEnable