aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/FailFast.java
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/FailFast.java')
-rw-r--r--framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/FailFast.java135
1 files changed, 135 insertions, 0 deletions
diff --git a/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/FailFast.java b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/FailFast.java
new file mode 100644
index 00000000..dc962bb4
--- /dev/null
+++ b/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/types/resources/FailFast.java
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.tools.ant.types.resources;
+
+import java.util.ConcurrentModificationException;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.WeakHashMap;
+
+import org.apache.tools.ant.types.Resource;
+
+/**
+ * Helper class for ResourceCollections to return Iterators
+ * that fail on changes to the object.
+ * @since Ant 1.7
+ */
+/*package-private*/ class FailFast implements Iterator<Resource> {
+ private static final WeakHashMap<Object, Set<FailFast>> MAP = new WeakHashMap<Object, Set<FailFast>>();
+
+ /**
+ * Invalidate any in-use Iterators from the specified Object.
+ * @param o the parent Object.
+ */
+ static synchronized void invalidate(Object o) {
+ Set<FailFast> s = MAP.get(o);
+ if (s != null) {
+ s.clear();
+ }
+ }
+
+ private static synchronized void add(FailFast f) {
+ Set<FailFast> s = MAP.get(f.parent);
+ if (s == null) {
+ s = new HashSet<FailFast>();
+ MAP.put(f.parent, s);
+ }
+ s.add(f);
+ }
+
+ private static synchronized void remove(FailFast f) {
+ Set<FailFast> s = MAP.get(f.parent);
+ if (s != null) {
+ s.remove(f);
+ }
+ }
+
+ private static synchronized void failFast(FailFast f) {
+ Set<FailFast> s = MAP.get(f.parent);
+ if (!s.contains(f)) {
+ throw new ConcurrentModificationException();
+ }
+ }
+
+ private final Object parent;
+ private Iterator<Resource> wrapped;
+
+ /**
+ * Construct a new FailFast Iterator wrapping the specified Iterator
+ * and dependent upon the specified parent Object.
+ * @param o the parent Object.
+ * @param i the wrapped Iterator.
+ */
+ FailFast(Object o, Iterator<Resource> i) {
+ if (o == null) {
+ throw new IllegalArgumentException("parent object is null");
+ }
+ if (i == null) {
+ throw new IllegalArgumentException("cannot wrap null iterator");
+ }
+ parent = o;
+ if (i.hasNext()) {
+ wrapped = i;
+ add(this);
+ }
+ }
+
+ /**
+ * Fulfill the Iterator contract.
+ * @return true if there are more elements.
+ */
+ public boolean hasNext() {
+ if (wrapped == null) {
+ return false;
+ }
+ failFast(this);
+ return wrapped.hasNext();
+ }
+
+ /**
+ * Fulfill the Iterator contract.
+ * @return the next element.
+ * @throws NoSuchElementException if no more elements.
+ */
+ public Resource next() {
+ if (wrapped == null || !wrapped.hasNext()) {
+ throw new NoSuchElementException();
+ }
+ failFast(this);
+ try {
+ return wrapped.next();
+ } finally {
+ if (!wrapped.hasNext()) {
+ wrapped = null;
+ remove(this);
+ }
+ }
+ }
+
+ /**
+ * Fulfill the Iterator contract.
+ * @throws UnsupportedOperationException always.
+ */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+}
+