genericize PersistentCollection hierarchy

This commit is contained in:
Gavin King 2021-02-20 16:23:56 +01:00 committed by Christian Beikov
parent 40947297e4
commit e631574922
10 changed files with 481 additions and 665 deletions

View File

@ -20,7 +20,6 @@ import org.hibernate.AssertionFailure;
import org.hibernate.FlushMode; import org.hibernate.FlushMode;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.LazyInitializationException; import org.hibernate.LazyInitializationException;
import org.hibernate.Session;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.ForeignKeys; import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.spi.CollectionEntry; import org.hibernate.engine.spi.CollectionEntry;
@ -54,7 +53,7 @@ import org.hibernate.type.UUIDCharType;
* *
* @author Gavin King * @author Gavin King
*/ */
public abstract class AbstractPersistentCollection implements Serializable, PersistentCollection { public abstract class AbstractPersistentCollection<E> implements Serializable, PersistentCollection<E> {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( AbstractPersistentCollection.class ); private static final CoreMessageLogger LOG = CoreLogging.messageLogger( AbstractPersistentCollection.class );
private transient SharedSessionContractImplementor session; private transient SharedSessionContractImplementor session;
@ -63,7 +62,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
private boolean initialized; private boolean initialized;
private transient boolean initializing; private transient boolean initializing;
private transient List<DelayedOperation> operationQueue; private transient List<DelayedOperation<E>> operationQueue;
private transient boolean directlyAccessible; private transient boolean directlyAccessible;
private Object owner; private Object owner;
private int cachedSize = -1; private int cachedSize = -1;
@ -154,7 +153,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
/** /**
* Called by the {@link Collection#size} method * Called by the {@link Collection#size} method
*/ */
@SuppressWarnings({"JavaDoc"})
protected boolean readSize() { protected boolean readSize() {
if ( !initialized ) { if ( !initialized ) {
if ( cachedSize != -1 && !hasQueuedOperations() ) { if ( cachedSize != -1 && !hasQueuedOperations() ) {
@ -162,9 +160,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
else { else {
final boolean isExtraLazy = withTemporarySessionIfNeeded( final boolean isExtraLazy = withTemporarySessionIfNeeded(
new LazyInitializationWork<Boolean>() { () -> {
@Override
public Boolean doWork() {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this ); final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this );
if ( entry != null ) { if ( entry != null ) {
@ -185,11 +181,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
return false; return false;
} }
}
); );
if ( isExtraLazy ) { return isExtraLazy;
return true;
}
} }
} }
return false; return false;
@ -200,13 +193,13 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* *
* @param <T> The java type of the return for this LazyInitializationWork * @param <T> The java type of the return for this LazyInitializationWork
*/ */
public static interface LazyInitializationWork<T> { public interface LazyInitializationWork<T> {
/** /**
* Do the represented work and return the result. * Do the represented work and return the result.
* *
* @return The result * @return The result
*/ */
public T doWork(); T doWork();
} }
private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitializationWork) { private <T> T withTemporarySessionIfNeeded(LazyInitializationWork<T> lazyInitializationWork) {
@ -299,9 +292,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
protected Boolean readIndexExistence(final Object index) { protected Boolean readIndexExistence(final Object index) {
if ( !initialized ) { if ( !initialized ) {
final Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded( final Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded(
new LazyInitializationWork<Boolean>() { () -> {
@Override
public Boolean doWork() {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this ); final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this );
final CollectionPersister persister = entry.getLoadedPersister(); final CollectionPersister persister = entry.getLoadedPersister();
if ( persister.isExtraLazy() ) { if ( persister.isExtraLazy() ) {
@ -315,7 +306,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
return null; return null;
} }
}
); );
if ( extraLazyExistenceCheck != null ) { if ( extraLazyExistenceCheck != null ) {
return extraLazyExistenceCheck; return extraLazyExistenceCheck;
@ -327,9 +317,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
protected Boolean readElementExistence(final Object element) { protected Boolean readElementExistence(final Object element) {
if ( !initialized ) { if ( !initialized ) {
final Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded( final Boolean extraLazyExistenceCheck = withTemporarySessionIfNeeded(
new LazyInitializationWork<Boolean>() { () -> {
@Override
public Boolean doWork() {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this ); final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this );
final CollectionPersister persister = entry.getLoadedPersister(); final CollectionPersister persister = entry.getLoadedPersister();
if ( persister.isExtraLazy() ) { if ( persister.isExtraLazy() ) {
@ -343,7 +331,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
return null; return null;
} }
}
); );
if ( extraLazyExistenceCheck != null ) { if ( extraLazyExistenceCheck != null ) {
return extraLazyExistenceCheck; return extraLazyExistenceCheck;
@ -356,7 +343,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
protected Object readElementByIndex(final Object index) { protected Object readElementByIndex(final Object index) {
if ( !initialized ) { if ( !initialized ) {
class ExtraLazyElementByIndexReader implements LazyInitializationWork { class ExtraLazyElementByIndexReader implements LazyInitializationWork<Object> {
private boolean isExtraLazy; private boolean isExtraLazy;
private Object element; private Object element;
@ -379,7 +366,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
final ExtraLazyElementByIndexReader reader = new ExtraLazyElementByIndexReader(); final ExtraLazyElementByIndexReader reader = new ExtraLazyElementByIndexReader();
//noinspection unchecked
withTemporarySessionIfNeeded( reader ); withTemporarySessionIfNeeded( reader );
if ( reader.isExtraLazy ) { if ( reader.isExtraLazy ) {
return reader.element; return reader.element;
@ -415,7 +401,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* Is this collection in a state that would allow us to * Is this collection in a state that would allow us to
* "queue" operations? * "queue" operations?
*/ */
@SuppressWarnings({"JavaDoc"})
protected boolean isOperationQueueEnabled() { protected boolean isOperationQueueEnabled() {
return !initialized return !initialized
&& isConnectedToSession() && isConnectedToSession()
@ -427,7 +412,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* "queue" puts? This is a special case, because of orphan * "queue" puts? This is a special case, because of orphan
* delete. * delete.
*/ */
@SuppressWarnings({"JavaDoc"})
protected boolean isPutQueueEnabled() { protected boolean isPutQueueEnabled() {
return !initialized return !initialized
&& isConnectedToSession() && isConnectedToSession()
@ -439,7 +423,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* "queue" clear? This is a special case, because of orphan * "queue" clear? This is a special case, because of orphan
* delete. * delete.
*/ */
@SuppressWarnings({"JavaDoc"})
protected boolean isClearQueueEnabled() { protected boolean isClearQueueEnabled() {
return !initialized return !initialized
&& isConnectedToSession() && isConnectedToSession()
@ -449,7 +432,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
/** /**
* Is this the "inverse" end of a bidirectional association? * Is this the "inverse" end of a bidirectional association?
*/ */
@SuppressWarnings({"JavaDoc"})
protected boolean isInverseCollection() { protected boolean isInverseCollection() {
final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( this ); final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( this );
return ce != null && ce.getLoadedPersister().isInverse(); return ce != null && ce.getLoadedPersister().isInverse();
@ -459,7 +441,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* Is this the "inverse" end of a bidirectional association with * Is this the "inverse" end of a bidirectional association with
* no orphan delete enabled? * no orphan delete enabled?
*/ */
@SuppressWarnings({"JavaDoc"})
protected boolean isInverseCollectionNoOrphanDelete() { protected boolean isInverseCollectionNoOrphanDelete() {
final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( this ); final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( ce == null ) { if ( ce == null ) {
@ -473,7 +454,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* Is this the "inverse" end of a bidirectional one-to-many, or * Is this the "inverse" end of a bidirectional one-to-many, or
* of a collection with no orphan delete? * of a collection with no orphan delete?
*/ */
@SuppressWarnings({"JavaDoc"})
protected boolean isInverseOneToManyOrNoOrphanDelete() { protected boolean isInverseOneToManyOrNoOrphanDelete() {
final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( this ); final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( ce == null ) { if ( ce == null ) {
@ -486,8 +466,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
/** /**
* Queue an addition * Queue an addition
*/ */
@SuppressWarnings({"JavaDoc"}) protected final void queueOperation(DelayedOperation<E> operation) {
protected final void queueOperation(DelayedOperation operation) {
if ( operationQueue == null ) { if ( operationQueue == null ) {
operationQueue = new ArrayList<>( 10 ); operationQueue = new ArrayList<>( 10 );
} }
@ -503,9 +482,9 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* merged to managed copy. * merged to managed copy.
*/ */
public final void replaceQueuedOperationValues(CollectionPersister persister, Map copyCache) { public final void replaceQueuedOperationValues(CollectionPersister persister, Map copyCache) {
for ( DelayedOperation operation : operationQueue ) { for ( DelayedOperation<?> operation : operationQueue ) {
if ( ValueDelayedOperation.class.isInstance( operation ) ) { if ( ValueDelayedOperation.class.isInstance( operation ) ) {
( (ValueDelayedOperation) operation ).replace( persister, copyCache ); ( (ValueDelayedOperation<?>) operation ).replace( persister, copyCache );
} }
} }
} }
@ -515,7 +494,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* add the queued elements to the underlying collection. * add the queued elements to the underlying collection.
*/ */
protected final void performQueuedOperations() { protected final void performQueuedOperations() {
for ( DelayedOperation operation : operationQueue ) { for ( DelayedOperation<?> operation : operationQueue ) {
operation.operate(); operation.operate();
} }
clearOperationQueue(); clearOperationQueue();
@ -588,13 +567,10 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
withTemporarySessionIfNeeded( withTemporarySessionIfNeeded(
new LazyInitializationWork<Object>() { () -> {
@Override
public Object doWork() {
session.initializeCollection( AbstractPersistentCollection.this, writing ); session.initializeCollection( AbstractPersistentCollection.this, writing );
return null; return null;
} }
}
); );
} }
@ -792,7 +768,6 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
/** /**
* Get the current snapshot from the session * Get the current snapshot from the session
*/ */
@SuppressWarnings({"JavaDoc"})
protected final Serializable getSnapshot() { protected final Serializable getSnapshot() {
return session.getPersistenceContext().getSnapshot( this ); return session.getPersistenceContext().getSnapshot( this );
} }
@ -813,13 +788,13 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public final Iterator queuedAdditionIterator() { public final Iterator<E> queuedAdditionIterator() {
if ( hasQueuedOperations() ) { if ( hasQueuedOperations() ) {
return new Iterator() { return new Iterator<E>() {
private int index; private int index;
@Override @Override
public Object next() { public E next() {
return operationQueue.get( index++ ).getAddedInstance(); return operationQueue.get( index++ ).getAddedInstance();
} }
@ -840,19 +815,18 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public final Collection<E> getQueuedOrphans(String entityName) {
public final Collection getQueuedOrphans(String entityName) {
if ( hasQueuedOperations() ) { if ( hasQueuedOperations() ) {
final Collection additions = new ArrayList( operationQueue.size() ); final Collection<E> additions = new ArrayList<>( operationQueue.size() );
final Collection removals = new ArrayList( operationQueue.size() ); final Collection<E> removals = new ArrayList<>( operationQueue.size() );
for ( DelayedOperation operation : operationQueue ) { for ( DelayedOperation<E> operation : operationQueue ) {
additions.add( operation.getAddedInstance() ); additions.add( operation.getAddedInstance() );
removals.add( operation.getOrphan() ); removals.add( operation.getOrphan() );
} }
return getOrphans( removals, additions, entityName, session ); return getOrphans( removals, additions, entityName, session );
} }
else { else {
return Collections.EMPTY_LIST; return Collections.emptyList();
} }
} }
@ -865,7 +839,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public abstract Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException; public abstract Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException;
/** /**
* Get the session currently associated with this collection. * Get the session currently associated with this collection.
@ -876,10 +850,10 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
return session; return session;
} }
protected final class IteratorProxy implements Iterator { protected final class IteratorProxy<E> implements Iterator<E> {
protected final Iterator itr; protected final Iterator<E> itr;
public IteratorProxy(Iterator itr) { public IteratorProxy(Iterator<E> itr) {
this.itr = itr; this.itr = itr;
} }
@ -889,7 +863,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public Object next() { public E next() {
return itr.next(); return itr.next();
} }
@ -900,16 +874,15 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
} }
protected final class ListIteratorProxy implements ListIterator { protected final class ListIteratorProxy implements ListIterator<E> {
protected final ListIterator itr; protected final ListIterator<E> itr;
public ListIteratorProxy(ListIterator itr) { public ListIteratorProxy(ListIterator<E> itr) {
this.itr = itr; this.itr = itr;
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public void add(E o) {
public void add(Object o) {
write(); write();
itr.add( o ); itr.add( o );
} }
@ -925,7 +898,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public Object next() { public E next() {
return itr.next(); return itr.next();
} }
@ -935,7 +908,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public Object previous() { public E previous() {
return itr.previous(); return itr.previous();
} }
@ -951,30 +924,27 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public void set(E o) {
public void set(Object o) {
write(); write();
itr.set( o ); itr.set( o );
} }
} }
protected class SetProxy implements java.util.Set { protected class SetProxy<E> implements java.util.Set<E> {
protected final Collection set; protected final Collection<E> set;
public SetProxy(Collection set) { public SetProxy(Collection<E> set) {
this.set = set; this.set = set;
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public boolean add(E o) {
public boolean add(Object o) {
write(); write();
return set.add( o ); return set.add( o );
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public boolean addAll(Collection<? extends E> c) {
public boolean addAll(Collection c) {
write(); write();
return set.addAll( c ); return set.addAll( c );
} }
@ -991,8 +961,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean containsAll(Collection<?> c) {
public boolean containsAll(Collection c) {
return set.containsAll( c ); return set.containsAll( c );
} }
@ -1002,8 +971,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public Iterator iterator() { public Iterator<E> iterator() {
return new IteratorProxy( set.iterator() ); return new IteratorProxy<>( set.iterator() );
} }
@Override @Override
@ -1013,15 +982,13 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean removeAll(Collection<?> c) {
public boolean removeAll(Collection c) {
write(); write();
return set.removeAll( c ); return set.removeAll( c );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean retainAll(Collection<?> c) {
public boolean retainAll(Collection c) {
write(); write();
return set.retainAll( c ); return set.retainAll( c );
} }
@ -1037,43 +1004,38 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public <A> A[] toArray(A[] array) {
public Object[] toArray(Object[] array) {
return set.toArray( array ); return set.toArray( array );
} }
} }
protected final class ListProxy implements java.util.List { protected final class ListProxy implements java.util.List<E> {
protected final List list; protected final List<E> list;
public ListProxy(List list) { public ListProxy(List<E> list) {
this.list = list; this.list = list;
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public void add(int index, E value) {
public void add(int index, Object value) {
write(); write();
list.add( index, value ); list.add( index, value );
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public boolean add(E o) {
public boolean add(Object o) {
write(); write();
return list.add( o ); return list.add( o );
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public boolean addAll(Collection<? extends E> c) {
public boolean addAll(Collection c) {
write(); write();
return list.addAll( c ); return list.addAll( c );
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public boolean addAll(int i, Collection<? extends E> c) {
public boolean addAll(int i, Collection c) {
write(); write();
return list.addAll( i, c ); return list.addAll( i, c );
} }
@ -1090,13 +1052,12 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean containsAll(Collection<?> c) {
public boolean containsAll(Collection c) {
return list.containsAll( c ); return list.containsAll( c );
} }
@Override @Override
public Object get(int i) { public E get(int i) {
return list.get( i ); return list.get( i );
} }
@ -1111,8 +1072,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public Iterator iterator() { public Iterator<E> iterator() {
return new IteratorProxy( list.iterator() ); return new IteratorProxy<>( list.iterator() );
} }
@Override @Override
@ -1121,17 +1082,17 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public ListIterator listIterator() { public ListIterator<E> listIterator() {
return new ListIteratorProxy( list.listIterator() ); return new ListIteratorProxy( list.listIterator() );
} }
@Override @Override
public ListIterator listIterator(int i) { public ListIterator<E> listIterator(int i) {
return new ListIteratorProxy( list.listIterator( i ) ); return new ListIteratorProxy( list.listIterator( i ) );
} }
@Override @Override
public Object remove(int i) { public E remove(int i) {
write(); write();
return list.remove( i ); return list.remove( i );
} }
@ -1143,22 +1104,19 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean removeAll(Collection<?> c) {
public boolean removeAll(Collection c) {
write(); write();
return list.removeAll( c ); return list.removeAll( c );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean retainAll(Collection<?> c) {
public boolean retainAll(Collection c) {
write(); write();
return list.retainAll( c ); return list.retainAll( c );
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public E set(int i, E o) {
public Object set(int i, Object o) {
write(); write();
return list.set( i, o ); return list.set( i, o );
} }
@ -1169,7 +1127,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
public List subList(int i, int j) { public List<E> subList(int i, int j) {
return list.subList( i, j ); return list.subList( i, j );
} }
@ -1179,8 +1137,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
@Override @Override
@SuppressWarnings({"unchecked"}) public <A> A[] toArray(A[] array) {
public Object[] toArray(Object[] array) {
return list.toArray( array ); return list.toArray( array );
} }
@ -1189,23 +1146,23 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
/** /**
* Contract for operations which are part of a collection's operation queue. * Contract for operations which are part of a collection's operation queue.
*/ */
protected interface DelayedOperation { protected interface DelayedOperation<E> {
public void operate(); void operate();
public Object getAddedInstance(); E getAddedInstance();
public Object getOrphan(); E getOrphan();
} }
protected interface ValueDelayedOperation extends DelayedOperation { protected interface ValueDelayedOperation<E> extends DelayedOperation<E> {
void replace(CollectionPersister collectionPersister, Map copyCache); void replace(CollectionPersister collectionPersister, Map copyCache);
} }
protected abstract class AbstractValueDelayedOperation implements ValueDelayedOperation { protected abstract class AbstractValueDelayedOperation implements ValueDelayedOperation<E> {
private Object addedValue; private E addedValue;
private Object orphan; private E orphan;
protected AbstractValueDelayedOperation(Object addedValue, Object orphan) { protected AbstractValueDelayedOperation(E addedValue, E orphan) {
this.addedValue = addedValue; this.addedValue = addedValue;
this.orphan = orphan; this.orphan = orphan;
} }
@ -1216,17 +1173,17 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
} }
protected final Object getReplacement(Type type, Object current, Map copyCache) { protected final E getReplacement(Type type, Object current, Map copyCache) {
return type.replace( current, null, session, owner, copyCache ); return (E) type.replace( current, null, session, owner, copyCache );
} }
@Override @Override
public final Object getAddedInstance() { public final E getAddedInstance() {
return addedValue; return addedValue;
} }
@Override @Override
public final Object getOrphan() { public final E getOrphan() {
return orphan; return orphan;
} }
} }
@ -1236,10 +1193,9 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* belong to the collection, and a collection of instances * belong to the collection, and a collection of instances
* that currently belong, return a collection of orphans * that currently belong, return a collection of orphans
*/ */
@SuppressWarnings({"JavaDoc", "unchecked"}) protected static <E> Collection<E> getOrphans(
protected static Collection getOrphans( Collection<E> oldElements,
Collection oldElements, Collection<E> currentElements,
Collection currentElements,
String entityName, String entityName,
SharedSessionContractImplementor session) throws HibernateException { SharedSessionContractImplementor session) throws HibernateException {
@ -1258,11 +1214,11 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
final boolean useIdDirect = mayUseIdDirect( idType ); final boolean useIdDirect = mayUseIdDirect( idType );
// create the collection holding the Orphans // create the collection holding the Orphans
final Collection res = new ArrayList(); final Collection<E> res = new ArrayList<>();
// collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access // collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
final java.util.Set currentIds = new HashSet(); final java.util.Set<Object> currentIds = new HashSet<>();
final java.util.Set currentSaving = new IdentitySet(); final java.util.Set<Object> currentSaving = new IdentitySet<>();
final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
for ( Object current : currentElements ) { for ( Object current : currentElements ) {
if ( current != null && ForeignKeys.isNotTransient( entityName, current, null, session ) ) { if ( current != null && ForeignKeys.isNotTransient( entityName, current, null, session ) ) {
@ -1282,7 +1238,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
} }
// iterate over the *old* list // iterate over the *old* list
for ( Object old : oldElements ) { for ( E old : oldElements ) {
if ( !currentSaving.contains( old ) ) { if ( !currentSaving.contains( old ) ) {
final Object oldId = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, old, session ); final Object oldId = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, old, session );
if ( !currentIds.contains( useIdDirect ? oldId : new TypedValue( idType, oldId ) ) ) { if ( !currentIds.contains( useIdDirect ? oldId : new TypedValue( idType, oldId ) ) ) {
@ -1312,7 +1268,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* @param session The session * @param session The session
*/ */
public static void identityRemove( public static void identityRemove(
Collection list, Collection<?> list,
Object entityInstance, Object entityInstance,
String entityName, String entityName,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
@ -1322,7 +1278,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
final Type idType = entityPersister.getIdentifierType(); final Type idType = entityPersister.getIdentifierType();
final Object idOfCurrent = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, entityInstance, session ); final Object idOfCurrent = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, entityInstance, session );
final Iterator itr = list.iterator(); final Iterator<?> itr = list.iterator();
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
final Object idOfOld = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, itr.next(), session ); final Object idOfOld = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, itr.next(), session );
if ( idType.isEqual( idOfCurrent, idOfOld, session.getFactory() ) ) { if ( idType.isEqual( idOfCurrent, idOfOld, session.getFactory() ) ) {
@ -1347,7 +1303,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
*/ */
@Deprecated @Deprecated
public static void identityRemove( public static void identityRemove(
Collection list, Collection<?> list,
Object entityInstance, Object entityInstance,
String entityName, String entityName,
SessionImplementor session) { SessionImplementor session) {

View File

@ -32,14 +32,14 @@ import org.hibernate.type.Type;
* *
* @author Gavin King * @author Gavin King
*/ */
public class PersistentBag extends AbstractPersistentCollection implements List { public class PersistentBag<E> extends AbstractPersistentCollection<E> implements List<E> {
protected List bag; protected List<E> bag;
/** /**
* The Collection provided to a PersistentBag constructor * The Collection provided to a PersistentBag constructor
*/ */
private Collection providedCollection; private Collection<E> providedCollection;
/** /**
* Constructs a PersistentBag. Needed for SOAP libraries, etc * Constructs a PersistentBag. Needed for SOAP libraries, etc
@ -75,15 +75,14 @@ public class PersistentBag extends AbstractPersistentCollection implements List
* @param session The session * @param session The session
* @param coll The base elements. * @param coll The base elements.
*/ */
@SuppressWarnings("unchecked") public PersistentBag(SharedSessionContractImplementor session, Collection<E> coll) {
public PersistentBag(SharedSessionContractImplementor session, Collection coll) {
super( session ); super( session );
providedCollection = coll; providedCollection = coll;
if ( coll instanceof List ) { if ( coll instanceof List ) {
bag = (List) coll; bag = (List<E>) coll;
} }
else { else {
bag = new ArrayList( coll ); bag = new ArrayList<>( coll );
} }
setInitialized(); setInitialized();
setDirectlyAccessible( true ); setDirectlyAccessible( true );
@ -99,7 +98,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
* should be used instead. * should be used instead.
*/ */
@Deprecated @Deprecated
public PersistentBag(SessionImplementor session, Collection coll) { public PersistentBag(SessionImplementor session, Collection<E> coll) {
this( (SharedSessionContractImplementor) session, coll ); this( (SharedSessionContractImplementor) session, coll );
} }
@ -119,33 +118,31 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
public Iterator entries(CollectionPersister persister) { public Iterator<E> entries(CollectionPersister persister) {
return bag.iterator(); return bag.iterator();
} }
public void injectLoadedState(PluralAttributeMapping attributeMapping, List loadingState) { public void injectLoadedState(PluralAttributeMapping attributeMapping, List<?> loadingState) {
assert bag == null; assert bag == null;
final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor(); final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor();
final CollectionSemantics collectionSemantics = collectionDescriptor.getCollectionSemantics(); final CollectionSemantics<?> collectionSemantics = collectionDescriptor.getCollectionSemantics();
final int elementCount = loadingState == null ? 0 : loadingState.size(); final int elementCount = loadingState == null ? 0 : loadingState.size();
this.bag = (List) collectionSemantics.instantiateRaw( elementCount, collectionDescriptor ); this.bag = (List<E>) collectionSemantics.instantiateRaw( elementCount, collectionDescriptor );
if ( loadingState != null ) { if ( loadingState != null ) {
for ( int i = 0; i < elementCount; i++ ) { for ( int i = 0; i < elementCount; i++ ) {
//noinspection unchecked bag.add( (E) loadingState.get( i ) );
bag.add( loadingState.get( i ) );
} }
} }
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
final Type elementType = persister.getElementType(); final Type elementType = persister.getElementType();
final List<Object> sn = (List<Object>) getSnapshot(); final List<?> sn = (List<?>) getSnapshot();
if ( sn.size() != bag.size() ) { if ( sn.size() != bag.size() ) {
return false; return false;
} }
@ -196,7 +193,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
* *
* @return Map of "equality" hashCode to List of objects * @return Map of "equality" hashCode to List of objects
*/ */
private Map<Integer, List<Object>> groupByEqualityHash(List<Object> searchedBag, Type elementType) { private Map<Integer, List<Object>> groupByEqualityHash(List<?> searchedBag, Type elementType) {
if ( searchedBag.isEmpty() ) { if ( searchedBag.isEmpty() ) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
@ -208,8 +205,6 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
/** /**
* @param o
* @param elementType
* @return the default elementType hashcode of the object o, or null if the object is null * @return the default elementType hashcode of the object o, or null if the object is null
*/ */
private Integer nullableHashCode(Object o, Type elementType) { private Integer nullableHashCode(Object o, Type elementType) {
@ -223,7 +218,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
@Override @Override
public boolean isSnapshotEmpty(Serializable snapshot) { public boolean isSnapshotEmpty(Serializable snapshot) {
return ( (Collection) snapshot ).isEmpty(); return ( (Collection<?>) snapshot ).isEmpty();
} }
private int countOccurrences(Object element, List<Object> list, Type elementType) { private int countOccurrences(Object element, List<Object> list, Type elementType) {
@ -249,26 +244,25 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked")
public Serializable getSnapshot(CollectionPersister persister) public Serializable getSnapshot(CollectionPersister persister)
throws HibernateException { throws HibernateException {
final ArrayList clonedList = new ArrayList( bag.size() ); final ArrayList<E> clonedList = new ArrayList<>( bag.size() );
for ( Object item : bag ) { for ( E item : bag ) {
clonedList.add( persister.getElementType().deepCopy( item, persister.getFactory() ) ); clonedList.add( (E) persister.getElementType().deepCopy( item, persister.getFactory() ) );
} }
return clonedList; return clonedList;
} }
@Override @Override
public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { public Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException {
final List sn = (List) snapshot; final List<E> sn = (List<E>) snapshot;
return getOrphans( sn, bag, entityName, getSession() ); return getOrphans( sn, bag, entityName, getSession() );
} }
@Override @Override
public void initializeEmptyCollection(CollectionPersister persister) { public void initializeEmptyCollection(CollectionPersister persister) {
assert bag == null; assert bag == null;
bag = (List) persister.getCollectionType().instantiate( 0 ); bag = (List<E>) persister.getCollectionType().instantiate( 0 );
endRead(); endRead();
} }
@ -283,7 +277,6 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked")
public void initializeFromCache(CollectionPersister collectionDescriptor, Object disassembled, Object owner) public void initializeFromCache(CollectionPersister collectionDescriptor, Object disassembled, Object owner)
throws HibernateException { throws HibernateException {
assert bag == null; assert bag == null;
@ -291,12 +284,12 @@ public class PersistentBag extends AbstractPersistentCollection implements List
final Serializable[] array = (Serializable[]) disassembled; final Serializable[] array = (Serializable[]) disassembled;
final int size = array.length; final int size = array.length;
this.bag = (List) collectionDescriptor.getCollectionSemantics().instantiateRaw( size, collectionDescriptor ); this.bag = (List<E>) collectionDescriptor.getCollectionSemantics().instantiateRaw( size, collectionDescriptor );
for ( Serializable item : array ) { for ( Serializable item : array ) {
final Object element = collectionDescriptor.getElementType().assemble( item, getSession(), owner ); final Object element = collectionDescriptor.getElementType().assemble( item, getSession(), owner );
if ( element != null ) { if ( element != null ) {
bag.add( element ); bag.add( (E) element );
} }
} }
} }
@ -315,16 +308,15 @@ public class PersistentBag extends AbstractPersistentCollection implements List
// <one-to-many> <bag>! // <one-to-many> <bag>!
@Override @Override
@SuppressWarnings("unchecked") public Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
final Type elementType = persister.getElementType(); final Type elementType = persister.getElementType();
final ArrayList deletes = new ArrayList(); final ArrayList<Object> deletes = new ArrayList<>();
final List sn = (List) getSnapshot(); final List<?> sn = (List<?>) getSnapshot();
final Iterator olditer = sn.iterator(); final Iterator<?> olditer = sn.iterator();
int i = 0; int i = 0;
while ( olditer.hasNext() ) { while ( olditer.hasNext() ) {
final Object old = olditer.next(); final Object old = olditer.next();
final Iterator newiter = bag.iterator(); final Iterator<E> newiter = bag.iterator();
boolean found = false; boolean found = false;
if ( bag.size() > i && elementType.isSame( old, bag.get( i++ ) ) ) { if ( bag.size() > i && elementType.isSame( old, bag.get( i++ ) ) ) {
//a shortcut if its location didn't change! //a shortcut if its location didn't change!
@ -349,7 +341,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
@Override @Override
public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException { public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException {
final List sn = (List) getSnapshot(); final List<?> sn = (List<?>) getSnapshot();
if ( sn.size() > i && elemType.isSame( sn.get( i ), entry ) ) { if ( sn.size() > i && elemType.isSame( sn.get( i ), entry ) ) {
//a shortcut if its location didn't change! //a shortcut if its location didn't change!
return false; return false;
@ -393,9 +385,9 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
public Iterator iterator() { public Iterator<E> iterator() {
read(); read();
return new IteratorProxy( bag.iterator() ); return new IteratorProxy<>( bag.iterator() );
} }
@Override @Override
@ -405,14 +397,13 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
public Object[] toArray(Object[] a) { public <A> A[] toArray(A[] a) {
read(); read();
return bag.toArray( a ); return bag.toArray( a );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean add(E object) {
public boolean add(Object object) {
if ( !isOperationQueueEnabled() ) { if ( !isOperationQueueEnabled() ) {
write(); write();
return bag.add( object ); return bag.add( object );
@ -437,15 +428,13 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean containsAll(Collection<?> c) {
public boolean containsAll(Collection c) {
read(); read();
return bag.containsAll( c ); return bag.containsAll( c );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean addAll(Collection<? extends E> values) {
public boolean addAll(Collection values) {
if ( values.size() == 0 ) { if ( values.size() == 0 ) {
return false; return false;
} }
@ -454,7 +443,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
return bag.addAll( values ); return bag.addAll( values );
} }
else { else {
for ( Object value : values ) { for ( E value : values ) {
queueOperation( new SimpleAdd( value ) ); queueOperation( new SimpleAdd( value ) );
} }
return values.size() > 0; return values.size() > 0;
@ -462,8 +451,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean removeAll(Collection<?> c) {
public boolean removeAll(Collection c) {
if ( c.size() > 0 ) { if ( c.size() > 0 ) {
initialize( true ); initialize( true );
if ( bag.removeAll( c ) ) { if ( bag.removeAll( c ) ) {
@ -481,8 +469,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean retainAll(Collection<?> c) {
public boolean retainAll(Collection c) {
initialize( true ); initialize( true );
if ( bag.retainAll( c ) ) { if ( bag.retainAll( c ) ) {
dirty(); dirty();
@ -494,7 +481,6 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked")
public void clear() { public void clear() {
if ( isClearQueueEnabled() ) { if ( isClearQueueEnabled() ) {
queueOperation( new Clear() ); queueOperation( new Clear() );
@ -520,7 +506,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
@Override @Override
public Object getSnapshotElement(Object entry, int i) { public Object getSnapshotElement(Object entry, int i) {
final List sn = (List) getSnapshot(); final List<?> sn = (List<?>) getSnapshot();
return sn.get( i ); return sn.get( i );
} }
@ -534,7 +520,7 @@ public class PersistentBag extends AbstractPersistentCollection implements List
@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
public int occurrences(Object o) { public int occurrences(Object o) {
read(); read();
final Iterator itr = bag.iterator(); final Iterator<E> itr = bag.iterator();
int result = 0; int result = 0;
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
if ( o.equals( itr.next() ) ) { if ( o.equals( itr.next() ) ) {
@ -547,15 +533,13 @@ public class PersistentBag extends AbstractPersistentCollection implements List
// List OPERATIONS: // List OPERATIONS:
@Override @Override
@SuppressWarnings("unchecked") public void add(int i, E o) {
public void add(int i, Object o) {
write(); write();
bag.add( i, o ); bag.add( i, o );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean addAll(int i, Collection<? extends E> c) {
public boolean addAll(int i, Collection c) {
if ( c.size() > 0 ) { if ( c.size() > 0 ) {
write(); write();
return bag.addAll( i, c ); return bag.addAll( i, c );
@ -566,57 +550,49 @@ public class PersistentBag extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked") public E get(int i) {
public Object get(int i) {
read(); read();
return bag.get( i ); return bag.get( i );
} }
@Override @Override
@SuppressWarnings("unchecked")
public int indexOf(Object o) { public int indexOf(Object o) {
read(); read();
return bag.indexOf( o ); return bag.indexOf( o );
} }
@Override @Override
@SuppressWarnings("unchecked")
public int lastIndexOf(Object o) { public int lastIndexOf(Object o) {
read(); read();
return bag.lastIndexOf( o ); return bag.lastIndexOf( o );
} }
@Override @Override
@SuppressWarnings("unchecked") public ListIterator<E> listIterator() {
public ListIterator listIterator() {
read(); read();
return new ListIteratorProxy( bag.listIterator() ); return new ListIteratorProxy( bag.listIterator() );
} }
@Override @Override
@SuppressWarnings("unchecked") public ListIterator<E> listIterator(int i) {
public ListIterator listIterator(int i) {
read(); read();
return new ListIteratorProxy( bag.listIterator( i ) ); return new ListIteratorProxy( bag.listIterator( i ) );
} }
@Override @Override
@SuppressWarnings("unchecked") public E remove(int i) {
public Object remove(int i) {
write(); write();
return bag.remove( i ); return bag.remove( i );
} }
@Override @Override
@SuppressWarnings("unchecked") public E set(int i, E o) {
public Object set(int i, Object o) {
write(); write();
return bag.set( i, o ); return bag.set( i, o );
} }
@Override @Override
@SuppressWarnings("unchecked") public List<E> subList(int start, int end) {
public List subList(int start, int end) {
read(); read();
return new ListProxy( bag.subList( start, end ) ); return new ListProxy( bag.subList( start, end ) );
} }
@ -652,31 +628,30 @@ public class PersistentBag extends AbstractPersistentCollection implements List
return super.hashCode(); return super.hashCode();
} }
final class Clear implements DelayedOperation { final class Clear implements DelayedOperation<E> {
@Override @Override
public void operate() { public void operate() {
bag.clear(); bag.clear();
} }
@Override @Override
public Object getAddedInstance() { public E getAddedInstance() {
return null; return null;
} }
@Override @Override
public Object getOrphan() { public E getOrphan() {
throw new UnsupportedOperationException( "queued clear cannot be used with orphan delete" ); throw new UnsupportedOperationException( "queued clear cannot be used with orphan delete" );
} }
} }
final class SimpleAdd extends AbstractValueDelayedOperation { final class SimpleAdd extends AbstractValueDelayedOperation {
public SimpleAdd(Object addedValue) { public SimpleAdd(E addedValue) {
super( addedValue, null ); super( addedValue, null );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
bag.add( getAddedInstance() ); bag.add( getAddedInstance() );
} }

View File

@ -35,14 +35,14 @@ import org.hibernate.type.Type;
* *
* @author Gavin King * @author Gavin King
*/ */
public class PersistentIdentifierBag extends AbstractPersistentCollection implements List { public class PersistentIdentifierBag<E> extends AbstractPersistentCollection<E> implements List<E> {
protected List<Object> values; protected List<E> values;
protected Map<Integer, Object> identifiers; protected Map<Integer, Object> identifiers;
/** /**
* The Collection provided to a PersistentIdentifierBag constructor * The Collection provided to a PersistentIdentifierBag constructor
*/ */
private Collection providedValues; private Collection<E> providedValues;
/** /**
* Constructs a PersistentIdentifierBag. This form needed for SOAP libraries, etc * Constructs a PersistentIdentifierBag. This form needed for SOAP libraries, etc
@ -77,12 +77,11 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
* @param session The session * @param session The session
* @param coll The base elements * @param coll The base elements
*/ */
@SuppressWarnings("unchecked") public PersistentIdentifierBag(SharedSessionContractImplementor session, Collection<E> coll) {
public PersistentIdentifierBag(SharedSessionContractImplementor session, Collection coll) {
super( session ); super( session );
providedValues = coll; providedValues = coll;
if (coll instanceof List) { if (coll instanceof List) {
values = (List<Object>) coll; values = (List<E>) coll;
} }
else { else {
values = new ArrayList<>( coll ); values = new ArrayList<>( coll );
@ -100,7 +99,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
* @deprecated {@link #PersistentIdentifierBag(SharedSessionContractImplementor, Collection)} should be used instead. * @deprecated {@link #PersistentIdentifierBag(SharedSessionContractImplementor, Collection)} should be used instead.
*/ */
@Deprecated @Deprecated
public PersistentIdentifierBag(SessionImplementor session, Collection coll) { public PersistentIdentifierBag(SessionImplementor session, Collection<E> coll) {
this( (SharedSessionContractImplementor) session, coll ); this( (SharedSessionContractImplementor) session, coll );
} }
@ -123,7 +122,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
(i/2), (i/2),
persister.getIdentifierType().assemble( array[i], getSession(), owner ) persister.getIdentifierType().assemble( array[i], getSession(), owner )
); );
values.add( persister.getElementType().assemble( array[i+1], getSession(), owner ) ); values.add( (E) persister.getElementType().assemble( array[i+1], getSession(), owner ) );
} }
} }
@ -143,7 +142,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public boolean add(Object o) { public boolean add(E o) {
write(); write();
values.add( o ); values.add( o );
return true; return true;
@ -166,7 +165,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public boolean containsAll(Collection c) { public boolean containsAll(Collection<?> c) {
read(); read();
return values.containsAll( c ); return values.containsAll( c );
} }
@ -177,9 +176,9 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public Iterator iterator() { public Iterator<E> iterator() {
read(); read();
return new IteratorProxy( values.iterator() ); return new IteratorProxy<>( values.iterator() );
} }
@Override @Override
@ -215,7 +214,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public boolean retainAll(Collection c) { public boolean retainAll(Collection<?> c) {
initialize( true ); initialize( true );
if ( values.retainAll( c ) ) { if ( values.retainAll( c ) ) {
dirty(); dirty();
@ -238,7 +237,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public Object[] toArray(Object[] a) { public <A> A[] toArray(A[] a) {
read(); read();
return values.toArray( a ); return values.toArray( a );
} }
@ -261,7 +260,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public Iterator entries(CollectionPersister persister) { public Iterator<E> entries(CollectionPersister persister) {
return values.iterator(); return values.iterator();
} }
@ -273,7 +272,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
@Override @Override
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
final Type elementType = persister.getElementType(); final Type elementType = persister.getElementType();
final Map snap = (Map) getSnapshot(); final Map<?,?> snap = (Map<?,?>) getSnapshot();
if ( snap.size()!= values.size() ) { if ( snap.size()!= values.size() ) {
return false; return false;
} }
@ -293,14 +292,13 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
@Override @Override
public boolean isSnapshotEmpty(Serializable snapshot) { public boolean isSnapshotEmpty(Serializable snapshot) {
return ( (Map) snapshot ).isEmpty(); return ( (Map<?,?>) snapshot ).isEmpty();
} }
@Override @Override
@SuppressWarnings("unchecked") public Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException { final Map<?,?> snap = (Map<?,?>) getSnapshot();
final Map snap = (Map) getSnapshot(); final List<Object> deletes = new ArrayList<>( snap.keySet() );
final List deletes = new ArrayList( snap.keySet() );
for ( int i=0; i<values.size(); i++ ) { for ( int i=0; i<values.size(); i++ ) {
if ( values.get( i ) != null ) { if ( values.get( i ) != null ) {
deletes.remove( identifiers.get( i ) ); deletes.remove( identifiers.get( i ) );
@ -321,7 +319,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
@Override @Override
public Object getSnapshotElement(Object entry, int i) { public Object getSnapshotElement(Object entry, int i) {
final Map snap = (Map) getSnapshot(); final Map<?,?> snap = (Map<?,?>) getSnapshot();
final Object id = identifiers.get( i ); final Object id = identifiers.get( i );
return snap.get( id ); return snap.get( id );
} }
@ -329,7 +327,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
@Override @Override
public boolean needsInserting(Object entry, int i, Type elemType) public boolean needsInserting(Object entry, int i, Type elemType)
throws HibernateException { throws HibernateException {
final Map snap = (Map) getSnapshot(); final Map<?,?> snap = (Map<?,?>) getSnapshot();
final Object id = identifiers.get( i ); final Object id = identifiers.get( i );
return entry != null return entry != null
&& ( id==null || snap.get( id )==null ); && ( id==null || snap.get( id )==null );
@ -341,7 +339,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
return false; return false;
} }
final Map snap = (Map) getSnapshot(); final Map<?,?> snap = (Map<?,?>) getSnapshot();
final Object id = identifiers.get( i ); final Object id = identifiers.get( i );
if ( id == null ) { if ( id == null ) {
return false; return false;
@ -352,24 +350,23 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
@SuppressWarnings("unchecked")
public Serializable getSnapshot(CollectionPersister persister) throws HibernateException { public Serializable getSnapshot(CollectionPersister persister) throws HibernateException {
final HashMap map = CollectionHelper.mapOfSize( values.size() ); final HashMap<Object,E> map = CollectionHelper.mapOfSize( values.size() );
final Iterator iter = values.iterator(); final Iterator<E> iter = values.iterator();
int i=0; int i=0;
while ( iter.hasNext() ) { while ( iter.hasNext() ) {
final Object value = iter.next(); final Object value = iter.next();
map.put( map.put(
identifiers.get( i++ ), identifiers.get( i++ ),
persister.getElementType().deepCopy( value, persister.getFactory() ) (E) persister.getElementType().deepCopy( value, persister.getFactory() )
); );
} }
return map; return map;
} }
@Override @Override
public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { public Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException {
final Map sn = (Map) snapshot; final Map<Object,E> sn = (Map<Object,E>) snapshot;
return getOrphans( sn.values(), values, entityName, getSession() ); return getOrphans( sn.values(), values, entityName, getSession() );
} }
@ -383,10 +380,10 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
@Override @Override
public void preInsert(CollectionPersister persister) throws HibernateException { public void preInsert(CollectionPersister persister) throws HibernateException {
final Iterator itr = values.iterator(); final Iterator<E> itr = values.iterator();
int i = 0; int i = 0;
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
final Object entry = itr.next(); final E entry = itr.next();
final Integer loc = i++; final Integer loc = i++;
if ( !identifiers.containsKey( loc ) ) { if ( !identifiers.containsKey( loc ) ) {
//TODO: native ids //TODO: native ids
@ -397,16 +394,16 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public void add(int index, Object element) { public void add(int index, E element) {
write(); write();
beforeAdd( index ); beforeAdd( index );
values.add( index, element ); values.add( index, element );
} }
@Override @Override
public boolean addAll(int index, Collection c) { public boolean addAll(int index, Collection<? extends E> c) {
if ( c.size() > 0 ) { if ( c.size() > 0 ) {
for ( Object element : c ) { for ( E element : c ) {
add( index++, element ); add( index++, element );
} }
return true; return true;
@ -417,7 +414,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public Object get(int index) { public E get(int index) {
read(); read();
return values.get( index ); return values.get( index );
} }
@ -435,13 +432,13 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public ListIterator listIterator() { public ListIterator<E> listIterator() {
read(); read();
return new ListIteratorProxy( values.listIterator() ); return new ListIteratorProxy( values.listIterator() );
} }
@Override @Override
public ListIterator listIterator(int index) { public ListIterator<E> listIterator(int index) {
read(); read();
return new ListIteratorProxy( values.listIterator( index ) ); return new ListIteratorProxy( values.listIterator( index ) );
} }
@ -469,26 +466,26 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
} }
@Override @Override
public Object remove(int index) { public E remove(int index) {
write(); write();
beforeRemove( index ); beforeRemove( index );
return values.remove( index ); return values.remove( index );
} }
@Override @Override
public Object set(int index, Object element) { public E set(int index, E element) {
write(); write();
return values.set( index, element ); return values.set( index, element );
} }
@Override @Override
public List subList(int fromIndex, int toIndex) { public List<E> subList(int fromIndex, int toIndex) {
read(); read();
return new ListProxy( values.subList( fromIndex, toIndex ) ); return new ListProxy( values.subList( fromIndex, toIndex ) );
} }
@Override @Override
public boolean addAll(Collection c) { public boolean addAll(Collection<? extends E> c) {
if ( c.size()> 0 ) { if ( c.size()> 0 ) {
write(); write();
return values.addAll( c ); return values.addAll( c );
@ -506,7 +503,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
//TODO: if we are using identity columns, fetch the identifier //TODO: if we are using identity columns, fetch the identifier
} }
public void injectLoadedState(PluralAttributeMapping attributeMapping, List loadingState) { public void injectLoadedState(PluralAttributeMapping attributeMapping, List<?> loadingState) {
assert identifiers == null; assert identifiers == null;
assert values == null; assert values == null;
@ -516,7 +513,7 @@ public class PersistentIdentifierBag extends AbstractPersistentCollection implem
for ( int i = 0; i < loadingState.size(); i++ ) { for ( int i = 0; i < loadingState.size(); i++ ) {
final Object[] row = (Object[]) loadingState.get( i ); final Object[] row = (Object[]) loadingState.get( i );
final Object identifier = row[0]; final Object identifier = row[0];
final Object element = row[1]; final E element = (E) row[1];
Object old = identifiers.put( values.size(), identifier ); Object old = identifiers.put( values.size(), identifier );
if ( old == null ) { if ( old == null ) {

View File

@ -27,8 +27,8 @@ import org.hibernate.type.Type;
* @see java.util.ArrayList * @see java.util.ArrayList
* @author Gavin King * @author Gavin King
*/ */
public class PersistentList extends AbstractPersistentCollection implements List { public class PersistentList<E> extends AbstractPersistentCollection<E> implements List<E> {
protected List list; protected List<E> list;
/** /**
* Constructs a PersistentList. This form needed for SOAP libraries, etc * Constructs a PersistentList. This form needed for SOAP libraries, etc
@ -62,7 +62,7 @@ public class PersistentList extends AbstractPersistentCollection implements List
* @param session The session * @param session The session
* @param list The raw list * @param list The raw list
*/ */
public PersistentList(SharedSessionContractImplementor session, List list) { public PersistentList(SharedSessionContractImplementor session, List<E> list) {
super( session ); super( session );
this.list = list; this.list = list;
setInitialized(); setInitialized();
@ -77,14 +77,13 @@ public class PersistentList extends AbstractPersistentCollection implements List
* @deprecated {@link #PersistentList(SharedSessionContractImplementor, List)} should be used instead. * @deprecated {@link #PersistentList(SharedSessionContractImplementor, List)} should be used instead.
*/ */
@Deprecated @Deprecated
public PersistentList(SessionImplementor session, List list) { public PersistentList(SessionImplementor session, List<E> list) {
this( (SharedSessionContractImplementor) session, list ); this( (SharedSessionContractImplementor) session, list );
} }
@Override @Override
@SuppressWarnings( {"unchecked"})
public Serializable getSnapshot(CollectionPersister persister) throws HibernateException { public Serializable getSnapshot(CollectionPersister persister) throws HibernateException {
final ArrayList clonedList = new ArrayList( list.size() ); final ArrayList<Object> clonedList = new ArrayList<>( list.size() );
for ( Object element : list ) { for ( Object element : list ) {
final Object deepCopy = persister.getElementType().deepCopy( element, persister.getFactory() ); final Object deepCopy = persister.getElementType().deepCopy( element, persister.getFactory() );
clonedList.add( deepCopy ); clonedList.add( deepCopy );
@ -93,27 +92,27 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { public Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException {
final List sn = (List) snapshot; return getOrphans( (List<E>) snapshot, list, entityName, getSession() );
return getOrphans( sn, list, entityName, getSession() );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void initializeEmptyCollection(CollectionPersister persister) { public void initializeEmptyCollection(CollectionPersister persister) {
assert list == null; assert list == null;
list = (List) persister.getCollectionType().instantiate( 0 ); list = (List<E>) persister.getCollectionType().instantiate( 0 );
endRead(); endRead();
} }
@Override @Override
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
final Type elementType = persister.getElementType(); final Type elementType = persister.getElementType();
final List sn = (List) getSnapshot(); final List<?> sn = (List<?>) getSnapshot();
if ( sn.size() != this.list.size() ) { if ( sn.size() != this.list.size() ) {
return false; return false;
} }
final Iterator itr = list.iterator(); final Iterator<?> itr = list.iterator();
final Iterator snapshotItr = sn.iterator(); final Iterator<?> snapshotItr = sn.iterator();
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
if ( elementType.isDirty( itr.next(), snapshotItr.next(), getSession() ) ) { if ( elementType.isDirty( itr.next(), snapshotItr.next(), getSession() ) ) {
return false; return false;
@ -124,7 +123,7 @@ public class PersistentList extends AbstractPersistentCollection implements List
@Override @Override
public boolean isSnapshotEmpty(Serializable snapshot) { public boolean isSnapshotEmpty(Serializable snapshot) {
return ( (Collection) snapshot ).isEmpty(); return ( (Collection<?>) snapshot ).isEmpty();
} }
@Override @Override
@ -135,27 +134,26 @@ public class PersistentList extends AbstractPersistentCollection implements List
final int size = array.length; final int size = array.length;
assert list == null; assert list == null;
this.list = (List) persister.getCollectionType().instantiate( size ); this.list = (List<E>) persister.getCollectionType().instantiate( size );
for ( Serializable arrayElement : array ) { for ( Serializable arrayElement : array ) {
list.add( persister.getElementType().assemble( arrayElement, getSession(), owner ) ); list.add( (E) persister.getElementType().assemble( arrayElement, getSession(), owner ) );
} }
} }
@Override @Override
public void injectLoadedState(PluralAttributeMapping attributeMapping, List loadingStateList) { public void injectLoadedState(PluralAttributeMapping attributeMapping, List<?> loadingStateList) {
assert isInitializing(); assert isInitializing();
assert list == null; assert list == null;
final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor(); final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor();
this.list = (List) collectionDescriptor.getCollectionSemantics().instantiateRaw( this.list = (List<E>) collectionDescriptor.getCollectionSemantics().instantiateRaw(
loadingStateList.size(), loadingStateList.size(),
collectionDescriptor collectionDescriptor
); );
//noinspection unchecked list.addAll( (List<E>) loadingStateList );
list.addAll( loadingStateList );
} }
@Override @Override
@ -182,9 +180,9 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
public Iterator iterator() { public Iterator<E> iterator() {
read(); read();
return new IteratorProxy( list.iterator() ); return new IteratorProxy<>( list.iterator() );
} }
@Override @Override
@ -194,14 +192,13 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
public Object[] toArray(Object[] array) { public <A> A[] toArray(A[] array) {
read(); read();
return list.toArray( array ); return list.toArray( array );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean add(E object) {
public boolean add(Object object) {
if ( !isOperationQueueEnabled() ) { if ( !isOperationQueueEnabled() ) {
write(); write();
return list.add( object ); return list.add( object );
@ -228,7 +225,7 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
else if ( exists ) { else if ( exists ) {
elementRemoved = true; elementRemoved = true;
queueOperation( new SimpleRemove( value ) ); queueOperation( new SimpleRemove( (E) value ) );
return true; return true;
} }
else { else {
@ -237,15 +234,13 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
public boolean containsAll(Collection coll) { public boolean containsAll(Collection<?> coll) {
read(); read();
//noinspection unchecked
return list.containsAll( coll ); return list.containsAll( coll );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean addAll(Collection<? extends E> values) {
public boolean addAll(Collection values) {
if ( values.size() == 0 ) { if ( values.size() == 0 ) {
return false; return false;
} }
@ -254,7 +249,7 @@ public class PersistentList extends AbstractPersistentCollection implements List
return list.addAll( values ); return list.addAll( values );
} }
else { else {
for ( Object value : values ) { for ( E value : values ) {
queueOperation( new SimpleAdd( value ) ); queueOperation( new SimpleAdd( value ) );
} }
return values.size() > 0; return values.size() > 0;
@ -262,8 +257,7 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean addAll(int index, Collection<? extends E> coll) {
public boolean addAll(int index, Collection coll) {
if ( coll.size() > 0 ) { if ( coll.size() > 0 ) {
write(); write();
return list.addAll( index, coll ); return list.addAll( index, coll );
@ -274,8 +268,7 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean removeAll(Collection<?> coll) {
public boolean removeAll(Collection coll) {
if ( coll.size() > 0 ) { if ( coll.size() > 0 ) {
initialize( true ); initialize( true );
if ( list.removeAll( coll ) ) { if ( list.removeAll( coll ) ) {
@ -293,9 +286,8 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
public boolean retainAll(Collection coll) { public boolean retainAll(Collection<?> coll) {
initialize( true ); initialize( true );
//noinspection unchecked
if ( list.retainAll( coll ) ) { if ( list.retainAll( coll ) ) {
dirty(); dirty();
return true; return true;
@ -320,16 +312,16 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
public Object get(int index) { public E get(int index) {
if ( index < 0 ) { if ( index < 0 ) {
throw new ArrayIndexOutOfBoundsException( "negative index" ); throw new ArrayIndexOutOfBoundsException( "negative index" );
} }
final Object result = readElementByIndex( index ); final Object result = readElementByIndex( index );
return result == UNKNOWN ? list.get( index ) : result; return result == UNKNOWN ? list.get( index ) : (E) result;
} }
@Override @Override
public Object set(int index, Object value) { public E set(int index, E value) {
if (index<0) { if (index<0) {
throw new ArrayIndexOutOfBoundsException("negative index"); throw new ArrayIndexOutOfBoundsException("negative index");
} }
@ -338,17 +330,16 @@ public class PersistentList extends AbstractPersistentCollection implements List
if ( old==UNKNOWN ) { if ( old==UNKNOWN ) {
write(); write();
//noinspection unchecked
return list.set( index, value ); return list.set( index, value );
} }
else { else {
queueOperation( new Set( index, value, old ) ); queueOperation( new Set( index, value, (E) old ) );
return old; return (E) old;
} }
} }
@Override @Override
public Object remove(int index) { public E remove(int index) {
if ( index < 0 ) { if ( index < 0 ) {
throw new ArrayIndexOutOfBoundsException( "negative index" ); throw new ArrayIndexOutOfBoundsException( "negative index" );
} }
@ -360,18 +351,17 @@ public class PersistentList extends AbstractPersistentCollection implements List
return list.remove( index ); return list.remove( index );
} }
else { else {
queueOperation( new Remove( index, old ) ); queueOperation( new Remove( index, (E) old ) );
return old; return (E) old;
} }
} }
@Override @Override
public void add(int index, Object value) { public void add(int index, E value) {
if ( index < 0 ) { if ( index < 0 ) {
throw new ArrayIndexOutOfBoundsException( "negative index" ); throw new ArrayIndexOutOfBoundsException( "negative index" );
} }
write(); write();
//noinspection unchecked
list.add( index, value ); list.add( index, value );
} }
@ -388,19 +378,19 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
public ListIterator listIterator() { public ListIterator<E> listIterator() {
read(); read();
return new ListIteratorProxy( list.listIterator() ); return new ListIteratorProxy( list.listIterator() );
} }
@Override @Override
public ListIterator listIterator(int index) { public ListIterator<E> listIterator(int index) {
read(); read();
return new ListIteratorProxy( list.listIterator( index ) ); return new ListIteratorProxy( list.listIterator( index ) );
} }
@Override @Override
public java.util.List subList(int from, int to) { public java.util.List<E> subList(int from, int to) {
read(); read();
return new ListProxy( list.subList( from, to ) ); return new ListProxy( list.subList( from, to ) );
} }
@ -417,7 +407,7 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
public Iterator entries(CollectionPersister persister) { public Iterator<E> entries(CollectionPersister persister) {
return list.iterator(); return list.iterator();
} }
@ -432,10 +422,9 @@ public class PersistentList extends AbstractPersistentCollection implements List
} }
@Override @Override
@SuppressWarnings("unchecked") public Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException { final List<Object> deletes = new ArrayList<>();
final List deletes = new ArrayList(); final List<?> sn = (List<?>) getSnapshot();
final List sn = (List) getSnapshot();
int end; int end;
if ( sn.size() > list.size() ) { if ( sn.size() > list.size() ) {
for ( int i=list.size(); i<sn.size(); i++ ) { for ( int i=list.size(); i<sn.size(); i++ ) {
@ -458,13 +447,13 @@ public class PersistentList extends AbstractPersistentCollection implements List
@Override @Override
public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException { public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException {
final List sn = (List) getSnapshot(); final List<?> sn = (List<?>) getSnapshot();
return list.get( i ) != null && ( i >= sn.size() || sn.get( i ) == null ); return list.get( i ) != null && ( i >= sn.size() || sn.get( i ) == null );
} }
@Override @Override
public boolean needsUpdating(Object entry, int i, Type elemType) throws HibernateException { public boolean needsUpdating(Object entry, int i, Type elemType) throws HibernateException {
final List sn = (List) getSnapshot(); final List<?> sn = (List<?>) getSnapshot();
return i < sn.size() return i < sn.size()
&& sn.get( i ) != null && sn.get( i ) != null
&& list.get( i ) != null && list.get( i ) != null
@ -483,7 +472,7 @@ public class PersistentList extends AbstractPersistentCollection implements List
@Override @Override
public Object getSnapshotElement(Object entry, int i) { public Object getSnapshotElement(Object entry, int i) {
final List sn = (List) getSnapshot(); final List<?> sn = (List<?>) getSnapshot();
return sn.get( i ); return sn.get( i );
} }
@ -505,40 +494,39 @@ public class PersistentList extends AbstractPersistentCollection implements List
return entry!=null; return entry!=null;
} }
final class Clear implements DelayedOperation { final class Clear implements DelayedOperation<E> {
@Override @Override
public void operate() { public void operate() {
list.clear(); list.clear();
} }
@Override @Override
public Object getAddedInstance() { public E getAddedInstance() {
return null; return null;
} }
@Override @Override
public Object getOrphan() { public E getOrphan() {
throw new UnsupportedOperationException( "queued clear cannot be used with orphan delete" ); throw new UnsupportedOperationException( "queued clear cannot be used with orphan delete" );
} }
} }
final class SimpleAdd extends AbstractValueDelayedOperation { final class SimpleAdd extends AbstractValueDelayedOperation {
public SimpleAdd(Object addedValue) { public SimpleAdd(E addedValue) {
super( addedValue, null ); super( addedValue, null );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
list.add( getAddedInstance() ); list.add( getAddedInstance() );
} }
} }
abstract class AbstractListValueDelayedOperation extends AbstractValueDelayedOperation { abstract class AbstractListValueDelayedOperation extends AbstractValueDelayedOperation {
private int index; private final int index;
AbstractListValueDelayedOperation(Integer index, Object addedValue, Object orphan) { AbstractListValueDelayedOperation(Integer index, E addedValue, E orphan) {
super( addedValue, orphan ); super( addedValue, orphan );
this.index = index; this.index = index;
} }
@ -550,12 +538,11 @@ public class PersistentList extends AbstractPersistentCollection implements List
final class Add extends AbstractListValueDelayedOperation { final class Add extends AbstractListValueDelayedOperation {
public Add(int index, Object addedValue) { public Add(int index, E addedValue) {
super( index, addedValue, null ); super( index, addedValue, null );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
list.add( getIndex(), getAddedInstance() ); list.add( getIndex(), getAddedInstance() );
} }
@ -563,12 +550,11 @@ public class PersistentList extends AbstractPersistentCollection implements List
final class Set extends AbstractListValueDelayedOperation { final class Set extends AbstractListValueDelayedOperation {
public Set(int index, Object addedValue, Object orphan) { public Set(int index, E addedValue, E orphan) {
super( index, addedValue, orphan ); super( index, addedValue, orphan );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
list.set( getIndex(), getAddedInstance() ); list.set( getIndex(), getAddedInstance() );
} }
@ -576,12 +562,11 @@ public class PersistentList extends AbstractPersistentCollection implements List
final class Remove extends AbstractListValueDelayedOperation { final class Remove extends AbstractListValueDelayedOperation {
public Remove(int index, Object orphan) { public Remove(int index, E orphan) {
super( index, null, orphan ); super( index, null, orphan );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
list.remove( getIndex() ); list.remove( getIndex() );
} }
@ -589,12 +574,11 @@ public class PersistentList extends AbstractPersistentCollection implements List
final class SimpleRemove extends AbstractValueDelayedOperation { final class SimpleRemove extends AbstractValueDelayedOperation {
public SimpleRemove(Object orphan) { public SimpleRemove(E orphan) {
super( null, orphan ); super( null, orphan );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
list.remove( getOrphan() ); list.remove( getOrphan() );
} }

View File

@ -31,9 +31,9 @@ import org.hibernate.type.Type;
* @see java.util.HashMap * @see java.util.HashMap
* @author Gavin King * @author Gavin King
*/ */
public class PersistentMap extends AbstractPersistentCollection implements Map { public class PersistentMap<K,E> extends AbstractPersistentCollection<E> implements Map<K,E> {
protected Map map; protected Map<K,E> map;
/** /**
* Empty constructor. * Empty constructor.
@ -72,7 +72,7 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
* @param session The session to which this map will belong. * @param session The session to which this map will belong.
* @param map The underlying map data. * @param map The underlying map data.
*/ */
public PersistentMap(SharedSessionContractImplementor session, Map map) { public PersistentMap(SharedSessionContractImplementor session, Map<K,E> map) {
super( session ); super( session );
this.map = map; this.map = map;
setInitialized(); setInitialized();
@ -88,45 +88,42 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
* @deprecated {@link #PersistentMap(SharedSessionContractImplementor, Map)} should be used instead. * @deprecated {@link #PersistentMap(SharedSessionContractImplementor, Map)} should be used instead.
*/ */
@Deprecated @Deprecated
public PersistentMap(SessionImplementor session, Map map) { public PersistentMap(SessionImplementor session, Map<K,E> map) {
this( (SharedSessionContractImplementor) session, map ); this( (SharedSessionContractImplementor) session, map );
} }
@Override @Override
@SuppressWarnings( {"unchecked"})
public Serializable getSnapshot(CollectionPersister persister) throws HibernateException { public Serializable getSnapshot(CollectionPersister persister) throws HibernateException {
final HashMap clonedMap = CollectionHelper.mapOfSize( map.size() ); final HashMap<K,E> clonedMap = CollectionHelper.mapOfSize( map.size() );
for ( Object o : map.entrySet() ) { for ( Entry<K,E> e : map.entrySet() ) {
final Entry e = (Entry) o; final E copy = (E) persister.getElementType().deepCopy( e.getValue(), persister.getFactory() );
final Object copy = persister.getElementType().deepCopy( e.getValue(), persister.getFactory() );
clonedMap.put( e.getKey(), copy ); clonedMap.put( e.getKey(), copy );
} }
return clonedMap; return clonedMap;
} }
@Override @Override
public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { public Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException {
final Map sn = (Map) snapshot; final Map<K,E> sn = (Map<K,E>) snapshot;
return getOrphans( sn.values(), map.values(), entityName, getSession() ); return getOrphans( sn.values(), map.values(), entityName, getSession() );
} }
@Override @Override
public void initializeEmptyCollection(CollectionPersister persister) { public void initializeEmptyCollection(CollectionPersister persister) {
assert map == null; assert map == null;
map = (Map) persister.getCollectionType().instantiate( 0 ); map = (Map<K,E>) persister.getCollectionType().instantiate( 0 );
endRead(); endRead();
} }
@Override @Override
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
final Type elementType = persister.getElementType(); final Type elementType = persister.getElementType();
final Map snapshotMap = (Map) getSnapshot(); final Map<?,?> snapshotMap = (Map<?,?>) getSnapshot();
if ( snapshotMap.size() != this.map.size() ) { if ( snapshotMap.size() != this.map.size() ) {
return false; return false;
} }
for ( Object o : map.entrySet() ) { for ( Entry<?,?> entry : map.entrySet() ) {
final Entry entry = (Entry) o;
if ( elementType.isDirty( entry.getValue(), snapshotMap.get( entry.getKey() ), getSession() ) ) { if ( elementType.isDirty( entry.getValue(), snapshotMap.get( entry.getKey() ), getSession() ) ) {
return false; return false;
} }
@ -136,7 +133,7 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
@Override @Override
public boolean isSnapshotEmpty(Serializable snapshot) { public boolean isSnapshotEmpty(Serializable snapshot) {
return ( (Map) snapshot ).isEmpty(); return ( (Map<?,?>) snapshot ).isEmpty();
} }
@Override @Override
@ -169,25 +166,24 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
} }
@Override @Override
public Object get(Object key) { public E get(Object key) {
final Object result = readElementByIndex( key ); final Object result = readElementByIndex( key );
return result == UNKNOWN return result == UNKNOWN
? map.get( key ) ? map.get( key )
: result; : (E) result;
} }
@Override @Override
public Object put(Object key, Object value) { public E put(K key, E value) {
if ( isPutQueueEnabled() ) { if ( isPutQueueEnabled() ) {
final Object old = readElementByIndex( key ); final Object old = readElementByIndex( key );
if ( old != UNKNOWN ) { if ( old != UNKNOWN ) {
queueOperation( new Put( key, value, old ) ); queueOperation( new Put( key, value, (E) old ) );
return old; return (E) old;
} }
} }
initialize( true ); initialize( true );
//noinspection unchecked final E old = map.put( key, value );
final Object old = map.put( key, value );
// would be better to use the element-type to determine // would be better to use the element-type to determine
// whether the old and the new are equal here; the problem being // whether the old and the new are equal here; the problem being
// we do not necessarily have access to the element type in all // we do not necessarily have access to the element type in all
@ -199,13 +195,13 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
} }
@Override @Override
public Object remove(Object key) { public E remove(Object key) {
if ( isPutQueueEnabled() ) { if ( isPutQueueEnabled() ) {
final Object old = readElementByIndex( key ); final Object old = readElementByIndex( key );
if ( old != UNKNOWN ) { if ( old != UNKNOWN ) {
elementRemoved = true; elementRemoved = true;
queueOperation( new Remove( key, old ) ); queueOperation( new Remove( (K) key, (E) old ) );
return old; return (E) old;
} }
} }
// TODO : safe to interpret "map.remove(key) == null" as non-dirty? // TODO : safe to interpret "map.remove(key) == null" as non-dirty?
@ -218,11 +214,10 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
} }
@Override @Override
public void putAll(Map puts) { public void putAll(Map<? extends K,? extends E> puts) {
if ( puts.size() > 0 ) { if ( puts.size() > 0 ) {
initialize( true ); initialize( true );
for ( Object o : puts.entrySet() ) { for ( Entry<? extends K,? extends E> entry : puts.entrySet() ) {
final Entry entry = (Entry) o;
put( entry.getKey(), entry.getValue() ); put( entry.getKey(), entry.getValue() );
} }
} }
@ -243,19 +238,19 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
} }
@Override @Override
public Set keySet() { public Set<K> keySet() {
read(); read();
return new SetProxy( map.keySet() ); return new SetProxy<>( map.keySet() );
} }
@Override @Override
public Collection values() { public Collection<E> values() {
read(); read();
return new SetProxy( map.values() ); return new SetProxy<>( map.values() );
} }
@Override @Override
public Set entrySet() { public Set<Entry<K,E>> entrySet() {
read(); read();
return new EntrySetProxy( map.entrySet() ); return new EntrySetProxy( map.entrySet() );
} }
@ -272,37 +267,35 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
} }
@Override @Override
public Iterator entries(CollectionPersister persister) { public Iterator<Entry<K,E>> entries(CollectionPersister persister) {
return map.entrySet().iterator(); return map.entrySet().iterator();
} }
public void injectLoadedState(PluralAttributeMapping attributeMapping, List loadingState) { public void injectLoadedState(PluralAttributeMapping attributeMapping, List<?> loadingState) {
assert isInitializing(); assert isInitializing();
assert map == null; assert map == null;
final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor(); final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor();
this.map = (Map) collectionDescriptor.getCollectionSemantics().instantiateRaw( loadingState.size(), collectionDescriptor ); this.map = (Map<K,E>) collectionDescriptor.getCollectionSemantics().instantiateRaw( loadingState.size(), collectionDescriptor );
for ( int i = 0; i < loadingState.size(); i++ ) { for ( int i = 0; i < loadingState.size(); i++ ) {
final Object[] keyVal = (Object[]) loadingState.get( i ); final Object[] keyVal = (Object[]) loadingState.get( i );
//noinspection unchecked map.put( (K) keyVal[0], (E) keyVal[1] );
map.put( keyVal[0], keyVal[1] );
} }
} }
@Override @Override
@SuppressWarnings("unchecked")
public void initializeFromCache(CollectionPersister persister, Object disassembled, Object owner) public void initializeFromCache(CollectionPersister persister, Object disassembled, Object owner)
throws HibernateException { throws HibernateException {
final Serializable[] array = (Serializable[]) disassembled; final Serializable[] array = (Serializable[]) disassembled;
final int size = array.length; final int size = array.length;
this.map = (Map) persister.getCollectionSemantics().instantiateRaw( size, persister ); this.map = (Map<K,E>) persister.getCollectionSemantics().instantiateRaw( size, persister );
for ( int i = 0; i < size; i+=2 ) { for ( int i = 0; i < size; i+=2 ) {
map.put( map.put(
persister.getIndexType().assemble( array[i], getSession(), owner ), (K) persister.getIndexType().assemble( array[i], getSession(), owner ),
persister.getElementType().assemble( array[i+1], getSession(), owner ) (E) persister.getElementType().assemble( array[i+1], getSession(), owner )
); );
} }
} }
@ -310,10 +303,10 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
@Override @Override
public Object disassemble(CollectionPersister persister) throws HibernateException { public Object disassemble(CollectionPersister persister) throws HibernateException {
final Object[] result = new Object[ map.size() * 2 ]; final Object[] result = new Object[ map.size() * 2 ];
final Iterator itr = map.entrySet().iterator(); final Iterator<Entry<K,E>> itr = map.entrySet().iterator();
int i=0; int i=0;
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
final Map.Entry e = (Map.Entry) itr.next(); final Map.Entry<K,E> e = itr.next();
result[i++] = persister.getIndexType().disassemble( e.getKey(), getSession(), null ); result[i++] = persister.getIndexType().disassemble( e.getKey(), getSession(), null );
result[i++] = persister.getElementType().disassemble( e.getValue(), getSession(), null ); result[i++] = persister.getElementType().disassemble( e.getValue(), getSession(), null );
} }
@ -324,80 +317,69 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
/** /**
* a wrapper for Map.Entry sets * a wrapper for Map.Entry sets
*/ */
class EntrySetProxy implements Set { class EntrySetProxy implements Set<Entry<K,E>> {
private final Set set; private final Set<Entry<K,E>> set;
EntrySetProxy(Set set) { EntrySetProxy(Set<Entry<K,E>> set) {
this.set=set; this.set=set;
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean add(Entry<K,E> entry) {
public boolean add(Object entry) {
//write(); -- doesn't //write(); -- doesn't
return set.add( entry ); return set.add( entry );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean addAll(Collection<? extends Entry<K,E>> entries) {
public boolean addAll(Collection entries) {
//write(); -- doesn't //write(); -- doesn't
return set.addAll( entries ); return set.addAll( entries );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void clear() { public void clear() {
write(); write();
set.clear(); set.clear();
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean contains(Object entry) { public boolean contains(Object entry) {
return set.contains( entry ); return set.contains( entry );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean containsAll(Collection<?> entries) {
public boolean containsAll(Collection entries) {
return set.containsAll( entries ); return set.containsAll( entries );
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean isEmpty() { public boolean isEmpty() {
return set.isEmpty(); return set.isEmpty();
} }
@Override @Override
@SuppressWarnings("unchecked") public Iterator<Entry<K,E>> iterator() {
public Iterator iterator() {
return new EntryIteratorProxy( set.iterator() ); return new EntryIteratorProxy( set.iterator() );
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean remove(Object entry) { public boolean remove(Object entry) {
write(); write();
return set.remove( entry ); return set.remove( entry );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean removeAll(Collection<?> entries) {
public boolean removeAll(Collection entries) {
write(); write();
return set.removeAll( entries ); return set.removeAll( entries );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean retainAll(Collection<?> entries) {
public boolean retainAll(Collection entries) {
write(); write();
return set.retainAll( entries ); return set.retainAll( entries );
} }
@Override @Override
@SuppressWarnings("unchecked")
public int size() { public int size() {
return set.size(); return set.size();
} }
@ -406,89 +388,78 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
// uses iterator() to fill the array // uses iterator() to fill the array
@Override @Override
@SuppressWarnings("unchecked")
public Object[] toArray() { public Object[] toArray() {
return set.toArray(); return set.toArray();
} }
@Override @Override
@SuppressWarnings("unchecked") public <A> A[] toArray(A[] array) {
public Object[] toArray(Object[] array) {
return set.toArray( array ); return set.toArray( array );
} }
} }
final class EntryIteratorProxy implements Iterator { final class EntryIteratorProxy implements Iterator<Entry<K,E>> {
private final Iterator iter; private final Iterator<Entry<K,E>> iter;
EntryIteratorProxy(Iterator iter) { EntryIteratorProxy(Iterator<Entry<K,E>> iter) {
this.iter=iter; this.iter=iter;
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean hasNext() { public boolean hasNext() {
return iter.hasNext(); return iter.hasNext();
} }
@Override @Override
@SuppressWarnings("unchecked") public Entry<K,E> next() {
public Object next() { return new MapEntryProxy( iter.next() );
return new MapEntryProxy( (Map.Entry) iter.next() );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void remove() { public void remove() {
write(); write();
iter.remove(); iter.remove();
} }
} }
final class MapEntryProxy implements Map.Entry { final class MapEntryProxy implements Map.Entry<K,E> {
private final Map.Entry me; private final Map.Entry<K,E> me;
MapEntryProxy( Map.Entry me ) { MapEntryProxy(Map.Entry<K,E> me) {
this.me = me; this.me = me;
} }
@Override @Override
@SuppressWarnings("unchecked") public K getKey() {
public Object getKey() {
return me.getKey(); return me.getKey();
} }
@Override @Override
@SuppressWarnings("unchecked") public E getValue() {
public Object getValue() {
return me.getValue(); return me.getValue();
} }
@Override @Override
@SuppressWarnings({"unchecked", "EqualsWhichDoesntCheckParameterClass"}) @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
public boolean equals(Object o) { public boolean equals(Object o) {
return me.equals( o ); return me.equals( o );
} }
@Override @Override
@SuppressWarnings("unchecked")
public int hashCode() { public int hashCode() {
return me.hashCode(); return me.hashCode();
} }
// finally, what it's all about... // finally, what it's all about...
@Override @Override
@SuppressWarnings("unchecked") public E setValue(E value) {
public Object setValue(Object value) {
write(); write();
return me.setValue( value ); return me.setValue( value );
} }
} }
@Override @Override
@SuppressWarnings("unchecked") public Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException { final List<Object> deletes = new ArrayList<>();
final List deletes = new ArrayList(); for ( Entry<?,?> e : ((Map<?,?>) getSnapshot()).entrySet() ) {
for ( Object o : ((Map) getSnapshot()).entrySet() ) {
final Entry e = (Entry) o;
final Object key = e.getKey(); final Object key = e.getKey();
if ( e.getValue() != null && map.get( key ) == null ) { if ( e.getValue() != null && map.get( key ) == null ) {
deletes.add( indexIsFormula ? e.getValue() : key ); deletes.add( indexIsFormula ? e.getValue() : key );
@ -498,18 +469,16 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException { public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException {
final Map sn = (Map) getSnapshot(); final Map<?,?> sn = (Map<?,?>) getSnapshot();
final Map.Entry e = (Map.Entry) entry; final Map.Entry<?,?> e = (Map.Entry<?,?>) entry;
return e.getValue() != null && sn.get( e.getKey() ) == null; return e.getValue() != null && sn.get( e.getKey() ) == null;
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean needsUpdating(Object entry, int i, Type elemType) throws HibernateException { public boolean needsUpdating(Object entry, int i, Type elemType) throws HibernateException {
final Map sn = (Map) getSnapshot(); final Map<?,?> sn = (Map<?,?>) getSnapshot();
final Map.Entry e = (Map.Entry) entry; final Map.Entry<?,?> e = (Map.Entry<?,?>) entry;
final Object snValue = sn.get( e.getKey() ); final Object snValue = sn.get( e.getKey() );
return e.getValue() != null return e.getValue() != null
&& snValue != null && snValue != null
@ -517,82 +486,76 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
} }
@Override @Override
@SuppressWarnings("unchecked")
public Object getIndex(Object entry, int i, CollectionPersister persister) { public Object getIndex(Object entry, int i, CollectionPersister persister) {
return ( (Map.Entry) entry ).getKey(); return ( (Map.Entry<?,?>) entry ).getKey();
} }
@Override @Override
@SuppressWarnings("unchecked")
public Object getElement(Object entry) { public Object getElement(Object entry) {
return ( (Map.Entry) entry ).getValue(); return ( (Map.Entry<?,?>) entry ).getValue();
} }
@Override @Override
@SuppressWarnings("unchecked")
public Object getSnapshotElement(Object entry, int i) { public Object getSnapshotElement(Object entry, int i) {
final Map sn = (Map) getSnapshot(); final Map<?,?> sn = (Map<?,?>) getSnapshot();
return sn.get( ( (Map.Entry) entry ).getKey() ); return sn.get( ( (Map.Entry<?,?>) entry ).getKey() );
} }
@Override @Override
@SuppressWarnings({"unchecked", "EqualsWhichDoesntCheckParameterClass"}) @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
public boolean equals(Object other) { public boolean equals(Object other) {
read(); read();
return map.equals( other ); return map.equals( other );
} }
@Override @Override
@SuppressWarnings("unchecked")
public int hashCode() { public int hashCode() {
read(); read();
return map.hashCode(); return map.hashCode();
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean entryExists(Object entry, int i) { public boolean entryExists(Object entry, int i) {
return ( (Map.Entry) entry ).getValue() != null; return ( (Map.Entry<?,?>) entry ).getValue() != null;
} }
final class Clear implements DelayedOperation { final class Clear implements DelayedOperation<E> {
@Override @Override
public void operate() { public void operate() {
map.clear(); map.clear();
} }
@Override @Override
public Object getAddedInstance() { public E getAddedInstance() {
return null; return null;
} }
@Override @Override
public Object getOrphan() { public E getOrphan() {
throw new UnsupportedOperationException( "queued clear cannot be used with orphan delete" ); throw new UnsupportedOperationException( "queued clear cannot be used with orphan delete" );
} }
} }
abstract class AbstractMapValueDelayedOperation extends AbstractValueDelayedOperation { abstract class AbstractMapValueDelayedOperation extends AbstractValueDelayedOperation {
private Object index; private final K index;
protected AbstractMapValueDelayedOperation(Object index, Object addedValue, Object orphan) { protected AbstractMapValueDelayedOperation(K index, E addedValue, E orphan) {
super( addedValue, orphan ); super( addedValue, orphan );
this.index = index; this.index = index;
} }
protected final Object getIndex() { protected final K getIndex() {
return index; return index;
} }
} }
final class Put extends AbstractMapValueDelayedOperation { final class Put extends AbstractMapValueDelayedOperation {
public Put(Object index, Object addedValue, Object orphan) { public Put(K index, E addedValue, E orphan) {
super( index, addedValue, orphan ); super( index, addedValue, orphan );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
map.put( getIndex(), getAddedInstance() ); map.put( getIndex(), getAddedInstance() );
} }
@ -600,12 +563,11 @@ public class PersistentMap extends AbstractPersistentCollection implements Map {
final class Remove extends AbstractMapValueDelayedOperation { final class Remove extends AbstractMapValueDelayedOperation {
public Remove(Object index, Object orphan) { public Remove(K index, E orphan) {
super( index, null, orphan ); super( index, null, orphan );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
map.remove( getIndex() ); map.remove( getIndex() );
} }

View File

@ -30,8 +30,8 @@ import org.hibernate.type.Type;
* @see java.util.HashSet * @see java.util.HashSet
* @author Gavin King * @author Gavin King
*/ */
public class PersistentSet extends AbstractPersistentCollection implements java.util.Set { public class PersistentSet<E> extends AbstractPersistentCollection<E> implements java.util.Set<E> {
protected Set set; protected Set<E> set;
/** /**
* Empty constructor. * Empty constructor.
@ -71,7 +71,7 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
* @param session The session to which this set will belong. * @param session The session to which this set will belong.
* @param set The underlying set data. * @param set The underlying set data.
*/ */
public PersistentSet(SharedSessionContractImplementor session, java.util.Set set) { public PersistentSet(SharedSessionContractImplementor session, java.util.Set<E> set) {
super( session ); super( session );
// Sets can be just a view of a part of another collection. // Sets can be just a view of a part of another collection.
// do we need to copy it to be sure it won't be changing // do we need to copy it to be sure it won't be changing
@ -91,38 +91,37 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
* @deprecated {@link #PersistentSet(SharedSessionContractImplementor, java.util.Set)} should be used instead. * @deprecated {@link #PersistentSet(SharedSessionContractImplementor, java.util.Set)} should be used instead.
*/ */
@Deprecated @Deprecated
public PersistentSet(SessionImplementor session, java.util.Set set) { public PersistentSet(SessionImplementor session, java.util.Set<E> set) {
this( (SharedSessionContractImplementor) session, set ); this( (SharedSessionContractImplementor) session, set );
} }
@Override @Override
@SuppressWarnings( {"unchecked"})
public Serializable getSnapshot(CollectionPersister persister) throws HibernateException { public Serializable getSnapshot(CollectionPersister persister) throws HibernateException {
final HashMap clonedSet = CollectionHelper.mapOfSize( set.size() ); final HashMap<E,E> clonedSet = CollectionHelper.mapOfSize( set.size() );
for ( Object aSet : set ) { for ( E aSet : set ) {
final Object copied = persister.getElementType().deepCopy( aSet, persister.getFactory() ); final E copied = (E) persister.getElementType().deepCopy( aSet, persister.getFactory() );
clonedSet.put( copied, copied ); clonedSet.put( copied, copied );
} }
return clonedSet; return clonedSet;
} }
@Override @Override
public Collection getOrphans(Serializable snapshot, String entityName) throws HibernateException { public Collection<E> getOrphans(Serializable snapshot, String entityName) throws HibernateException {
final java.util.Map sn = (java.util.Map) snapshot; final java.util.Map<E,E> sn = (java.util.Map<E,E>) snapshot;
return getOrphans( sn.keySet(), set, entityName, getSession() ); return getOrphans( sn.keySet(), set, entityName, getSession() );
} }
@Override @Override
public void initializeEmptyCollection(CollectionPersister persister) { public void initializeEmptyCollection(CollectionPersister persister) {
assert set == null; assert set == null;
set = (Set) persister.getCollectionType().instantiate( 0 ); set = (Set<E>) persister.getCollectionType().instantiate( 0 );
endRead(); endRead();
} }
@Override @Override
public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException { public boolean equalsSnapshot(CollectionPersister persister) throws HibernateException {
final Type elementType = persister.getElementType(); final Type elementType = persister.getElementType();
final java.util.Map sn = (java.util.Map) getSnapshot(); final java.util.Map<?,?> sn = (java.util.Map<?,?>) getSnapshot();
if ( sn.size()!=set.size() ) { if ( sn.size()!=set.size() ) {
return false; return false;
} }
@ -139,22 +138,21 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
@Override @Override
public boolean isSnapshotEmpty(Serializable snapshot) { public boolean isSnapshotEmpty(Serializable snapshot) {
return ( (java.util.Map) snapshot ).isEmpty(); return ( (java.util.Map<?,?>) snapshot ).isEmpty();
} }
@Override @Override
@SuppressWarnings("unchecked")
public void initializeFromCache(CollectionPersister persister, Object disassembled, Object owner) public void initializeFromCache(CollectionPersister persister, Object disassembled, Object owner)
throws HibernateException { throws HibernateException {
final Serializable[] array = (Serializable[]) disassembled; final Serializable[] array = (Serializable[]) disassembled;
final int size = array.length; final int size = array.length;
this.set = (Set) persister.getCollectionSemantics().instantiateRaw( size, persister ); this.set = (Set<E>) persister.getCollectionSemantics().instantiateRaw( size, persister );
for ( Serializable arrayElement : array ) { for ( Serializable arrayElement : array ) {
final Object assembledArrayElement = persister.getElementType().assemble( arrayElement, getSession(), owner ); final Object assembledArrayElement = persister.getElementType().assemble( arrayElement, getSession(), owner );
if ( assembledArrayElement != null ) { if ( assembledArrayElement != null ) {
set.add( assembledArrayElement ); set.add( (E) assembledArrayElement );
} }
} }
} }
@ -165,13 +163,11 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
@Override @Override
@SuppressWarnings("unchecked")
public int size() { public int size() {
return readSize() ? getCachedSize() : set.size(); return readSize() ? getCachedSize() : set.size();
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean isEmpty() { public boolean isEmpty() {
return readSize() ? getCachedSize()==0 : set.isEmpty(); return readSize() ? getCachedSize()==0 : set.isEmpty();
} }
@ -185,28 +181,25 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
@Override @Override
@SuppressWarnings("unchecked") public Iterator<E> iterator() {
public Iterator iterator() {
read(); read();
return new IteratorProxy( set.iterator() ); return new IteratorProxy<>( set.iterator() );
} }
@Override @Override
@SuppressWarnings("unchecked")
public Object[] toArray() { public Object[] toArray() {
read(); read();
return set.toArray(); return set.toArray();
} }
@Override @Override
@SuppressWarnings("unchecked") public <A> A[] toArray(A[] array) {
public Object[] toArray(Object[] array) {
read(); read();
return set.toArray( array ); return set.toArray( array );
} }
@Override @Override
public boolean add(Object value) { public boolean add(E value) {
final Boolean exists = isOperationQueueEnabled() ? readElementExistence( value ) : null; final Boolean exists = isOperationQueueEnabled() ? readElementExistence( value ) : null;
if ( exists == null ) { if ( exists == null ) {
initialize( true ); initialize( true );
@ -243,7 +236,7 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
else if ( exists ) { else if ( exists ) {
elementRemoved = true; elementRemoved = true;
queueOperation( new SimpleRemove( value ) ); queueOperation( new SimpleRemove( (E) value ) );
return true; return true;
} }
else { else {
@ -252,15 +245,13 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean containsAll(Collection<?> coll) {
public boolean containsAll(Collection coll) {
read(); read();
return set.containsAll( coll ); return set.containsAll( coll );
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean addAll(Collection<? extends E> coll) {
public boolean addAll(Collection coll) {
if ( coll.size() > 0 ) { if ( coll.size() > 0 ) {
initialize( true ); initialize( true );
if ( set.addAll( coll ) ) { if ( set.addAll( coll ) ) {
@ -277,8 +268,7 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean retainAll(Collection<?> coll) {
public boolean retainAll(Collection coll) {
initialize( true ); initialize( true );
if ( set.retainAll( coll ) ) { if ( set.retainAll( coll ) ) {
dirty(); dirty();
@ -290,8 +280,7 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
@Override @Override
@SuppressWarnings("unchecked") public boolean removeAll(Collection<?> coll) {
public boolean removeAll(Collection coll) {
if ( coll.size() > 0 ) { if ( coll.size() > 0 ) {
initialize( true ); initialize( true );
if ( set.removeAll( coll ) ) { if ( set.removeAll( coll ) ) {
@ -330,19 +319,18 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
public void injectLoadedState( public void injectLoadedState(
PluralAttributeMapping attributeMapping, PluralAttributeMapping attributeMapping,
List loadingStateList) { List<?> loadingStateList) {
final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor(); final CollectionPersister collectionDescriptor = attributeMapping.getCollectionDescriptor();
if ( loadingStateList != null ) { if ( loadingStateList != null ) {
this.set = (Set) attributeMapping.getCollectionDescriptor().getCollectionSemantics().instantiateRaw( this.set = (Set<E>) attributeMapping.getCollectionDescriptor().getCollectionSemantics().instantiateRaw(
loadingStateList.size(), loadingStateList.size(),
collectionDescriptor collectionDescriptor
); );
//noinspection unchecked this.set.addAll( (List<E>) loadingStateList );
this.set.addAll( loadingStateList );
} }
else { else {
this.set = (Set) attributeMapping.getCollectionDescriptor().getCollectionSemantics().instantiateRaw( this.set = (Set<E>) attributeMapping.getCollectionDescriptor().getCollectionSemantics().instantiateRaw(
0, 0,
collectionDescriptor collectionDescriptor
); );
@ -350,14 +338,14 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
@Override @Override
public Iterator entries(CollectionPersister persister) { public Iterator<E> entries(CollectionPersister persister) {
return set.iterator(); return set.iterator();
} }
@Override @Override
public Object disassemble(CollectionPersister persister) throws HibernateException { public Object disassemble(CollectionPersister persister) throws HibernateException {
final Object[] result = new Object[ set.size() ]; final Object[] result = new Object[ set.size() ];
final Iterator itr = set.iterator(); final Iterator<E> itr = set.iterator();
int i=0; int i=0;
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
result[i++] = persister.getElementType().disassemble( itr.next(), getSession(), null ); result[i++] = persister.getElementType().disassemble( itr.next(), getSession(), null );
@ -366,13 +354,12 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
@Override @Override
@SuppressWarnings("unchecked") public Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
public Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula) throws HibernateException {
final Type elementType = persister.getElementType(); final Type elementType = persister.getElementType();
final java.util.Map sn = (java.util.Map) getSnapshot(); final java.util.Map<?,?> sn = (java.util.Map<?,?>) getSnapshot();
final ArrayList deletes = new ArrayList( sn.size() ); final ArrayList<Object> deletes = new ArrayList<>( sn.size() );
Iterator itr = sn.keySet().iterator(); Iterator<?> itr = sn.keySet().iterator();
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
final Object test = itr.next(); final Object test = itr.next();
if ( !set.contains( test ) ) { if ( !set.contains( test ) ) {
@ -395,96 +382,87 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException { public boolean needsInserting(Object entry, int i, Type elemType) throws HibernateException {
final Object oldValue = ( (java.util.Map) getSnapshot() ).get( entry ); final Object oldValue = ( (java.util.Map<?,?>) getSnapshot() ).get( entry );
// note that it might be better to iterate the snapshot but this is safe, // note that it might be better to iterate the snapshot but this is safe,
// assuming the user implements equals() properly, as required by the Set // assuming the user implements equals() properly, as required by the Set
// contract! // contract!
return ( oldValue == null && entry != null ) || elemType.isDirty( oldValue, entry, getSession() ); return oldValue == null && entry != null
|| elemType.isDirty( oldValue, entry, getSession() );
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean needsUpdating(Object entry, int i, Type elemType) { public boolean needsUpdating(Object entry, int i, Type elemType) {
return false; return false;
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean isRowUpdatePossible() { public boolean isRowUpdatePossible() {
return false; return false;
} }
@Override @Override
@SuppressWarnings("unchecked")
public Object getIndex(Object entry, int i, CollectionPersister persister) { public Object getIndex(Object entry, int i, CollectionPersister persister) {
throw new UnsupportedOperationException("Sets don't have indexes"); throw new UnsupportedOperationException("Sets don't have indexes");
} }
@Override @Override
@SuppressWarnings("unchecked")
public Object getElement(Object entry) { public Object getElement(Object entry) {
return entry; return entry;
} }
@Override @Override
@SuppressWarnings("unchecked")
public Object getSnapshotElement(Object entry, int i) { public Object getSnapshotElement(Object entry, int i) {
throw new UnsupportedOperationException("Sets don't support updating by element"); throw new UnsupportedOperationException("Sets don't support updating by element");
} }
@Override @Override
@SuppressWarnings({"unchecked", "EqualsWhichDoesntCheckParameterClass"}) @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
public boolean equals(Object other) { public boolean equals(Object other) {
read(); read();
return set.equals( other ); return set.equals( other );
} }
@Override @Override
@SuppressWarnings("unchecked")
public int hashCode() { public int hashCode() {
read(); read();
return set.hashCode(); return set.hashCode();
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean entryExists(Object key, int i) { public boolean entryExists(Object key, int i) {
return key != null; return key != null;
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean isWrapper(Object collection) { public boolean isWrapper(Object collection) {
return set==collection; return set==collection;
} }
final class Clear implements DelayedOperation { final class Clear implements DelayedOperation<E> {
@Override @Override
public void operate() { public void operate() {
set.clear(); set.clear();
} }
@Override @Override
public Object getAddedInstance() { public E getAddedInstance() {
return null; return null;
} }
@Override @Override
public Object getOrphan() { public E getOrphan() {
throw new UnsupportedOperationException("queued clear cannot be used with orphan delete"); throw new UnsupportedOperationException("queued clear cannot be used with orphan delete");
} }
} }
final class SimpleAdd extends AbstractValueDelayedOperation { final class SimpleAdd extends AbstractValueDelayedOperation {
public SimpleAdd(Object addedValue) { public SimpleAdd(E addedValue) {
super( addedValue, null ); super( addedValue, null );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
set.add( getAddedInstance() ); set.add( getAddedInstance() );
} }
@ -492,12 +470,11 @@ public class PersistentSet extends AbstractPersistentCollection implements java.
final class SimpleRemove extends AbstractValueDelayedOperation { final class SimpleRemove extends AbstractValueDelayedOperation {
public SimpleRemove(Object orphan) { public SimpleRemove(E orphan) {
super( null, orphan ); super( null, orphan );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void operate() { public void operate() {
set.remove( getOrphan() ); set.remove( getOrphan() );
} }

View File

@ -27,8 +27,8 @@ import org.hibernate.persister.collection.BasicCollectionPersister;
* @see java.util.TreeMap * @see java.util.TreeMap
* @author <a href="mailto:doug.currie@alum.mit.edu">e</a> * @author <a href="mailto:doug.currie@alum.mit.edu">e</a>
*/ */
public class PersistentSortedMap extends PersistentMap implements SortedMap { public class PersistentSortedMap<K,E> extends PersistentMap<K,E> implements SortedMap<K,E> {
protected Comparator comparator; protected Comparator<? super K> comparator;
/** /**
* Constructs a PersistentSortedMap. This form needed for SOAP libraries, etc * Constructs a PersistentSortedMap. This form needed for SOAP libraries, etc
@ -63,7 +63,7 @@ public class PersistentSortedMap extends PersistentMap implements SortedMap {
* @param session The session * @param session The session
* @param map The underlying map data * @param map The underlying map data
*/ */
public PersistentSortedMap(SharedSessionContractImplementor session, SortedMap map) { public PersistentSortedMap(SharedSessionContractImplementor session, SortedMap<K,E> map) {
super( session, map ); super( session, map );
comparator = map.comparator(); comparator = map.comparator();
} }
@ -76,186 +76,162 @@ public class PersistentSortedMap extends PersistentMap implements SortedMap {
* @deprecated {@link #PersistentSortedMap(SharedSessionContractImplementor, SortedMap)} should be used instead. * @deprecated {@link #PersistentSortedMap(SharedSessionContractImplementor, SortedMap)} should be used instead.
*/ */
@Deprecated @Deprecated
public PersistentSortedMap(SessionImplementor session, SortedMap map) { public PersistentSortedMap(SessionImplementor session, SortedMap<K,E> map) {
this( (SharedSessionContractImplementor) session, map ); this( (SharedSessionContractImplementor) session, map );
} }
@SuppressWarnings({"unchecked", "UnusedParameters"}) @SuppressWarnings("UnusedParameters")
protected Serializable snapshot(BasicCollectionPersister persister, EntityMode entityMode) throws HibernateException { protected Serializable snapshot(BasicCollectionPersister persister, EntityMode entityMode) throws HibernateException {
final TreeMap clonedMap = new TreeMap( comparator ); final TreeMap<K,E> clonedMap = new TreeMap<>( comparator );
for ( Object o : map.entrySet() ) { for ( Entry<K,E> e : map.entrySet() ) {
final Entry e = (Entry) o; clonedMap.put( e.getKey(), (E) persister.getElementType().deepCopy( e.getValue(), persister.getFactory() ) );
clonedMap.put( e.getKey(), persister.getElementType().deepCopy( e.getValue(), persister.getFactory() ) );
} }
return clonedMap; return clonedMap;
} }
public void setComparator(Comparator comparator) { public void setComparator(Comparator<? super K> comparator) {
this.comparator = comparator; this.comparator = comparator;
} }
@Override @Override
public Comparator comparator() { public Comparator<? super K> comparator() {
return comparator; return comparator;
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedMap<K,E> subMap(K fromKey, K toKey) {
public SortedMap subMap(Object fromKey, Object toKey) {
read(); read();
final SortedMap subMap = ( (SortedMap) map ).subMap( fromKey, toKey ); final SortedMap<K,E> subMap = ( (SortedMap<K,E>) map ).subMap( fromKey, toKey );
return new SortedSubMap( subMap ); return new SortedSubMap( subMap );
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedMap<K,E> headMap(K toKey) {
public SortedMap headMap(Object toKey) {
read(); read();
final SortedMap headMap = ( (SortedMap) map ).headMap( toKey ); final SortedMap<K,E> headMap = ( (SortedMap<K,E>) map ).headMap( toKey );
return new SortedSubMap( headMap ); return new SortedSubMap( headMap );
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedMap<K,E> tailMap(K fromKey) {
public SortedMap tailMap(Object fromKey) {
read(); read();
final SortedMap tailMap = ( (SortedMap) map ).tailMap( fromKey ); final SortedMap<K,E> tailMap = ( (SortedMap<K,E>) map ).tailMap( fromKey );
return new SortedSubMap( tailMap ); return new SortedSubMap( tailMap );
} }
@Override @Override
@SuppressWarnings("unchecked") public K firstKey() {
public Object firstKey() {
read(); read();
return ( (SortedMap) map ).firstKey(); return ( (SortedMap<K,E>) map ).firstKey();
} }
@Override @Override
@SuppressWarnings("unchecked") public K lastKey() {
public Object lastKey() {
read(); read();
return ( (SortedMap) map ).lastKey(); return ( (SortedMap<K,E>) map ).lastKey();
} }
class SortedSubMap implements SortedMap { class SortedSubMap implements SortedMap<K,E> {
SortedMap subMap; SortedMap<K,E> subMap;
SortedSubMap(SortedMap subMap) { SortedSubMap(SortedMap<K,E> subMap) {
this.subMap = subMap; this.subMap = subMap;
} }
@Override @Override
@SuppressWarnings("unchecked")
public int size() { public int size() {
return subMap.size(); return subMap.size();
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean isEmpty() { public boolean isEmpty() {
return subMap.isEmpty(); return subMap.isEmpty();
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean containsKey(Object key) { public boolean containsKey(Object key) {
return subMap.containsKey( key ); return subMap.containsKey( key );
} }
@Override @Override
@SuppressWarnings("unchecked")
public boolean containsValue(Object key) { public boolean containsValue(Object key) {
return subMap.containsValue( key ) ; return subMap.containsValue( key ) ;
} }
@Override @Override
@SuppressWarnings("unchecked") public E get(Object key) {
public Object get(Object key) {
return subMap.get( key ); return subMap.get( key );
} }
@Override @Override
@SuppressWarnings("unchecked") public E put(K key, E value) {
public Object put(Object key, Object value) {
write(); write();
return subMap.put( key, value ); return subMap.put( key, value );
} }
@Override @Override
@SuppressWarnings("unchecked") public E remove(Object key) {
public Object remove(Object key) {
write(); write();
return subMap.remove( key ); return subMap.remove( key );
} }
@Override @Override
@SuppressWarnings("unchecked") public void putAll(Map<? extends K,? extends E> other) {
public void putAll(Map other) {
write(); write();
subMap.putAll( other ); subMap.putAll( other );
} }
@Override @Override
@SuppressWarnings("unchecked")
public void clear() { public void clear() {
write(); write();
subMap.clear(); subMap.clear();
} }
@Override @Override
@SuppressWarnings("unchecked") public Set<K> keySet() {
public Set keySet() { return new SetProxy<>( subMap.keySet() );
return new SetProxy( subMap.keySet() );
} }
@Override @Override
@SuppressWarnings("unchecked") public Collection<E> values() {
public Collection values() { return new SetProxy<>( subMap.values() );
return new SetProxy( subMap.values() );
} }
@Override @Override
@SuppressWarnings("unchecked") public Set<Entry<K,E>> entrySet() {
public Set entrySet() {
return new EntrySetProxy( subMap.entrySet() ); return new EntrySetProxy( subMap.entrySet() );
} }
@Override @Override
@SuppressWarnings("unchecked") public Comparator<? super K> comparator() {
public Comparator comparator() {
return subMap.comparator(); return subMap.comparator();
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedMap<K,E> subMap(K fromKey, K toKey) {
public SortedMap subMap(Object fromKey, Object toKey) { final SortedMap<K,E> subMap = this.subMap.subMap( fromKey, toKey );
final SortedMap subMap = this.subMap.subMap( fromKey, toKey );
return new SortedSubMap( subMap ); return new SortedSubMap( subMap );
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedMap<K,E> headMap(K toKey) {
public SortedMap headMap(Object toKey) { final SortedMap<K,E> headMap = subMap.headMap( toKey );
final SortedMap headMap = subMap.headMap( toKey );
return new SortedSubMap( headMap ); return new SortedSubMap( headMap );
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedMap<K,E> tailMap(K fromKey) {
public SortedMap tailMap(Object fromKey) { final SortedMap<K,E> tailMap = subMap.tailMap( fromKey );
final SortedMap tailMap = subMap.tailMap( fromKey );
return new SortedSubMap( tailMap ); return new SortedSubMap( tailMap );
} }
@Override @Override
@SuppressWarnings("unchecked") public K firstKey() {
public Object firstKey() {
return subMap.firstKey(); return subMap.firstKey();
} }
@Override @Override
@SuppressWarnings("unchecked") public K lastKey() {
public Object lastKey() {
return subMap.lastKey(); return subMap.lastKey();
} }
} }

View File

@ -24,8 +24,8 @@ import org.hibernate.persister.collection.BasicCollectionPersister;
* @see java.util.TreeSet * @see java.util.TreeSet
* @author <a href="mailto:doug.currie@alum.mit.edu">e</a> * @author <a href="mailto:doug.currie@alum.mit.edu">e</a>
*/ */
public class PersistentSortedSet extends PersistentSet implements SortedSet { public class PersistentSortedSet<E> extends PersistentSet<E> implements SortedSet<E> {
protected Comparator comparator; protected Comparator<? super E> comparator;
/** /**
* Constructs a PersistentSortedSet. This form needed for SOAP libraries, etc * Constructs a PersistentSortedSet. This form needed for SOAP libraries, etc
@ -60,7 +60,7 @@ public class PersistentSortedSet extends PersistentSet implements SortedSet {
* @param session The session * @param session The session
* @param set The underlying set data * @param set The underlying set data
*/ */
public PersistentSortedSet(SharedSessionContractImplementor session, SortedSet set) { public PersistentSortedSet(SharedSessionContractImplementor session, SortedSet<E> set) {
super( session, set ); super( session, set );
comparator = set.comparator(); comparator = set.comparator();
} }
@ -73,110 +73,99 @@ public class PersistentSortedSet extends PersistentSet implements SortedSet {
* @deprecated {@link #PersistentSortedSet(SharedSessionContractImplementor, SortedSet)} should be used instead. * @deprecated {@link #PersistentSortedSet(SharedSessionContractImplementor, SortedSet)} should be used instead.
*/ */
@Deprecated @Deprecated
public PersistentSortedSet(SessionImplementor session, SortedSet set) { public PersistentSortedSet(SessionImplementor session, SortedSet<E> set) {
this( (SharedSessionContractImplementor) session, set ); this( (SharedSessionContractImplementor) session, set );
} }
@SuppressWarnings({"unchecked", "UnusedParameters"}) @SuppressWarnings("UnusedParameters")
protected Serializable snapshot(BasicCollectionPersister persister, EntityMode entityMode) protected Serializable snapshot(BasicCollectionPersister persister, EntityMode entityMode)
throws HibernateException { throws HibernateException {
final TreeMap clonedSet = new TreeMap( comparator ); final TreeMap<E,E> clonedSet = new TreeMap<>( comparator );
for ( Object setElement : set ) { for ( E setElement : set ) {
final Object copy = persister.getElementType().deepCopy( setElement, persister.getFactory() ); final E copy = (E) persister.getElementType().deepCopy( setElement, persister.getFactory() );
clonedSet.put( copy, copy ); clonedSet.put( copy, copy );
} }
return clonedSet; return clonedSet;
} }
public void setComparator(Comparator comparator) { public void setComparator(Comparator<? super E> comparator) {
this.comparator = comparator; this.comparator = comparator;
} }
@Override @Override
public Comparator comparator() { public Comparator<? super E> comparator() {
return comparator; return comparator;
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedSet<E> subSet(E fromElement, E toElement) {
public SortedSet subSet(Object fromElement, Object toElement) {
read(); read();
final SortedSet subSet = ( (SortedSet) set ).subSet( fromElement, toElement ); final SortedSet<E> subSet = ( (SortedSet<E>) set ).subSet( fromElement, toElement );
return new SubSetProxy( subSet ); return new SubSetProxy( subSet );
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedSet<E> headSet(E toElement) {
public SortedSet headSet(Object toElement) {
read(); read();
final SortedSet headSet = ( (SortedSet) set ).headSet( toElement ); final SortedSet<E> headSet = ( (SortedSet<E>) set ).headSet( toElement );
return new SubSetProxy( headSet ); return new SubSetProxy( headSet );
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedSet<E> tailSet(E fromElement) {
public SortedSet tailSet(Object fromElement) {
read(); read();
final SortedSet tailSet = ( (SortedSet) set ).tailSet( fromElement ); final SortedSet<E> tailSet = ( (SortedSet<E>) set ).tailSet( fromElement );
return new SubSetProxy( tailSet ); return new SubSetProxy( tailSet );
} }
@Override @Override
@SuppressWarnings("unchecked") public E first() {
public Object first() {
read(); read();
return ( (SortedSet) set ).first(); return ( (SortedSet<E>) set ).first();
} }
@Override @Override
@SuppressWarnings("unchecked") public E last() {
public Object last() {
read(); read();
return ( (SortedSet) set ).last(); return ( (SortedSet<E>) set ).last();
} }
/** /**
* wrapper for subSets to propagate write to its backing set * wrapper for subSets to propagate write to its backing set
*/ */
class SubSetProxy extends SetProxy implements SortedSet { class SubSetProxy extends SetProxy<E> implements SortedSet<E> {
SubSetProxy(SortedSet s) { SubSetProxy(SortedSet<E> s) {
super( s ); super( s );
} }
@Override @Override
@SuppressWarnings("unchecked") public Comparator<? super E> comparator() {
public Comparator comparator() { return ( (SortedSet<E>) this.set ).comparator();
return ( (SortedSet) this.set ).comparator();
} }
@Override @Override
@SuppressWarnings("unchecked") public E first() {
public Object first() { return ( (SortedSet<E>) this.set ).first();
return ( (SortedSet) this.set ).first();
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedSet<E> headSet(E toValue) {
public SortedSet headSet(Object toValue) { return new SubSetProxy( ( (SortedSet<E>) this.set ).headSet( toValue ) );
return new SubSetProxy( ( (SortedSet) this.set ).headSet( toValue ) );
} }
@Override @Override
@SuppressWarnings("unchecked") public E last() {
public Object last() { return ( (SortedSet<E>) this.set ).last();
return ( (SortedSet) this.set ).last();
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedSet<E> subSet(E fromValue, E toValue) {
public SortedSet subSet(Object fromValue, Object toValue) { return new SubSetProxy( ( (SortedSet<E>) this.set ).subSet( fromValue, toValue ) );
return new SubSetProxy( ( (SortedSet) this.set ).subSet( fromValue, toValue ) );
} }
@Override @Override
@SuppressWarnings("unchecked") public SortedSet<E> tailSet(E fromValue) {
public SortedSet tailSet(Object fromValue) { return new SubSetProxy( ( (SortedSet<E>) this.set ).tailSet( fromValue ) );
return new SubSetProxy( ( (SortedSet) this.set ).tailSet( fromValue ) );
} }
} }
} }

View File

@ -44,7 +44,7 @@ import org.hibernate.type.Type;
* *
* @author Gavin King * @author Gavin King
*/ */
public interface PersistentCollection { public interface PersistentCollection<E> {
/** /**
* Get the owning entity. Note that the owner is only * Get the owning entity. Note that the owner is only
* set during the flush cycle, and when a new collection * set during the flush cycle, and when a new collection
@ -125,7 +125,7 @@ public interface PersistentCollection {
* *
* @return The iterator * @return The iterator
*/ */
Iterator entries(CollectionPersister persister); Iterator<?> entries(CollectionPersister persister);
/** /**
* Get the identifier of the given collection entry. This refers to the collection identifier, not the * Get the identifier of the given collection entry. This refers to the collection identifier, not the
@ -260,7 +260,7 @@ public interface PersistentCollection {
* *
* @return An iterator over the elements to delete * @return An iterator over the elements to delete
*/ */
Iterator getDeletes(CollectionPersister persister, boolean indexIsFormula); Iterator<?> getDeletes(CollectionPersister persister, boolean indexIsFormula);
/** /**
* Is this the wrapper for the given collection instance? * Is this the wrapper for the given collection instance?
@ -306,7 +306,7 @@ public interface PersistentCollection {
/** /**
* Inject the state loaded for a collection instance. * Inject the state loaded for a collection instance.
*/ */
void injectLoadedState(PluralAttributeMapping attributeMapping, List loadingState); void injectLoadedState(PluralAttributeMapping attributeMapping, List<?> loadingState);
/** /**
* Called after reading all rows from the JDBC result set. Pairs with {@link #beginRead} * Called after reading all rows from the JDBC result set. Pairs with {@link #beginRead}
@ -342,7 +342,7 @@ public interface PersistentCollection {
* *
* @return The iterator * @return The iterator
*/ */
Iterator queuedAdditionIterator(); Iterator<E> queuedAdditionIterator();
/** /**
* Get the "queued" orphans * Get the "queued" orphans
@ -351,7 +351,7 @@ public interface PersistentCollection {
* *
* @return The orphaned elements * @return The orphaned elements
*/ */
Collection getQueuedOrphans(String entityName); Collection<E> getQueuedOrphans(String entityName);
/** /**
* Get the current collection key value * Get the current collection key value
@ -446,7 +446,7 @@ public interface PersistentCollection {
* *
* @return The orphans * @return The orphans
*/ */
Collection getOrphans(Serializable snapshot, String entityName); Collection<E> getOrphans(Serializable snapshot, String entityName);
void initializeEmptyCollection(CollectionPersister persister); void initializeEmptyCollection(CollectionPersister persister);

View File

@ -1541,7 +1541,7 @@ public abstract class AbstractCollectionPersister
final Expectation expectation = Expectations.appropriateExpectation( getDeleteCheckStyle() ); final Expectation expectation = Expectations.appropriateExpectation( getDeleteCheckStyle() );
try { try {
// delete all the deleted entries // delete all the deleted entries
Iterator deletes = collection.getDeletes( this, !deleteByIndex ); Iterator<?> deletes = collection.getDeletes( this, !deleteByIndex );
if ( deletes.hasNext() ) { if ( deletes.hasNext() ) {
int offset = 1; int offset = 1;
int count = 0; int count = 0;