diff options
author | Brian Vosburgh | 2016-06-17 15:54:49 +0000 |
---|---|---|
committer | Brian Vosburgh | 2017-05-18 22:37:09 +0000 |
commit | 3a8dfb1cc77dd0abeea0c9f6f39f2694fc9927bc (patch) | |
tree | 265a37948b38abf4bd7aa6b9c55011ec89de2976 /common/plugins | |
parent | ca0a01ec044bf1b071ece5c16913d864e4513298 (diff) | |
download | webtools.dali-3a8dfb1cc77dd0abeea0c9f6f39f2694fc9927bc.tar.gz webtools.dali-3a8dfb1cc77dd0abeea0c9f6f39f2694fc9927bc.tar.xz webtools.dali-3a8dfb1cc77dd0abeea0c9f6f39f2694fc9927bc.zip |
add [Nullable]BooleanClosure
Diffstat (limited to 'common/plugins')
3 files changed, 119 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); + } +} |