Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan D. Brooks2013-11-01 02:48:42 -0400
committerRoberto E. Escobar2013-11-25 18:45:34 -0500
commit73b39bf993855b06b04a82663bc56e65a870c483 (patch)
tree2d4c366ee35af22a14715170c41eb2cca54cef8a /plugins/org.eclipse.osee.framework.jdk.core
parentcbceea1cfea7ee2fcbe798c59ee11b3b4fee039e (diff)
downloadorg.eclipse.osee-73b39bf993855b06b04a82663bc56e65a870c483.tar.gz
org.eclipse.osee-73b39bf993855b06b04a82663bc56e65a870c483.tar.xz
org.eclipse.osee-73b39bf993855b06b04a82663bc56e65a870c483.zip
refinement: Create DrainingIterator datatype
Diffstat (limited to 'plugins/org.eclipse.osee.framework.jdk.core')
-rw-r--r--plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/DrainingIterator.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/DrainingIterator.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/DrainingIterator.java
new file mode 100644
index 0000000000..0a10a0cf9a
--- /dev/null
+++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/DrainingIterator.java
@@ -0,0 +1,62 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Boeing.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Boeing - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.osee.framework.jdk.core.type;
+
+import java.util.Iterator;
+
+/**
+ * Immediately removes each element from the underlying iterator when it is returned from calling next(). One use for a
+ * DrainingIterator is when an iterator from a ConcurrentHashMap (i.e. ConcurrentHashMap.values().iterator()) is passed
+ * to another method for iteration and the subsequent removal of each element is required. In that case, this class is
+ * more than just a convenience because it is not safe to simply call ConcurrentHashMap.clear() after the iterator
+ * finishes since the ConcurrentHashMap may well have been added to concurrently and this may or may not be reflected in
+ * the iterator. Thus new additions to the ConcurrentHashMap might otherwise get cleared with out ever being accessed
+ * through iteration.
+ *
+ * @author Ryan D. Brooks
+ */
+public class DrainingIterator<T> implements Iterator<T>, Iterable<T> {
+ private final Iterator<T> iterator;
+
+ public DrainingIterator(Iterator<T> iterator) {
+ this.iterator = iterator;
+ }
+
+ public DrainingIterator(Iterable<T> iterable) {
+ this.iterator = iterable.iterator();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public T next() {
+ T value = iterator.next();
+ iterator.remove();
+ return value;
+ }
+
+ @Override
+ /**
+ * It is always unnecessary to call remove from a DrainingIterator since every call to next has already called remove. This method always throws UnsupportedOperationException.
+ */
+ public void remove() {
+ throw new UnsupportedOperationException(
+ "It is always unnecessary to call remove from a DrainingIterator since every call to next has already called remove.");
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ return this;
+ }
+} \ No newline at end of file

Back to the top