org.eclipse.persistence.jpa.jpql.utility.iterable.SnapshotCloneListIterable Maven / Gradle / Ivy
Show all versions of eclipselink Show documentation
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation
//
package org.eclipse.persistence.jpa.jpql.utility.iterable;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import org.eclipse.persistence.jpa.jpql.utility.iterator.CloneListIterator;
/**
* A SnapshotCloneListIterable
returns a list iterator on a
* "snapshot" of a list, allowing for concurrent access to the original list.
* A copy of the list is created when the list iterable is constructed.
* As a result, the contents of the list will be the same with
* every call to {@link #iterator()}, even if the original list is modified via
* the list iterator's mutation methods.
*
* The original list passed to the SnapshotCloneListIterable
's
* constructor should be thread-safe (e.g. {@link java.util.Vector});
* otherwise you run the risk of a corrupted list.
*
* By default, the list iterator returned by a SnapshotCloneListIterable
does not
* support the {@link ListIterator} mutation operations; this is because it does not
* have access to the original list. But if the SnapshotCloneListIterable
* is supplied with a {@link CloneListIterator.Mutator}
* it will delegate the
* {@link ListIterator} mutation operations to the Mutator
.
* Alternatively, a subclass can override the list iterable's mutation
* methods.
*
* This list iterable is useful for multiple passes over a list that should not
* be changed (e.g. by another thread) between passes.
*
* @param the type of elements returned by the list iterable's list iterator
*
* @see CloneListIterator
* @see LiveCloneListIterable
* @see SnapshotCloneListIterable
*/
public class SnapshotCloneListIterable
extends CloneListIterable
{
private final Object[] array;
// ********** constructors **********
/**
* Construct a "snapshot" list iterable for the specified list.
* The {@link ListIterator} modify operations will not be supported
* by the list iterator returned by {@link #iterator()}
* unless a subclass overrides the list iterable's modify
* method.
*/
public SnapshotCloneListIterable(List list) {
super();
this.array = list.toArray();
}
/**
* Construct a "snapshot" list iterable for the specified list.
* The specified mutator will be used by any generated list iterators to
* modify the original list.
*/
public SnapshotCloneListIterable(List list, CloneListIterator.Mutator mutator) {
super(mutator);
this.array = list.toArray();
}
// ********** ListIterable implementation **********
public ListIterator iterator() {
return new LocalCloneListIterator(this.mutator, this.array);
}
@Override
public String toString() {
return Arrays.toString(this.array);
}
// ********** clone iterator **********
/**
* provide access to "internal" constructor
*/
protected static class LocalCloneListIterator extends CloneListIterator {
protected LocalCloneListIterator(Mutator mutator, Object[] array) {
super(mutator, array);
}
}
}