From 38e9a22f959634577e465e1530850576984759d1 Mon Sep 17 00:00:00 2001 From: Brian Vosburgh Date: Tue, 5 Jul 2016 15:21:26 -0400 Subject: clean up PVM null checks etc. --- .../jpt/common/ui/internal/widgets/Pane.java | 2 +- .../model/value/PropertyValueModelTools.java | 127 +++++++++++++++++++-- .../model/value/swing/RadioButtonModelAdapter.java | 2 +- .../model/value/DoublePropertyValueModelTests.java | 3 - 4 files changed, 121 insertions(+), 13 deletions(-) diff --git a/common/plugins/org.eclipse.jpt.common.ui/src/org/eclipse/jpt/common/ui/internal/widgets/Pane.java b/common/plugins/org.eclipse.jpt.common.ui/src/org/eclipse/jpt/common/ui/internal/widgets/Pane.java index 4081b35ae9..ce0a8067dd 100644 --- a/common/plugins/org.eclipse.jpt.common.ui/src/org/eclipse/jpt/common/ui/internal/widgets/Pane.java +++ b/common/plugins/org.eclipse.jpt.common.ui/src/org/eclipse/jpt/common/ui/internal/widgets/Pane.java @@ -306,7 +306,7 @@ public abstract class Pane { /** * Convenience method for sub-classes. - * Wrap the pane's {@link #subjectModel} in a {@link #buildIsNotNullModel(PropertyValueModel)}; + * Wrap the pane's {@link #subjectModel} in a {@link PropertyValueModelTools#valueIsNotNull(PropertyValueModel)}; * i.e. a model that returns whether the subject is null. */ protected PropertyValueModel buildSubjectIsNotNullModel() { diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java index 877af5f51a..f8de8fc4e4 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/PropertyValueModelTools.java @@ -31,7 +31,7 @@ public final class PropertyValueModelTools { * is null. */ public static PropertyValueModel valueIsNull(PropertyValueModel propertyModel) { - return valueIsInSet(propertyModel, PredicateTools.isNull()); + return valueIsInSet_(propertyModel, PredicateTools.isNull()); } /** @@ -40,7 +40,7 @@ public final class PropertyValueModelTools { * is not null. */ public static PropertyValueModel valueIsNotNull(PropertyValueModel propertyModel) { - return valueIsInSet(propertyModel, PredicateTools.isNotNull()); + return valueIsInSet_(propertyModel, PredicateTools.isNotNull()); } /** @@ -49,7 +49,7 @@ public final class PropertyValueModelTools { * equals the specified value. */ public static PropertyValueModel valueEquals(PropertyValueModel propertyModel, Object value) { - return valueIsInSet(propertyModel, PredicateTools.isEqual(value)); + return valueIsInSet_(propertyModel, PredicateTools.isEqual(value)); } /** @@ -58,7 +58,7 @@ public final class PropertyValueModelTools { * does not equal the specified value. */ public static PropertyValueModel valueNotEquals(PropertyValueModel propertyModel, Object value) { - return valueIsInSet(propertyModel, PredicateTools.isNotEqual(value)); + return valueIsInSet_(propertyModel, PredicateTools.isNotEqual(value)); } /** @@ -67,7 +67,7 @@ public final class PropertyValueModelTools { * is identical to the specified value. */ public static PropertyValueModel valueIsIdentical(PropertyValueModel propertyModel, Object value) { - return valueIsInSet(propertyModel, PredicateTools.isIdentical(value)); + return valueIsInSet_(propertyModel, PredicateTools.isIdentical(value)); } /** @@ -76,16 +76,52 @@ public final class PropertyValueModelTools { * is not identical to the specified value. */ public static PropertyValueModel valueIsNotIdentical(PropertyValueModel propertyModel, Object value) { - return valueIsInSet(propertyModel, PredicateTools.isNotIdentical(value)); + return valueIsInSet_(propertyModel, PredicateTools.isNotIdentical(value)); } /** * Construct a property value model adapter for the specified * property value model that returns whether the property's value * is in the set defined by the specified predicate. + *

+ * NB: If specified model's value is null, + * the returned model's value will also be a null + * {@link Boolean}; and the value will never be passed to the specified + * predicate. + * @see #valueIsInSet_(PropertyValueModel, Predicate) + * @see #valueIsInSet(PropertyValueModel, Predicate, Boolean) */ public static PropertyValueModel valueIsInSet(PropertyValueModel propertyModel, Predicate predicate) { - return transform(propertyModel, TransformerTools.adapt(predicate)); + return valueIsInSet(propertyModel, predicate, null); + } + + /** + * Construct a property value model adapter for the specified + * property value model that returns whether the property's value + * is in the set defined by the specified predicate. + *

+ * NB: If specified model's value is null, + * the returned model's value will be the specified null result; + * and the value will never be passed to the specified predicate. + * @see #valueIsInSet(PropertyValueModel, Predicate) + * @see #valueIsInSet_(PropertyValueModel, Predicate) + */ + public static PropertyValueModel valueIsInSet(PropertyValueModel propertyModel, Predicate predicate, Boolean nullResult) { + return transform_(propertyModel, TransformerTools.adapt(predicate, nullResult)); + } + + /** + * Construct a property value model adapter for the specified + * property value model that returns whether the property's value + * is in the set defined by the specified predicate. + *

+ * NB: The specified predicate must be able to + * handle a null variable. + * @see #valueIsInSet(PropertyValueModel, Predicate) + * @see #valueIsInSet(PropertyValueModel, Predicate, Boolean) + */ + public static PropertyValueModel valueIsInSet_(PropertyValueModel propertyModel, Predicate predicate) { + return transform_(propertyModel, TransformerTools.adapt(predicate)); } @@ -155,7 +191,7 @@ public final class PropertyValueModelTools { * @see PluggablePropertyValueModel */ public static PropertyValueModel nullCheck(PropertyValueModel propertyModel, V nullValue) { - return transform(propertyModel, TransformerTools.nullCheck(nullValue)); + return transform_(propertyModel, TransformerTools.nullCheck(nullValue)); } @@ -165,28 +201,72 @@ public final class PropertyValueModelTools { * Construct a property value model that wraps the specified * property value model and transforms its value with the specified * transformer. + *

+ * NB: The specified transformer will never be passed a null input. + * Instead, a null input will be transformed into a null output. * @see PluggablePropertyValueModel */ public static PropertyValueModel transform(PropertyValueModel propertyModel, Transformer transformer) { return propertyValueModel(pluggablePropertyValueModelAdapterFactory(propertyModel, transformer)); } + /** + * Construct a property value model that wraps the specified + * property value model and transforms its value with the specified + * transformer. + *

+ * NB: The specified transformer must be able to handle a null input. + * @see PluggablePropertyValueModel + */ + public static PropertyValueModel transform_(PropertyValueModel propertyModel, Transformer transformer) { + return propertyValueModel(pluggablePropertyValueModelAdapterFactory_(propertyModel, transformer)); + } + /** * Construct a modifiable property value model that wraps the specified * property value model and transforms its value with the specified * transformer. The specified closure is invoked when the model's value is set. + *

+ * NB: The specified transformer will never be passed a null input. + * Instead, a null input will be transformed into a null output. * @see PluggablePropertyValueModel */ public static ModifiablePropertyValueModel transform(PropertyValueModel propertyModel, Transformer transformer, Closure setValueClosure) { return pluggableModifiablePropertyValueModel(pluggablePropertyValueModelAdapterFactory(propertyModel, transformer), setValueClosure); } + /** + * Construct a modifiable property value model that wraps the specified + * property value model and transforms its value with the specified + * transformer. The specified closure is invoked when the model's value is set. + *

+ * NB: The specified transformer must be able to handle a null input. + * @see PluggablePropertyValueModel + */ + public static ModifiablePropertyValueModel transform_(PropertyValueModel propertyModel, Transformer transformer, Closure setValueClosure) { + return pluggableModifiablePropertyValueModel(pluggablePropertyValueModelAdapterFactory_(propertyModel, transformer), setValueClosure); + } + /** * Construct a pluggable property value model adapter factory for the specified * property value model and transformer. + *

+ * NB: The specified transformer will never be passed a null input. + * Instead, a null input will be transformed into a null output. * @see PluggablePropertyValueModel */ public static PluggablePropertyValueModel.Adapter.Factory pluggablePropertyValueModelAdapterFactory(PropertyValueModel propertyModel, Transformer transformer) { + return pluggablePropertyValueModelAdapterFactory_(propertyModel, TransformerTools.nullCheck(transformer)); + } + + /** + * Construct a pluggable property value model adapter factory for the specified + * property value model and transformer. + *

+ * NB: The specified transformer must be able to handle a null input. + * @see PluggablePropertyValueModel + */ + public static PluggablePropertyValueModel.Adapter.Factory pluggablePropertyValueModelAdapterFactory_(PropertyValueModel propertyModel, Transformer transformer) { return new PropertyPluggablePropertyValueModelAdapter.Factory<>(propertyModel, transformer); } @@ -194,18 +274,47 @@ public final class PropertyValueModelTools { * Construct a property value model that wraps the specified * property value model and transforms its value with the specified * transformer. + *

+ * NB: The specified transformers will never be passed a null input. + * Instead, a null input will be transformed into a null output. * @see PluggablePropertyValueModel */ public static ModifiablePropertyValueModel transform(ModifiablePropertyValueModel propertyModel, Transformer getTransformer, Transformer setTransformer) { return modifiablePropertyValueModel(pluggableModifiablePropertyValueModelAdapterFactory(propertyModel, getTransformer, setTransformer)); } + /** + * Construct a property value model that wraps the specified + * property value model and transforms its value with the specified + * transformer. + *

+ * NB: The specified transformers must be able to handle a null input. + * @see PluggablePropertyValueModel + */ + public static ModifiablePropertyValueModel transform_(ModifiablePropertyValueModel propertyModel, Transformer getTransformer, Transformer setTransformer) { + return modifiablePropertyValueModel(pluggableModifiablePropertyValueModelAdapterFactory_(propertyModel, getTransformer, setTransformer)); + } + /** * Construct a pluggable property value model adapter factory for the specified * property value model and transformer. + *

+ * NB: The specified transformers will never be passed a null input. + * Instead, a null input will be transformed into a null output. * @see PluggablePropertyValueModel */ public static PluggableModifiablePropertyValueModel.Adapter.Factory pluggableModifiablePropertyValueModelAdapterFactory(ModifiablePropertyValueModel propertyModel, Transformer getTransformer, Transformer setTransformer) { + return pluggableModifiablePropertyValueModelAdapterFactory_(propertyModel, TransformerTools.nullCheck(getTransformer), TransformerTools.nullCheck(setTransformer)); + } + + /** + * Construct a pluggable property value model adapter factory for the specified + * property value model and transformer. + *

+ * NB: The specified transformers must be able to handle a null input. + * @see PluggablePropertyValueModel + */ + public static PluggableModifiablePropertyValueModel.Adapter.Factory pluggableModifiablePropertyValueModelAdapterFactory_(ModifiablePropertyValueModel propertyModel, Transformer getTransformer, Transformer setTransformer) { return new PropertyPluggableModifiablePropertyValueModelAdapter.Factory<>(propertyModel, getTransformer, setTransformer); } @@ -297,6 +406,8 @@ public final class PropertyValueModelTools { /** * Return a transformer that converts a property value model to its value. + * If the property value model is null, the transformer throws + * a {@link NullPointerException}. * @see PropertyValueModel#VALUE_TRANSFORMER */ @SuppressWarnings("unchecked") diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/swing/RadioButtonModelAdapter.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/swing/RadioButtonModelAdapter.java index 33fcb3213b..2e4c9ec29f 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/swing/RadioButtonModelAdapter.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/swing/RadioButtonModelAdapter.java @@ -128,7 +128,7 @@ public class RadioButtonModelAdapter } private static PluggablePropertyValueModel.Adapter.Factory buildAdapterFactory(PropertyValueModel sharedValueModel, V buttonValue) { - return PropertyValueModelTools.pluggablePropertyValueModelAdapterFactory(sharedValueModel, new GetTransformer<>(buttonValue)); + return PropertyValueModelTools.pluggablePropertyValueModelAdapterFactory_(sharedValueModel, new GetTransformer<>(buttonValue)); } public void setValue(Boolean value) { diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoublePropertyValueModelTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoublePropertyValueModelTests.java index d896e6c6c9..c933411190 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoublePropertyValueModelTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/model/value/DoublePropertyValueModelTests.java @@ -62,9 +62,6 @@ public class DoublePropertyValueModelTests } protected SimplePropertyValueModel getValueModel(String key) { - if (key == null) { - return null; - } SimplePropertyValueModel valueModel = this.valueModels.get(key); if (valueModel == null) { valueModel = new SimplePropertyValueModel<>(key + key); -- cgit v1.2.3