All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.persistence.jpa.jpql.tools.utility.iterable.SnapshotCloneIterable Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
/*
 * Copyright (c) 2009, 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.tools.utility.iterable;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import org.eclipse.persistence.jpa.jpql.tools.utility.iterator.CloneIterator;
import org.eclipse.persistence.jpa.jpql.utility.CollectionTools;

/**
 * A SnapshotCloneIterable returns an iterator on a "snapshot" of a
 * collection, allowing for concurrent access to the original collection. A
 * copy of the collection is created when the iterable is constructed.
 * As a result, the contents of the collection will be the same with
 * every call to {@link #iterator()}.
 * 

* The original collection passed to the SnapshotCloneIterable's * constructor should be thread-safe (e.g. {@link java.util.Vector}); * otherwise you run the risk of a corrupted collection. *

* By default, the iterator returned by a SnapshotCloneIterable does not * support the {@link Iterator#remove()} operation; this is because it does not * have access to the original collection. But if the SnapshotCloneIterable * is supplied with a {@link CloneIterator.Remover} * it will delegate the * {@link Iterator#remove()} operation to the Remover. * Alternatively, a subclass can override the iterable's {@link #remove(Object)} * method. *

* This iterable is useful for multiple passes over a collection that should not * be changed (e.g. by another thread) between passes. * * @param the type of elements returned by the iterable's iterator * * @see CloneIterator * @see LiveCloneIterable * @see org.eclipse.persistence.jpa.jpql.utility.iterable.SnapshotCloneListIterable SnapshotCloneListIterable */ public class SnapshotCloneIterable extends CloneIterable { private final Object[] array; // ********** constructors ********** /** * Construct a "snapshot" iterable for the specified iterator. * The {@link Iterator#remove()} operation will not be supported * by the iterator returned by {@link #iterator()} * unless a subclass overrides the iterable's {@link #remove(Object)} * method. */ public SnapshotCloneIterable(Iterator iterator) { super(); this.array = CollectionTools.array(Object.class, iterator); } /** * Construct a "snapshot" iterable for the specified iterator. * The specified remover will be used by any generated iterators to * remove objects from the original collection. */ public SnapshotCloneIterable(Iterator iterator, CloneIterator.Remover remover) { super(remover); this.array =CollectionTools.array(Object.class, iterator); } /** * Construct a "snapshot" iterable for the specified collection. * The {@link Iterator#remove()} operation will not be supported * by the iterator returned by {@link #iterator()} * unless a subclass overrides the iterable's {@link #remove(Object)} * method. */ public SnapshotCloneIterable(Collection collection) { super(); this.array = collection.toArray(); } /** * Construct a "snapshot" iterable for the specified collection. * The specified remover will be used by any generated iterators to * remove objects from the original collection. */ public SnapshotCloneIterable(Collection collection, CloneIterator.Remover remover) { super(remover); this.array = collection.toArray(); } // ********** Iterable implementation ********** public Iterator iterator() { return new LocalCloneIterator(this.remover, this.array); } @Override public String toString() { return Arrays.toString(this.array); } // ********** clone iterator ********** /** * provide access to "internal" constructor */ protected static class LocalCloneIterator extends CloneIterator { protected LocalCloneIterator(Remover remover, Object[] array) { super(remover, array); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy