, Closure super A>>> closures, Closure super A> defaultClosure) {
+ public static Closure switchClosure(Iterable extends Association extends Predicate super A>, ? extends Closure super A>>> closures, Closure super A> defaultClosure) {
return new SwitchClosure<>(closures, defaultClosure);
}
+ /**
+ * Return a closure that loops over the specified set of predicate/closure
+ * pairs, passing its argument to each predicate to determine
+ * which of the closures to execute. Only the first closure whose predicate
+ * evaluates to true
is executed, even if other, following,
+ * predicates would evaluate to true
.
+ * If none of the predicates evaluates to true
, the closure
+ * will do nothing.
+ *
+ * @param the type of the object passed to the closure
+ */
+ public static InterruptibleClosure switchInterruptibleClosure(Iterable extends Association extends Predicate super A>, ? extends InterruptibleClosure super A>>> closures) {
+ return switchInterruptibleClosure(closures, nullClosure());
+ }
+
+ /**
+ * Return a closure that loops over the specified set of predicate/closure
+ * pairs, passing its argument to each predicate to determine
+ * which of the closures to execute. Only the first closure whose predicate
+ * evaluates to true
is executed, even if other, following,
+ * predicates would evaluate to true
.
+ * If none of the predicates evaluates to true
, the specified
+ * default closure is executed.
+ *
+ * @param the type of the object passed to the closure
+ */
+ public static InterruptibleClosure switchInterruptibleClosure(Iterable extends Association extends Predicate super A>, ? extends InterruptibleClosure super A>>> closures, InterruptibleClosure super A> defaultClosure) {
+ return new SwitchInterruptibleClosure<>(closures, defaultClosure);
+ }
+
// ********** boolean **********
@@ -303,6 +565,7 @@ public final class ClosureTools {
/**
* Return a closure that will throw an
* {@link UnsupportedOperationException exception} when executed.
+ *
* @param the type of the object passed to the closure
*/
public static Closure disabledClosure() {
@@ -310,26 +573,61 @@ public final class ClosureTools {
}
+ // ********** interrupted **********
+
+ /**
+ * Return a closure that will throw an
+ * {@link InterruptedException exception} when executed.
+ *
+ * @param the type of the object passed to the closure
+ */
+ public static InterruptibleClosure interruptedClosure() {
+ return InterruptedClosure.instance();
+ }
+
+
// ********** looping **********
/**
* Return a closure that executes the specified closure the specified
* number of times.
+ *
* @param the type of the object passed to the closure
*/
- public static Closure repeat(Closure super A> closure, int count) {
- return new RepeatingClosure<>(closure, count);
+ public static Closure repeatingClosure(Closure super A> closure, int count) {
+ return (count == 0) ? nullClosure() : new RepeatingClosure<>(closure, count);
+ }
+
+ /**
+ * Return a closure that executes the specified closure the specified
+ * number of times.
+ *
+ * @param the type of the object passed to the closure
+ */
+ public static InterruptibleClosure repeatingClosure(InterruptibleClosure super A> closure, int count) {
+ return (count == 0) ? nullClosure() : new RepeatingInterruptibleClosure<>(closure, count);
}
/**
* Return a closure that executes the specified closure while the specified
* predicate evaluates to true
when passed the argument.
+ *
* @param the type of the object passed to the closure
*/
- public static Closure while_(Predicate super A> predicate, Closure super A> closure) {
+ public static Closure whileClosure(Predicate super A> predicate, Closure super A> closure) {
return new WhileClosure<>(predicate, closure);
}
+ /**
+ * Return a closure that executes the specified closure while the specified
+ * predicate evaluates to true
when passed the argument.
+ *
+ * @param the type of the object passed to the closure
+ */
+ public static InterruptibleClosure whileClosure(Predicate super A> predicate, InterruptibleClosure super A> closure) {
+ return new WhileInterruptibleClosure<>(predicate, closure);
+ }
+
/**
* Return a closure that executes the specified closure until the specified
* predicate evaluates to true
when passed the argument. The
@@ -339,12 +637,29 @@ public final class ClosureTools {
* statement (i.e. it executes until the predicate evaluates to
* true
,
* not false
).
+ *
* @param the type of the object passed to the closure
*/
- public static Closure until(Closure super A> closure, Predicate super A> predicate) {
+ public static Closure untilClosure(Closure super A> closure, Predicate super A> predicate) {
return new UntilClosure<>(closure, predicate);
}
+ /**
+ * Return a closure that executes the specified closure until the specified
+ * predicate evaluates to true
when passed the argument. The
+ * specifie closure will always execute at least once.
+ *
+ * NB: This is the inverse of the Java do-while
+ * statement (i.e. it executes until the predicate evaluates to
+ * true
,
+ * not false
).
+ *
+ * @param the type of the object passed to the closure
+ */
+ public static InterruptibleClosure untilClosure(InterruptibleClosure super A> closure, Predicate super A> predicate) {
+ return new UntilInterruptibleClosure<>(closure, predicate);
+ }
+
// ********** constructor **********
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CommandBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CommandBiClosure.java
new file mode 100644
index 0000000000..9be7d10d9f
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CommandBiClosure.java
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.command.Command;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Adapt a {@link Command} to the {@link BiClosure} interface.
+ * The closure's arguments are ignored.
+ *
+ * @param the type of the first object passed to the closure; ignored
+ * @param the type of the second object passed to the closure; ignored
+ */
+public class CommandBiClosure
+ implements BiClosure
+{
+ private final Command command;
+
+
+ public CommandBiClosure(Command command) {
+ super();
+ if (command == null) {
+ throw new NullPointerException();
+ }
+ this.command = command;
+ }
+
+ public void execute(A1 argument1, A2 argument2) {
+ this.command.execute();
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.command);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeBiClosure.java
new file mode 100644
index 0000000000..de8583f962
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeBiClosure.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
+
+/**
+ * A composite of closures. Pass the composite's arguments to each closure,
+ * in sequence.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ */
+public class CompositeBiClosure
+ implements BiClosure
+{
+ private final Iterable extends BiClosure super A1, ? super A2>> closures;
+
+ public CompositeBiClosure(Iterable extends BiClosure super A1, ? super A2>> closures) {
+ super();
+ if (IterableTools.isOrContainsNull(closures)) {
+ throw new NullPointerException();
+ }
+ this.closures = closures;
+ }
+
+ public void execute(A1 argument1, A2 argument2) {
+ for (BiClosure super A1, ? super A2> closure : this.closures) {
+ closure.execute(argument1, argument2);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.closures);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeClosure.java
index 792547aee0..bbeb4bc8c6 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -22,9 +22,9 @@ import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
public class CompositeClosure
implements Closure
{
- private final Iterable> closures;
+ private final Iterable extends Closure super A>> closures;
- public CompositeClosure(Iterable> closures) {
+ public CompositeClosure(Iterable extends Closure super A>> closures) {
super();
if (IterableTools.isOrContainsNull(closures)) {
throw new NullPointerException();
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeInterruptibleBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeInterruptibleBiClosure.java
new file mode 100644
index 0000000000..1d83f5515b
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeInterruptibleBiClosure.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
+
+/**
+ * A composite of closures. Pass the composite's arguments to each closure,
+ * in sequence.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ */
+public class CompositeInterruptibleBiClosure
+ implements InterruptibleBiClosure
+{
+ private final Iterable extends InterruptibleBiClosure super A1, ? super A2>> closures;
+
+ public CompositeInterruptibleBiClosure(Iterable extends InterruptibleBiClosure super A1, ? super A2>> closures) {
+ super();
+ if (IterableTools.isOrContainsNull(closures)) {
+ throw new NullPointerException();
+ }
+ this.closures = closures;
+ }
+
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ for (InterruptibleBiClosure super A1, ? super A2> closure : this.closures) {
+ closure.execute(argument1, argument2);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.closures);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeInterruptibleClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeInterruptibleClosure.java
index 5564a840a2..e1669c766c 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeInterruptibleClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/CompositeInterruptibleClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -19,9 +19,9 @@ import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
public class CompositeInterruptibleClosure
implements InterruptibleClosure
{
- private final Iterable> closures;
+ private final Iterable extends InterruptibleClosure super A>> closures;
- public CompositeInterruptibleClosure(Iterable> closures) {
+ public CompositeInterruptibleClosure(Iterable extends InterruptibleClosure super A>> closures) {
super();
if (IterableTools.isOrContainsNull(closures)) {
throw new NullPointerException();
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ConditionalClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ConditionalClosure.java
index 9e6c6d4680..7dd8ad1605 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ConditionalClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ConditionalClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -33,11 +33,17 @@ public class ConditionalClosure
public ConditionalClosure(Predicate super A> predicate, Closure super A> trueClosure, Closure super A> falseClosure) {
super();
- if ((predicate == null) || (trueClosure == null) || (falseClosure == null)) {
+ if (predicate == null) {
throw new NullPointerException();
}
this.predicate = predicate;
+ if (trueClosure == null) {
+ throw new NullPointerException();
+ }
this.trueClosure = trueClosure;
+ if (falseClosure == null) {
+ throw new NullPointerException();
+ }
this.falseClosure = falseClosure;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ConditionalInterruptibleClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ConditionalInterruptibleClosure.java
index 64a766ff92..e2f62b5243 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ConditionalInterruptibleClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ConditionalInterruptibleClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -29,11 +29,17 @@ public class ConditionalInterruptibleClosure
public ConditionalInterruptibleClosure(Predicate super A> predicate, InterruptibleClosure super A> trueClosure, InterruptibleClosure super A> falseClosure) {
super();
- if ((predicate == null) || (trueClosure == null) || (falseClosure == null)) {
+ if (predicate == null) {
throw new NullPointerException();
}
this.predicate = predicate;
+ if (trueClosure == null) {
+ throw new NullPointerException();
+ }
this.trueClosure = trueClosure;
+ if (falseClosure == null) {
+ throw new NullPointerException();
+ }
this.falseClosure = falseClosure;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/DisabledBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/DisabledBiClosure.java
new file mode 100644
index 0000000000..e5c3fc5649
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/DisabledBiClosure.java
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import java.io.Serializable;
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Closure that will throw an
+ * {@link UnsupportedOperationException exception} when executed.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ */
+public final class DisabledBiClosure
+ implements BiClosure, Serializable
+{
+ @SuppressWarnings("rawtypes")
+ public static final BiClosure INSTANCE = new DisabledBiClosure();
+
+ @SuppressWarnings("unchecked")
+ public static BiClosure instance() {
+ return INSTANCE;
+ }
+
+ // ensure single instance
+ private DisabledBiClosure() {
+ super();
+ }
+
+ // throw an exception
+ public void execute(A1 argument1, A2 argument2) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.singletonToString(this);
+ }
+
+ private static final long serialVersionUID = 1L;
+ private Object readResolve() {
+ // replace this object with the singleton
+ return INSTANCE;
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/FactoryBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/FactoryBiClosure.java
new file mode 100644
index 0000000000..16b424e8e5
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/FactoryBiClosure.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.factory.Factory;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Adapt a {@link Factory} to the {@link BiClosure} interface.
+ * The closure's arguments and the factory's output are ignored.
+ * This really only useful for a factory that has side-effects.
+ *
+ * @param the type of the first object passed to the closure; ignored
+ * @param the type of the second object passed to the closure; ignored
+ */
+public class FactoryBiClosure
+ implements BiClosure
+{
+ private final Factory> factory;
+
+
+ public FactoryBiClosure(Factory> factory) {
+ super();
+ if (factory == null) {
+ throw new NullPointerException();
+ }
+ this.factory = factory;
+ }
+
+ public void execute(A1 argument1, A2 argument2) {
+ this.factory.create();
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.factory);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptedBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptedBiClosure.java
new file mode 100644
index 0000000000..9dba95aa14
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptedBiClosure.java
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import java.io.Serializable;
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Closure that will throw an {@link InterruptedException exception}
+ * when executed.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ */
+public final class InterruptedBiClosure
+ implements InterruptibleBiClosure, Serializable
+{
+ @SuppressWarnings("rawtypes")
+ public static final InterruptibleBiClosure INSTANCE = new InterruptedBiClosure();
+
+ @SuppressWarnings("unchecked")
+ public static InterruptibleBiClosure instance() {
+ return INSTANCE;
+ }
+
+ // ensure single instance
+ private InterruptedBiClosure() {
+ super();
+ }
+
+ // throw an exception
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ throw new InterruptedException();
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.singletonToString(this);
+ }
+
+ private static final long serialVersionUID = 1L;
+ private Object readResolve() {
+ // replace this object with the singleton
+ return INSTANCE;
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleBiClosureAdapter.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleBiClosureAdapter.java
new file mode 100644
index 0000000000..9d05d7b70c
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleBiClosureAdapter.java
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Convenience interruptible bi-closure that does nothing.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ *
+ * @see BiClosureAdapter
+ * @see NullBiClosure
+ */
+public class InterruptibleBiClosureAdapter
+ implements InterruptibleBiClosure
+{
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ // NOP
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleBiClosureWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleBiClosureWrapper.java
new file mode 100644
index 0000000000..fced788030
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleBiClosureWrapper.java
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Closure wrapper that can have its wrapped closure changed,
+ * allowing a client to change a previously-supplied closure's
+ * behavior mid-stream.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ *
+ * @see #setClosure(InterruptibleBiClosure)
+ */
+public class InterruptibleBiClosureWrapper
+ implements InterruptibleBiClosure
+{
+ protected volatile InterruptibleBiClosure super A1, ? super A2> closure;
+
+ public InterruptibleBiClosureWrapper(InterruptibleBiClosure super A1, ? super A2> closure) {
+ super();
+ if (closure == null) {
+ throw new NullPointerException();
+ }
+ this.closure = closure;
+ }
+
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ this.closure.execute(argument1, argument2);
+ }
+
+ public void setClosure(InterruptibleBiClosure super A1, ? super A2> closure) {
+ if (closure == null) {
+ throw new NullPointerException();
+ }
+ this.closure = closure;
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.closure);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleCommandBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleCommandBiClosure.java
new file mode 100644
index 0000000000..a71d62a62c
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleCommandBiClosure.java
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.command.InterruptibleCommand;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Adapt an {@link InterruptibleCommand} to the {@link InterruptibleBiClosure} interface.
+ * The closure's arguments are ignored.
+ *
+ * @param the type of the first object passed to the closure; ignored
+ * @param the type of the second object passed to the closure; ignored
+ */
+public class InterruptibleCommandBiClosure
+ implements InterruptibleBiClosure
+{
+ private final InterruptibleCommand command;
+
+
+ public InterruptibleCommandBiClosure(InterruptibleCommand command) {
+ super();
+ if (command == null) {
+ throw new NullPointerException();
+ }
+ this.command = command;
+ }
+
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ this.command.execute();
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.command);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleFactoryBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleFactoryBiClosure.java
new file mode 100644
index 0000000000..d36186807c
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/InterruptibleFactoryBiClosure.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.factory.InterruptibleFactory;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Adapt an {@link InterruptibleFactory} to the {@link InterruptibleBiClosure} interface.
+ * The closure's arguments and the factory's output are ignored.
+ * This really only useful for a factory that has side-effects.
+ *
+ * @param the type of the first object passed to the closure; ignored
+ * @param the type of the second object passed to the closure; ignored
+ */
+public class InterruptibleFactoryBiClosure
+ implements InterruptibleBiClosure
+{
+ private final InterruptibleFactory> factory;
+
+
+ public InterruptibleFactoryBiClosure(InterruptibleFactory> factory) {
+ super();
+ if (factory == null) {
+ throw new NullPointerException();
+ }
+ this.factory = factory;
+ }
+
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ this.factory.create();
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.factory);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/MethodClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/MethodClosure.java
index bea0748c6f..571388f12a 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/MethodClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/MethodClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -40,11 +40,17 @@ public class MethodClosure
public MethodClosure(String methodName, Class>[] parameterTypes, Object[] arguments) {
super();
- if ((methodName == null) || ArrayTools.isOrContainsNull(parameterTypes) || (arguments == null)) {
+ if (methodName == null) {
throw new NullPointerException();
}
this.methodName = methodName;
+ if (ArrayTools.isOrContainsNull(parameterTypes)) {
+ throw new NullPointerException();
+ }
this.parameterTypes = parameterTypes;
+ if (arguments == null) {
+ throw new NullPointerException();
+ }
this.arguments = arguments;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullBiClosure.java
new file mode 100644
index 0000000000..c1e21e8423
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullBiClosure.java
@@ -0,0 +1,54 @@
+/*******************************************************************************
+ * Copyright (c) 2012, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import java.io.Serializable;
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * BiClosure that will do nothing when executed.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ *
+ * @see BiClosureAdapter
+ */
+public final class NullBiClosure
+ implements BiClosure, Serializable
+{
+ @SuppressWarnings("rawtypes")
+ public static final BiClosure INSTANCE = new NullBiClosure();
+
+ @SuppressWarnings("unchecked")
+ public static BiClosure instance() {
+ return INSTANCE;
+ }
+
+ // ensure single instance
+ private NullBiClosure() {
+ super();
+ }
+
+ public void execute(A1 argument1, A2 argument2) {
+ // NOP
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.singletonToString(this);
+ }
+
+ private static final long serialVersionUID = 1L;
+ private Object readResolve() {
+ // replace this object with the singleton
+ return INSTANCE;
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullCheckClosureWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullCheckClosureWrapper.java
index 78c1ac3dd8..23ba6f3522 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullCheckClosureWrapper.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullCheckClosureWrapper.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -37,6 +37,9 @@ public class NullCheckClosureWrapper
throw new NullPointerException();
}
this.closure = closure;
+ if (nullCommand == null) {
+ throw new NullPointerException();
+ }
this.nullCommand = nullCommand;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullCheckInterruptibleClosureWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullCheckInterruptibleClosureWrapper.java
index 9033212fae..ef029729b1 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullCheckInterruptibleClosureWrapper.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullCheckInterruptibleClosureWrapper.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -32,6 +32,9 @@ public class NullCheckInterruptibleClosureWrapper
throw new NullPointerException();
}
this.closure = closure;
+ if (nullCommand == null) {
+ throw new NullPointerException();
+ }
this.nullCommand = nullCommand;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingBiClosure.java
new file mode 100644
index 0000000000..6540dddf53
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingBiClosure.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * BiClosure that executes another closure a specified number of times.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ */
+public class RepeatingBiClosure
+ implements BiClosure
+{
+ private final int count;
+ private final BiClosure super A1, ? super A2> closure;
+
+ public RepeatingBiClosure(BiClosure super A1, ? super A2> closure, int count) {
+ super();
+ if (closure == null) {
+ throw new NullPointerException();
+ }
+ this.closure = closure;
+ if (count < 0) {
+ throw new IndexOutOfBoundsException("invalid count: " + count); //$NON-NLS-1$
+ }
+ this.count = count;
+ }
+
+ public void execute(A1 argument1, A2 argument2) {
+ for (int i = this.count; i-- > 0;) {
+ this.closure.execute(argument1, argument2);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.closure);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingClosure.java
index 2eb80a1421..a1d6e3c995 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -33,10 +33,10 @@ public class RepeatingClosure
if (closure == null) {
throw new NullPointerException();
}
- if (count <= 0) {
+ this.closure = closure;
+ if (count < 0) {
throw new IndexOutOfBoundsException("invalid count: " + count); //$NON-NLS-1$
}
- this.closure = closure;
this.count = count;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingInterruptibleBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingInterruptibleBiClosure.java
new file mode 100644
index 0000000000..937d4d5a3a
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingInterruptibleBiClosure.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * BiClosure that executes another closure a specified number of times.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ */
+public class RepeatingInterruptibleBiClosure
+ implements InterruptibleBiClosure
+{
+ private final int count;
+ private final InterruptibleBiClosure super A1, ? super A2> closure;
+
+ public RepeatingInterruptibleBiClosure(InterruptibleBiClosure super A1, ? super A2> closure, int count) {
+ super();
+ if (closure == null) {
+ throw new NullPointerException();
+ }
+ this.closure = closure;
+ if (count < 0) {
+ throw new IndexOutOfBoundsException("invalid count: " + count); //$NON-NLS-1$
+ }
+ this.count = count;
+ }
+
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ for (int i = this.count; i-- > 0;) {
+ this.closure.execute(argument1, argument2);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.closure);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingInterruptibleClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingInterruptibleClosure.java
index a401a79a9d..9599946900 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingInterruptibleClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/RepeatingInterruptibleClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -30,7 +30,7 @@ public class RepeatingInterruptibleClosure
if (closure == null) {
throw new NullPointerException();
}
- if (count <= 0) {
+ if (count < 0) {
throw new IndexOutOfBoundsException("invalid count: " + count); //$NON-NLS-1$
}
this.closure = closure;
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeBiClosureWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeBiClosureWrapper.java
new file mode 100644
index 0000000000..aeb4e43d4c
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeBiClosureWrapper.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.exception.ExceptionHandler;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Closure wrapper that will handle any exceptions thrown by the wrapped
+ * closure with an {@link ExceptionHandler exception handler}. If the
+ * wrapped closure throws an exception, the safe closure will handle
+ * the exception and return.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ */
+public class SafeBiClosureWrapper
+ implements BiClosure
+{
+ private final BiClosure super A1, ? super A2> closure;
+ private final ExceptionHandler exceptionHandler;
+
+
+ public SafeBiClosureWrapper(BiClosure super A1, ? super A2> closure, ExceptionHandler exceptionHandler) {
+ super();
+ if (closure == null) {
+ throw new NullPointerException();
+ }
+ this.closure = closure;
+ if (exceptionHandler == null) {
+ throw new NullPointerException();
+ }
+ this.exceptionHandler = exceptionHandler;
+ }
+
+ public void execute(A1 argument1, A2 argument2) {
+ try {
+ this.closure.execute(argument1, argument2);
+ } catch (Throwable ex) {
+ this.exceptionHandler.handleException(ex);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.closure);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeClosureWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeClosureWrapper.java
index 0fae6e1ca7..9caa77e632 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeClosureWrapper.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeClosureWrapper.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -30,10 +30,13 @@ public class SafeClosureWrapper
public SafeClosureWrapper(Closure super A> closure, ExceptionHandler exceptionHandler) {
super();
- if ((closure == null) || (exceptionHandler == null)) {
+ if (closure == null) {
throw new NullPointerException();
}
this.closure = closure;
+ if (exceptionHandler == null) {
+ throw new NullPointerException();
+ }
this.exceptionHandler = exceptionHandler;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeInterruptibleBiClosureWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeInterruptibleBiClosureWrapper.java
new file mode 100644
index 0000000000..776ff9552d
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeInterruptibleBiClosureWrapper.java
@@ -0,0 +1,58 @@
+/*******************************************************************************
+ * Copyright (c) 2013, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.exception.ExceptionHandler;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Closure wrapper that will handle any exceptions thrown by the wrapped
+ * closure with an {@link ExceptionHandler exception handler}. If the
+ * wrapped closure throws an exception, the safe closure will handle
+ * the exception and return.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ */
+public class SafeInterruptibleBiClosureWrapper
+ implements InterruptibleBiClosure
+{
+ private final InterruptibleBiClosure super A1, ? super A2> closure;
+ private final ExceptionHandler exceptionHandler;
+
+
+ public SafeInterruptibleBiClosureWrapper(InterruptibleBiClosure super A1, ? super A2> closure, ExceptionHandler exceptionHandler) {
+ super();
+ if (closure == null) {
+ throw new NullPointerException();
+ }
+ this.closure = closure;
+ if (exceptionHandler == null) {
+ throw new NullPointerException();
+ }
+ this.exceptionHandler = exceptionHandler;
+ }
+
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ try {
+ this.closure.execute(argument1, argument2);
+ } catch (InterruptedException ex) {
+ throw ex;
+ } catch (Throwable ex) {
+ this.exceptionHandler.handleException(ex);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.closure);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeInterruptibleClosureWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeInterruptibleClosureWrapper.java
index b106b075b8..cc3a317353 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeInterruptibleClosureWrapper.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SafeInterruptibleClosureWrapper.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -25,10 +25,13 @@ public class SafeInterruptibleClosureWrapper
public SafeInterruptibleClosureWrapper(InterruptibleClosure super A> closure, ExceptionHandler exceptionHandler) {
super();
- if ((closure == null) || (exceptionHandler == null)) {
+ if (closure == null) {
throw new NullPointerException();
}
this.closure = closure;
+ if (exceptionHandler == null) {
+ throw new NullPointerException();
+ }
this.exceptionHandler = exceptionHandler;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SwitchClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SwitchClosure.java
index c4839d7af7..d62730a374 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SwitchClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SwitchClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -34,20 +34,23 @@ import org.eclipse.jpt.common.utility.predicate.Predicate;
public class SwitchClosure
implements Closure
{
- private final Iterable, Closure super A>>> closures;
+ private final Iterable extends Association extends Predicate super A>, ? extends Closure super A>>> closures;
private final Closure super A> defaultClosure;
- public SwitchClosure(Iterable, Closure super A>>> closures, Closure super A> defaultClosure) {
+ public SwitchClosure(Iterable extends Association extends Predicate super A>, ? extends Closure super A>>> closures, Closure super A> defaultClosure) {
super();
- if (IterableTools.isOrContainsNull(closures) || (defaultClosure == null)) {
+ if (IterableTools.isOrContainsNull(closures)) {
throw new NullPointerException();
}
this.closures = closures;
+ if (defaultClosure == null) {
+ throw new NullPointerException();
+ }
this.defaultClosure = defaultClosure;
}
public void execute(A argument) {
- for (Association, Closure super A>> association : this.closures) {
+ for (Association extends Predicate super A>, ? extends Closure super A>> association : this.closures) {
if (association.getKey().evaluate(argument)) {
association.getValue().execute(argument);
return; // execute only one closure
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SwitchInterruptibleClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SwitchInterruptibleClosure.java
index 086c518f0e..f6640be275 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SwitchInterruptibleClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/SwitchInterruptibleClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -25,26 +25,29 @@ import org.eclipse.jpt.common.utility.predicate.Predicate;
public class SwitchInterruptibleClosure
implements InterruptibleClosure
{
- private final Iterable, InterruptibleClosure super A>>> closures;
- private final InterruptibleClosure super A> defaultInterruptibleClosure;
+ private final Iterable extends Association extends Predicate super A>, ? extends InterruptibleClosure super A>>> closures;
+ private final InterruptibleClosure super A> defaultClosure;
- public SwitchInterruptibleClosure(Iterable, InterruptibleClosure super A>>> closures, InterruptibleClosure super A> defaultInterruptibleClosure) {
+ public SwitchInterruptibleClosure(Iterable extends Association extends Predicate super A>, ? extends InterruptibleClosure super A>>> closures, InterruptibleClosure super A> defaultClosure) {
super();
- if (IterableTools.isOrContainsNull(closures) || (defaultInterruptibleClosure == null)) {
+ if (IterableTools.isOrContainsNull(closures)) {
throw new NullPointerException();
}
this.closures = closures;
- this.defaultInterruptibleClosure = defaultInterruptibleClosure;
+ if (defaultClosure == null) {
+ throw new NullPointerException();
+ }
+ this.defaultClosure = defaultClosure;
}
public void execute(A argument) throws InterruptedException {
- for (Association, InterruptibleClosure super A>> association : this.closures) {
+ for (Association extends Predicate super A>, ? extends InterruptibleClosure super A>> association : this.closures) {
if (association.getKey().evaluate(argument)) {
association.getValue().execute(argument);
return; // execute only one closure
}
}
- this.defaultInterruptibleClosure.execute(argument);
+ this.defaultClosure.execute(argument);
}
@Override
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalBiClosure.java
new file mode 100644
index 0000000000..e62cb12056
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalBiClosure.java
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+
+/**
+ * This closure allows the client to specify a different closure for each
+ * thread. If there is no closure for the current thread, the configured default
+ * closure is executed.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ *
+ * @see #set(BiClosure)
+ */
+public class ThreadLocalBiClosure
+ implements BiClosure
+{
+ private final ThreadLocal> threadLocal;
+ private final BiClosure super A1, ? super A2> defaultClosure;
+
+ public ThreadLocalBiClosure(BiClosure super A1, ? super A2> defaultClosure) {
+ super();
+ if (defaultClosure == null) {
+ throw new NullPointerException();
+ }
+ this.defaultClosure = defaultClosure;
+ this.threadLocal = this.buildThreadLocal();
+ }
+
+ private ThreadLocal> buildThreadLocal() {
+ return new ThreadLocal<>();
+ }
+
+ public void execute(A1 argument1, A2 argument2) {
+ this.get().execute(argument1, argument2);
+ }
+
+ private BiClosure super A1, ? super A2> get() {
+ BiClosure super A1, ? super A2> closure = this.threadLocal.get();
+ return (closure != null) ? closure : this.defaultClosure;
+ }
+
+ /**
+ * Set the current thread's closure to the specified value.
+ */
+ public void set(BiClosure super A1, ? super A2> closure) {
+ this.threadLocal.set(closure);
+ }
+
+ /**
+ * Return the string representation of the current thread's closure.
+ */
+ @Override
+ public String toString() {
+ return this.get().toString();
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalClosure.java
index 7d9d93bab0..4857697c81 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalClosure.java
@@ -45,10 +45,7 @@ public class ThreadLocalClosure
private Closure super A> get() {
Closure super A> closure = this.threadLocal.get();
- if (closure != null) {
- return closure;
- }
- return this.defaultClosure;
+ return (closure != null) ? closure : this.defaultClosure;
}
/**
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleBiClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleBiClosure.java
new file mode 100644
index 0000000000..a722dec737
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleBiClosure.java
@@ -0,0 +1,67 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+
+/**
+ * This closure allows the client to specify a different closure for each
+ * thread. If there is no closure for the current thread, the configured default
+ * closure is executed.
+ *
+ * @param the type of the first object passed to the closure
+ * @param the type of the second object passed to the closure
+ *
+ * @see #set(BiClosure)
+ */
+public class ThreadLocalInterruptibleBiClosure
+ implements InterruptibleBiClosure
+{
+ private final ThreadLocal> threadLocal;
+ private final InterruptibleBiClosure super A1, ? super A2> defaultClosure;
+
+ public ThreadLocalInterruptibleBiClosure(InterruptibleBiClosure super A1, ? super A2> defaultClosure) {
+ super();
+ if (defaultClosure == null) {
+ throw new NullPointerException();
+ }
+ this.defaultClosure = defaultClosure;
+ this.threadLocal = this.buildThreadLocal();
+ }
+
+ private ThreadLocal> buildThreadLocal() {
+ return new ThreadLocal<>();
+ }
+
+ public void execute(A1 argument1, A2 argument2) throws InterruptedException {
+ this.get().execute(argument1, argument2);
+ }
+
+ private InterruptibleBiClosure super A1, ? super A2> get() {
+ InterruptibleBiClosure super A1, ? super A2> closure = this.threadLocal.get();
+ return (closure != null) ? closure : this.defaultClosure;
+ }
+
+ /**
+ * Set the current thread's closure to the specified value.
+ */
+ public void set(BiClosure super A1, ? super A2> closure) {
+ this.threadLocal.set(closure);
+ }
+
+ /**
+ * Return the string representation of the current thread's closure.
+ */
+ @Override
+ public String toString() {
+ return this.get().toString();
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleClosure.java
index f9cca6122b..43616a3894 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ThreadLocalInterruptibleClosure.java
@@ -18,14 +18,14 @@ public class ThreadLocalInterruptibleClosure
implements InterruptibleClosure
{
private final ThreadLocal> threadLocal;
- private final InterruptibleClosure super A> defaultInterruptibleClosure;
+ private final InterruptibleClosure super A> defaultClosure;
- public ThreadLocalInterruptibleClosure(InterruptibleClosure super A> defaultInterruptibleClosure) {
+ public ThreadLocalInterruptibleClosure(InterruptibleClosure super A> defaultClosure) {
super();
- if (defaultInterruptibleClosure == null) {
+ if (defaultClosure == null) {
throw new NullPointerException();
}
- this.defaultInterruptibleClosure = defaultInterruptibleClosure;
+ this.defaultClosure = defaultClosure;
this.threadLocal = this.buildThreadLocal();
}
@@ -39,10 +39,7 @@ public class ThreadLocalInterruptibleClosure
private InterruptibleClosure super A> get() {
InterruptibleClosure super A> closure = this.threadLocal.get();
- if (closure != null) {
- return closure;
- }
- return this.defaultInterruptibleClosure;
+ return (closure != null) ? closure : this.defaultClosure;
}
/**
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/UntilClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/UntilClosure.java
index 1caa60423b..4632b96ea9 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/UntilClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/UntilClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -38,11 +38,14 @@ public class UntilClosure
public UntilClosure(Closure super A> closure, Predicate super A> predicate) {
super();
- if ((closure == null) || (predicate == null)) {
+ if (closure == null) {
throw new NullPointerException();
}
- this.predicate = predicate;
this.closure = closure;
+ if (predicate == null) {
+ throw new NullPointerException();
+ }
+ this.predicate = predicate;
}
public void execute(A argument) {
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/UntilInterruptibleClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/UntilInterruptibleClosure.java
index a307e8ebac..38b69adaed 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/UntilInterruptibleClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/UntilInterruptibleClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -28,11 +28,14 @@ public class UntilInterruptibleClosure
public UntilInterruptibleClosure(InterruptibleClosure super A> closure, Predicate super A> predicate) {
super();
- if ((closure == null) || (predicate == null)) {
+ if (closure == null) {
throw new NullPointerException();
}
- this.predicate = predicate;
this.closure = closure;
+ if (predicate == null) {
+ throw new NullPointerException();
+ }
+ this.predicate = predicate;
}
public void execute(A argument) throws InterruptedException {
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/WhileClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/WhileClosure.java
index dfbd8c8f2e..147e7e5e27 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/WhileClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/WhileClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle. 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.
@@ -32,10 +32,13 @@ public class WhileClosure
public WhileClosure(Predicate super A> predicate, Closure super A> closure) {
super();
- if ((predicate == null) || (closure == null)) {
+ if (predicate == null) {
throw new NullPointerException();
}
this.predicate = predicate;
+ if (closure == null) {
+ throw new NullPointerException();
+ }
this.closure = closure;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/WhileInterruptibleClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/WhileInterruptibleClosure.java
index de0014ab88..06a332f625 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/WhileInterruptibleClosure.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/WhileInterruptibleClosure.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2015 Oracle. 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.
@@ -28,10 +28,13 @@ public class WhileInterruptibleClosure
public WhileInterruptibleClosure(Predicate super A> predicate, InterruptibleClosure super A> closure) {
super();
- if ((predicate == null) || (closure == null)) {
+ if (predicate == null) {
throw new NullPointerException();
}
this.predicate = predicate;
+ if (closure == null) {
+ throw new NullPointerException();
+ }
this.closure = closure;
}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java
index ab7b8c52ec..a0e2cd0d9e 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/JptCommonUtilityTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2015 Oracle. All rights reserved.
+ * Copyright (c) 2005, 2016 Oracle. 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.
@@ -9,6 +9,7 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.tests.internal;
+import org.eclipse.jpt.common.utility.tests.internal.closure.JptCommonUtilityClosureTests;
import org.eclipse.jpt.common.utility.tests.internal.collection.JptCommonUtilityCollectionTests;
import org.eclipse.jpt.common.utility.tests.internal.command.JptCommonUtilityCommandTests;
import org.eclipse.jpt.common.utility.tests.internal.comparator.JptCommonUtilityComparatorTests;
@@ -38,6 +39,7 @@ public class JptCommonUtilityTests {
public static Test suite() {
TestSuite suite = new TestSuite(JptCommonUtilityTests.class.getPackage().getName());
+ suite.addTest(JptCommonUtilityClosureTests.suite());
suite.addTest(JptCommonUtilityCollectionTests.suite());
suite.addTest(JptCommonUtilityCommandTests.suite());
suite.addTest(JptCommonUtilityComparatorTests.suite());
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BiClosureToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BiClosureToolsTests.java
new file mode 100644
index 0000000000..da20ae3efa
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BiClosureToolsTests.java
@@ -0,0 +1,726 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Oracle. 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:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.tests.internal.closure;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import org.eclipse.jpt.common.utility.closure.BiClosure;
+import org.eclipse.jpt.common.utility.closure.InterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.command.Command;
+import org.eclipse.jpt.common.utility.command.InterruptibleCommand;
+import org.eclipse.jpt.common.utility.factory.Factory;
+import org.eclipse.jpt.common.utility.factory.InterruptibleFactory;
+import org.eclipse.jpt.common.utility.internal.ClassTools;
+import org.eclipse.jpt.common.utility.internal.closure.BiClosureAdapter;
+import org.eclipse.jpt.common.utility.internal.closure.BiClosureTools;
+import org.eclipse.jpt.common.utility.internal.closure.BiClosureWrapper;
+import org.eclipse.jpt.common.utility.internal.closure.InterruptibleBiClosureAdapter;
+import org.eclipse.jpt.common.utility.internal.closure.InterruptibleBiClosureWrapper;
+import org.eclipse.jpt.common.utility.internal.closure.ThreadLocalBiClosure;
+import org.eclipse.jpt.common.utility.internal.closure.ThreadLocalInterruptibleBiClosure;
+import org.eclipse.jpt.common.utility.internal.exception.CollectingExceptionHandler;
+import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
+import org.eclipse.jpt.common.utility.internal.reference.SimpleIntReference;
+import org.eclipse.jpt.common.utility.reference.ModifiableIntReference;
+import org.eclipse.jpt.common.utility.tests.internal.TestTools;
+import org.eclipse.jpt.common.utility.tests.internal.closure.ClosureToolsTests.LocalCommand;
+import org.eclipse.jpt.common.utility.tests.internal.closure.ClosureToolsTests.LocalFactory;
+import org.eclipse.jpt.common.utility.tests.internal.closure.ClosureToolsTests.LocalInterruptibleCommand;
+import org.eclipse.jpt.common.utility.tests.internal.closure.ClosureToolsTests.LocalInterruptibleFactory;
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class BiClosureToolsTests
+ extends TestCase
+{
+ public BiClosureToolsTests(String name) {
+ super(name);
+ }
+
+ public void testAdaptCommand_execute() {
+ LocalCommand command = new LocalCommand();
+ BiClosure closure = BiClosureTools.adapt(command);
+ closure.execute(Boolean.FALSE, Boolean.TRUE);
+ assertEquals(1, command.count);
+ }
+
+ public void testAdaptCommand_toString() {
+ LocalCommand command = new LocalCommand();
+ BiClosure closure = BiClosureTools.adapt(command);
+ assertTrue(closure.toString().indexOf("CommandBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalCommand") != -1);
+ }
+
+ public void testAdaptCommand_NPE() {
+ boolean exCaught = false;
+ try {
+ BiClosure closure = BiClosureTools.adapt((Command) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptInterruptibleCommand_execute() throws Exception {
+ LocalInterruptibleCommand command = new LocalInterruptibleCommand();
+ InterruptibleBiClosure closure = BiClosureTools.adapt(command);
+ closure.execute(Boolean.FALSE, Boolean.TRUE);
+ assertEquals(1, command.count);
+ }
+
+ public void testAdaptInterruptibleCommand_toString() throws Exception {
+ LocalInterruptibleCommand command = new LocalInterruptibleCommand();
+ InterruptibleBiClosure closure = BiClosureTools.adapt(command);
+ assertTrue(closure.toString().indexOf("InterruptibleCommandBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleCommand") != -1);
+ }
+
+ public void testAdaptInterruptibleCommand_NPE() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure closure = BiClosureTools.adapt((InterruptibleCommand) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptFactory_execute() throws Exception {
+ LocalFactory factory = new LocalFactory<>();
+ BiClosure closure = BiClosureTools.adapt(factory);
+ closure.execute(Boolean.FALSE, Boolean.TRUE);
+ assertEquals(1, factory.count);
+ }
+
+ public void testAdaptFactory_toString() throws Exception {
+ LocalFactory factory = new LocalFactory<>();
+ BiClosure closure = BiClosureTools.adapt(factory);
+ assertTrue(closure.toString().indexOf("FactoryBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalFactory") != -1);
+ }
+
+ public void testAdaptFactory_NPE() {
+ boolean exCaught = false;
+ try {
+ BiClosure closure = BiClosureTools.adapt((Factory) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testAdaptInterruptibleFactory_execute() throws Exception {
+ LocalInterruptibleFactory factory = new LocalInterruptibleFactory<>();
+ InterruptibleBiClosure closure = BiClosureTools.adapt(factory);
+ closure.execute(Boolean.FALSE, Boolean.TRUE);
+ assertEquals(1, factory.count);
+ }
+
+ public void testAdaptInterruptibleFactory_toString() throws Exception {
+ LocalInterruptibleFactory factory = new LocalInterruptibleFactory<>();
+ InterruptibleBiClosure closure = BiClosureTools.adapt(factory);
+ assertTrue(closure.toString().indexOf("InterruptibleFactoryBiClosure") != -1);
+ assertTrue(closure.toString().indexOf("LocalInterruptibleFactory") != -1);
+ }
+
+ public void testAdaptInterruptibleFactory_NPE() {
+ boolean exCaught = false;
+ try {
+ InterruptibleBiClosure closure = BiClosureTools.adapt((InterruptibleFactory) null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testNullBiClosure_execute() throws Exception {
+ BiClosure closure = BiClosureTools.nullBiClosure();
+ closure.execute(Boolean.FALSE, null);
+ assertNotNull(closure);
+ }
+
+ public void testNullBiClosure_toString() throws Exception {
+ BiClosure closure = BiClosureTools.nullBiClosure();
+ assertTrue(closure.toString().indexOf("NullBiClosure") != -1);
+ }
+
+ public void testNullBiClosure_serialization() throws Exception {
+ BiClosure closure = BiClosureTools.nullBiClosure();
+ Object clone = TestTools.serialize(closure);
+ assertSame(closure, clone);
+ }
+
+ public void testThreadLocalBiClosure_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ final ThreadLocalBiClosure closure = BiClosureTools.threadLocalBiClosure();
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(0, ref.getValue());
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ closure.set(new BiClosureAdapter() {
+ @Override
+ public void execute(Integer integer1, Integer integer2) {
+ ref.add(integer1.intValue());
+ ref.add(integer2.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ }
+ };
+ Thread thread = new Thread(r);
+ thread.start();
+ thread.join();
+ assertEquals(5, ref.getValue());
+ }
+
+ public void testThreadLocalBiClosure_toString() {
+ ThreadLocalBiClosure closure = BiClosureTools.threadLocalBiClosure();
+ assertTrue(closure.toString().indexOf("NullBiClosure") != -1);
+ }
+
+ public void testThreadLocalBiClosure_NPE() {
+ boolean exCaught = false;
+ try {
+ ThreadLocalBiClosure closure = BiClosureTools.threadLocalBiClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testThreadLocalInterruptibleBiClosure_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ final ThreadLocalInterruptibleBiClosure closure = BiClosureTools.threadLocalInterruptibleBiClosure();
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(0, ref.getValue());
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ closure.set(new BiClosureAdapter() {
+ @Override
+ public void execute(Integer integer1, Integer integer2) {
+ ref.add(integer1.intValue());
+ ref.add(integer2.intValue());
+ }
+ });
+ try {
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ } catch (InterruptedException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ };
+ Thread thread = new Thread(r);
+ thread.start();
+ thread.join();
+ assertEquals(5, ref.getValue());
+ }
+
+ public void testThreadLocalInterruptibleBiClosure_toString() {
+ ThreadLocalInterruptibleBiClosure closure = BiClosureTools.threadLocalInterruptibleBiClosure();
+ assertTrue(closure.toString().indexOf("NullBiClosure") != -1);
+ }
+
+ public void testThreadLocalInterruptibleBiClosure_NPE() {
+ boolean exCaught = false;
+ try {
+ ThreadLocalInterruptibleBiClosure closure = BiClosureTools.threadLocalInterruptibleBiClosure(null);
+ fail("bogus: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testWrap_execute() throws Exception {
+ final ModifiableIntReference ref = new SimpleIntReference();
+ BiClosureWrapper closure = BiClosureTools.wrap(new BiClosureAdapter() {
+ @Override
+ public void execute(Integer argument1, Integer argument2) {
+ ref.add(argument1.intValue());
+ ref.add(argument2.intValue());
+ }
+ });
+ closure.execute(Integer.valueOf(3), Integer.valueOf(2));
+ assertEquals(5, ref.getValue());
+ closure.setClosure(new BiClosureAdapter