Skip to main content
summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorBrian Vosburgh2016-06-17 15:54:49 +0000
committerBrian Vosburgh2017-05-18 22:37:09 +0000
commit3a8dfb1cc77dd0abeea0c9f6f39f2694fc9927bc (patch)
tree265a37948b38abf4bd7aa6b9c55011ec89de2976 /common
parentca0a01ec044bf1b071ece5c16913d864e4513298 (diff)
downloadwebtools.dali-3a8dfb1cc77dd0abeea0c9f6f39f2694fc9927bc.tar.gz
webtools.dali-3a8dfb1cc77dd0abeea0c9f6f39f2694fc9927bc.tar.xz
webtools.dali-3a8dfb1cc77dd0abeea0c9f6f39f2694fc9927bc.zip
add [Nullable]BooleanClosure
Diffstat (limited to 'common')
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/BooleanClosure.java46
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java30
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullableBooleanClosure.java43
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BooleanClosureTests.java81
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java33
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/NullableBooleanClosureTests.java80
6 files changed, 313 insertions, 0 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/BooleanClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/BooleanClosure.java
new file mode 100644
index 0000000000..957e6009c9
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/BooleanClosure.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * 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.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.Closure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Adapt a <code>boolean</code> "closure" to the {@link Closure} interface.
+ * The closure's argument can never be <code>null</code>.
+ */
+public class BooleanClosure
+ implements Closure<Boolean>
+{
+ private final Adapter adapter;
+
+
+ public BooleanClosure(Adapter adapter) {
+ super();
+ if (adapter == null) {
+ throw new NullPointerException();
+ }
+ this.adapter = adapter;
+ }
+
+ public void execute(Boolean argument) {
+ // a null argument will result in a NPE
+ this.adapter.execute(argument.booleanValue());
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.adapter);
+ }
+
+ public interface Adapter {
+ void execute(boolean argument);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java
index a6b96476d7..83a4a8f576 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/ClosureTools.java
@@ -268,6 +268,36 @@ public final class ClosureTools {
}
+ // ********** boolean **********
+
+ /**
+ * Return a closure that adapts the specified <code>boolean</code> "closure".
+ * If the closure's argument is <code>null</code>, the closure will throw a
+ * {@link NullPointerException}.
+ */
+ public static Closure<Boolean> booleanClosure(BooleanClosure.Adapter adapter) {
+ return new BooleanClosure(adapter);
+ }
+
+ /**
+ * Return a closure that adapts the specified <code>boolean</code> "closure".
+ * If the closure's argument is <code>null</code>, the <code>boolean</code> "closure"
+ * will be passed an argument of <code>false</code>.
+ */
+ public static Closure<Boolean> nullableBooleanClosure(BooleanClosure.Adapter adapter) {
+ return nullableBooleanClosure(adapter, false);
+ }
+
+ /**
+ * Return a closure that adapts the specified <code>boolean</code> "closure".
+ * If the closure's argument is <code>null</code>, the <code>boolean</code> "closure"
+ * will be passed the specified null argument value.
+ */
+ public static Closure<Boolean> nullableBooleanClosure(BooleanClosure.Adapter adapter, boolean nullArgumentValue) {
+ return new NullableBooleanClosure(adapter, nullArgumentValue);
+ }
+
+
// ********** disabled **********
/**
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullableBooleanClosure.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullableBooleanClosure.java
new file mode 100644
index 0000000000..58eb95e9f0
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/closure/NullableBooleanClosure.java
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * 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.internal.closure;
+
+import org.eclipse.jpt.common.utility.closure.Closure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * Adapt a <code>boolean</code> "closure" to the {@link Closure} interface.
+ * The closure's argument can never be <code>null</code>.
+ */
+public class NullableBooleanClosure
+ implements Closure<Boolean>
+{
+ private final BooleanClosure.Adapter adapter;
+ private final boolean nullArgumentValue;
+
+
+ public NullableBooleanClosure(BooleanClosure.Adapter adapter, boolean nullArgumentValue) {
+ super();
+ if (adapter == null) {
+ throw new NullPointerException();
+ }
+ this.adapter = adapter;
+ this.nullArgumentValue = nullArgumentValue;
+ }
+
+ public void execute(Boolean argument) {
+ this.adapter.execute((argument != null) ? argument.booleanValue() : this.nullArgumentValue);
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.adapter);
+ }
+}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BooleanClosureTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BooleanClosureTests.java
new file mode 100644
index 0000000000..5ad60d9955
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/BooleanClosureTests.java
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * 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 org.eclipse.jpt.common.utility.closure.Closure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+import org.eclipse.jpt.common.utility.internal.closure.BooleanClosure;
+import org.eclipse.jpt.common.utility.internal.closure.ClosureTools;
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class BooleanClosureTests
+ extends TestCase
+{
+ public BooleanClosureTests(String name) {
+ super(name);
+ }
+
+ public void testExecute() {
+ Adapter adapter = new Adapter();
+ assertNull(adapter.value);
+ Closure<Boolean> closure = ClosureTools.booleanClosure(adapter);
+ closure.execute(Boolean.TRUE);
+ assertEquals("foo", adapter.value);
+ closure.execute(Boolean.FALSE);
+ assertEquals("bar", adapter.value);
+ }
+
+ public void testExecute_NPE() {
+ Adapter adapter = new Adapter();
+ assertNull(adapter.value);
+ Closure<Boolean> closure = ClosureTools.booleanClosure(adapter);
+ closure.execute(Boolean.TRUE);
+ assertEquals("foo", adapter.value);
+ boolean exCaught = false;
+ try {
+ closure.execute(null);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testCtor_NPE() {
+ boolean exCaught = false;
+ try {
+ Closure<Boolean> closure = ClosureTools.booleanClosure(null);
+ fail("bogus closure: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testToString() {
+ Adapter adapter = new Adapter();
+ assertNull(adapter.value);
+ Closure<Boolean> closure = ClosureTools.booleanClosure(adapter);
+ assertTrue(closure.toString().indexOf("Adapter") != -1);
+ }
+
+ public static class Adapter
+ implements BooleanClosure.Adapter
+ {
+ String value = null;
+ public void execute(boolean argument) {
+ this.value = (argument ? "foo" : "bar");
+ }
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this);
+ }
+ }
+}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java
new file mode 100644
index 0000000000..40f8b348ec
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/JptCommonUtilityClosureTests.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * 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 junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * decentralize test creation code
+ */
+public class JptCommonUtilityClosureTests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(JptCommonUtilityClosureTests.class.getPackage().getName());
+
+ suite.addTestSuite(BooleanClosureTests.class);
+ suite.addTestSuite(NullableBooleanClosureTests.class);
+
+ return suite;
+ }
+
+ private JptCommonUtilityClosureTests() {
+ super();
+ throw new UnsupportedOperationException();
+ }
+}
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/NullableBooleanClosureTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/NullableBooleanClosureTests.java
new file mode 100644
index 0000000000..74b775bfca
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/closure/NullableBooleanClosureTests.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * 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 org.eclipse.jpt.common.utility.closure.Closure;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+import org.eclipse.jpt.common.utility.internal.closure.BooleanClosure;
+import org.eclipse.jpt.common.utility.internal.closure.ClosureTools;
+import junit.framework.TestCase;
+
+@SuppressWarnings("nls")
+public class NullableBooleanClosureTests
+ extends TestCase
+{
+ public NullableBooleanClosureTests(String name) {
+ super(name);
+ }
+
+ public void testExecute() {
+ Adapter adapter = new Adapter();
+ assertNull(adapter.value);
+ Closure<Boolean> closure = ClosureTools.nullableBooleanClosure(adapter);
+ closure.execute(Boolean.TRUE);
+ assertEquals("foo", adapter.value);
+ closure.execute(Boolean.FALSE);
+ assertEquals("bar", adapter.value);
+ }
+
+ public void testExecute_NPE() {
+ Adapter adapter = new Adapter();
+ assertNull(adapter.value);
+ Closure<Boolean> closure = ClosureTools.nullableBooleanClosure(adapter);
+ closure.execute(Boolean.TRUE);
+ assertEquals("foo", adapter.value);
+ closure.execute(null);
+ assertEquals("bar", adapter.value);
+ closure.execute(Boolean.TRUE);
+ assertEquals("foo", adapter.value);
+ closure.execute(Boolean.FALSE);
+ assertEquals("bar", adapter.value);
+ }
+
+ public void testCtor_NPE() {
+ boolean exCaught = false;
+ try {
+ Closure<Boolean> closure = ClosureTools.nullableBooleanClosure(null);
+ fail("bogus closure: " + closure);
+ } catch (NullPointerException ex) {
+ exCaught = true;
+ }
+ assertTrue(exCaught);
+ }
+
+ public void testToString() {
+ Adapter adapter = new Adapter();
+ assertNull(adapter.value);
+ Closure<Boolean> closure = ClosureTools.nullableBooleanClosure(adapter);
+ assertTrue(closure.toString().indexOf("Adapter") != -1);
+ }
+
+ public static class Adapter
+ implements BooleanClosure.Adapter
+ {
+ String value = null;
+ public void execute(boolean argument) {
+ this.value = (argument ? "foo" : "bar");
+ }
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this);
+ }
+ }
+}

Back to the top