diff options
author | Brian Vosburgh | 2016-06-23 18:47:39 +0000 |
---|---|---|
committer | Brian Vosburgh | 2017-05-18 22:37:23 +0000 |
commit | 8077ad16826b0a6769ede4af962a67fa4c669f85 (patch) | |
tree | 83719fb2a71be605074ce45f21f41d9bfdd686be /common | |
parent | ca6d22ee2fe1336e0d1b4750d7417317965856ef (diff) | |
download | webtools.dali-8077ad16826b0a6769ede4af962a67fa4c669f85.tar.gz webtools.dali-8077ad16826b0a6769ede4af962a67fa4c669f85.tar.xz webtools.dali-8077ad16826b0a6769ede4af962a67fa4c669f85.zip |
add FilteringTransformer and NullCheckTransformer
Diffstat (limited to 'common')
6 files changed, 288 insertions, 11 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/FilteringTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/FilteringTransformer.java new file mode 100644 index 0000000000..6e7c243bf4 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/FilteringTransformer.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.predicate.Predicate; +import org.eclipse.jpt.common.utility.transformer.Transformer; + +/** + * Tranformer that "filters" its input. + * If the input is a member of the set defined by the transformer's filter, + * the transformer simply returns the input; + * otherwise, the transformer will return the default output. + * + * @param <I> input: the type of the object passed to and + * returned by the transformer + */ +public class FilteringTransformer<I> + implements Transformer<I, I> +{ + private final Predicate<? super I> filter; + private final I defaultOutput; + + + public FilteringTransformer(Predicate<? super I> filter, I defaultOutput) { + super(); + if (filter == null) { + throw new NullPointerException(); + } + this.filter = filter; + this.defaultOutput = defaultOutput; + } + + public I transform(I input) { + return this.filter.evaluate(input) ? input : this.defaultOutput; + } + + public Predicate<? super I> getFilter() { + return this.filter; + } + + public I getDefaultOutput() { + return this.defaultOutput; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.filter); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/NullCheckTransformer.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/NullCheckTransformer.java new file mode 100644 index 0000000000..e427ece3e1 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/NullCheckTransformer.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * 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.transformer; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.transformer.Transformer; + +/** + * Tranformer that checks for <code>null</code> input. + * If the input is <code>null</code>, + * the transformer will return the configured output value; + * otherwise, it will simply return the input. + * + * @param <I> input: the type of the object passed to and + * returned by the transformer + */ +public class NullCheckTransformer<I> + implements Transformer<I, I> +{ + private final I nullOutput; + + + public NullCheckTransformer(I nullOutput) { + super(); + this.nullOutput = nullOutput; + } + + public I transform(I input) { + return (input != null) ? input : this.nullOutput; + } + + public I getNullOutput() { + return this.nullOutput; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.nullOutput); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformerTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformerTools.java index 40fc3f499a..205e5b74e1 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformerTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformerTools.java @@ -185,7 +185,7 @@ public final class TransformerTools { * @see #notBooleanTransformer() */ public static Transformer<Boolean, Boolean> notBooleanTransformer(Boolean nullBoolean) { - return nullCheck(notBooleanTransformer(), nullBoolean); + return (nullBoolean == null) ? notBooleanTransformer() : nullCheck(notBooleanTransformer(), nullBoolean); } /** @@ -256,9 +256,9 @@ public final class TransformerTools { * forwarded to the wrapped predicate) * @see PredicateTransformer * @see #adapt(Predicate, Boolean) - * @see #adapt_(Predicate) + * @see #adapt(Predicate) */ - public static <I> Transformer<I, Boolean> adapt(Predicate<? super I> predicate) { + public static <I> Transformer<I, Boolean> adapt_(Predicate<? super I> predicate) { return adapt(predicate, null); } @@ -272,11 +272,11 @@ public final class TransformerTools { * @param <I> input: the type of the object passed to the transformer (and * forwarded to the wrapped predicate) * @see PredicateTransformer - * @see #adapt(Predicate) * @see #adapt_(Predicate) + * @see #adapt(Predicate) */ public static <I> Transformer<I, Boolean> adapt(Predicate<? super I> predicate, Boolean nullBoolean) { - return nullCheck(adapt_(predicate), nullBoolean); + return nullCheck(adapt(predicate), nullBoolean); } /** @@ -288,10 +288,10 @@ public final class TransformerTools { * @param <I> input: the type of the object passed to the transformer (and * forwarded to the wrapped predicate) * @see PredicateTransformer - * @see #adapt(Predicate) + * @see #adapt_(Predicate) * @see #adapt(Predicate, Boolean) */ - public static <I> Transformer<I, Boolean> adapt_(Predicate<? super I> predicate) { + public static <I> Transformer<I, Boolean> adapt(Predicate<? super I> predicate) { return new PredicateTransformer<>(predicate); } @@ -447,7 +447,7 @@ public final class TransformerTools { * Transformer that converts a collection into a boolean * that indicates whether the collection is <em>not</em> empty. */ - public static final Transformer<Collection<?>, Boolean> COLLECTION_IS_NOT_EMPTY_TRANSFORMER = adapt_(PredicateTools.collectionIsNotEmptyPredicate()); + public static final Transformer<Collection<?>, Boolean> COLLECTION_IS_NOT_EMPTY_TRANSFORMER = adapt(PredicateTools.collectionIsNotEmptyPredicate()); /** * Return a transformer that converts a collection into a boolean @@ -461,7 +461,7 @@ public final class TransformerTools { * Transformer that converts a collection into a boolean * that indicates whether the collection is empty. */ - public static final Transformer<Collection<?>, Boolean> COLLECTION_IS_EMPTY_TRANSFORMER = adapt_(PredicateTools.collectionIsEmptyPredicate()); + public static final Transformer<Collection<?>, Boolean> COLLECTION_IS_EMPTY_TRANSFORMER = adapt(PredicateTools.collectionIsEmptyPredicate()); /** * Return a transformer that converts a collection into a boolean @@ -475,7 +475,7 @@ public final class TransformerTools { * Transformer that converts a collection into a boolean * that indicates whether the collection contains exactly one element. */ - public static final Transformer<Collection<?>, Boolean> COLLECTION_CONTAINS_SINGLE_ELEMENT_TRANSFORMER = adapt_(PredicateTools.collectionContainsSingleElementPredicate()); + public static final Transformer<Collection<?>, Boolean> COLLECTION_CONTAINS_SINGLE_ELEMENT_TRANSFORMER = adapt(PredicateTools.collectionContainsSingleElementPredicate()); /** * Return a transformer that converts a collection into a boolean @@ -486,7 +486,7 @@ public final class TransformerTools { } private static Transformer<Collection<?>, Boolean> collectionSizeEqualsTransformer_(int size) { - return adapt_(PredicateTools.collectionSizeEqualsPredicate(size)); + return adapt(PredicateTools.collectionSizeEqualsPredicate(size)); } @@ -619,6 +619,21 @@ public final class TransformerTools { } /** + * Return a tranformer that checks for <code>null</code> input. + * If the input is <code>null</code>, the transformer will return + * the configured output value; + * otherwise, it will simply return the input. + * @param <I> input: the type of the object passed to and + * returned by the transformer + * @see NullCheckTransformer + * @see #nullCheck(Transformer) + * @see #nullCheck(Transformer, Object) + */ + public static <I> Transformer<I, I> nullCheck(I nullOutput) { + return new NullCheckTransformer<>(nullOutput); + } + + /** * Return a transformer that wraps the specified transformer and checks * for <code>null</code> input before forwarding the input to the specified * transformer. If the input is <code>null</code>, the transformer will @@ -627,6 +642,7 @@ public final class TransformerTools { * @param <O> output: the type of the object returned by the transformer * @see NullCheckTransformerWrapper * @see #nullCheck(Transformer, Object) + * @see #nullCheck(Object) */ public static <I, O> Transformer<I, O> nullCheck(Transformer<? super I, ? extends O> transformer) { return nullCheck(transformer, null); @@ -641,6 +657,7 @@ public final class TransformerTools { * @param <O> output: the type of the object returned by the transformer * @see NullCheckTransformerWrapper * @see #nullCheck(Transformer) + * @see #nullCheck(Object) */ public static <I, O> Transformer<I, O> nullCheck(Transformer<? super I, ? extends O> transformer, O nullOutput) { return new NullCheckTransformerWrapper<>(transformer, nullOutput); @@ -798,6 +815,35 @@ public final class TransformerTools { } + // ********** filtering ********** + + /** + * Return a tranformer that "filters" its input. + * If the input is a member of the set defined by the specified filter, + * the transformer simply returns the input; + * otherwise, the transformer will return <code>null</code>. + * + * @param <I> input: the type of the object passed to and + * returned by the transformer + */ + public static <I> Transformer<I, I> filteringTransformer(Predicate<? super I> filter) { + return filteringTransformer(filter, null); + } + + /** + * Return a tranformer that "filters" its input. + * If the input is a member of the set defined by the specified filter, + * the transformer simply returns the input; + * otherwise, the transformer will return the default output. + * + * @param <I> input: the type of the object passed to and + * returned by the transformer + */ + public static <I> Transformer<I, I> filteringTransformer(Predicate<? super I> filter, I defaultOutput) { + return new FilteringTransformer<>(filter, defaultOutput); + } + + // ********** reflection ********** /** diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/FilteringTransformerTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/FilteringTransformerTests.java new file mode 100644 index 0000000000..9f5c198a46 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/FilteringTransformerTests.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.transformer; + +import org.eclipse.jpt.common.utility.internal.predicate.PredicateAdapter; +import org.eclipse.jpt.common.utility.internal.predicate.PredicateTools; +import org.eclipse.jpt.common.utility.internal.transformer.FilteringTransformer; +import org.eclipse.jpt.common.utility.internal.transformer.TransformerTools; +import org.eclipse.jpt.common.utility.predicate.Predicate; +import org.eclipse.jpt.common.utility.transformer.Transformer; +import junit.framework.TestCase; + +@SuppressWarnings("nls") +public class FilteringTransformerTests + extends TestCase +{ + public FilteringTransformerTests(String name) { + super(name); + } + + public void testTransform() { + Predicate<String> nonNullFilter = new Filter(3); + Predicate<String> filter = PredicateTools.nullCheck(nonNullFilter, false); + Transformer<String, String> transformer = TransformerTools.filteringTransformer(filter); + + assertEquals("foo", transformer.transform("foo")); + assertEquals("bar", transformer.transform("bar")); + assertNull(transformer.transform("barbar")); + assertNull(transformer.transform("b")); + assertNull(transformer.transform("")); + assertNull(transformer.transform(null)); + } + + public void testToString() { + Predicate<String> nonNullFilter = new Filter(3); + Predicate<String> filter = PredicateTools.nullCheck(nonNullFilter, false); + Transformer<String, String> transformer = TransformerTools.filteringTransformer(filter); + assertTrue(transformer.toString().indexOf("Filtering") != -1); + } + + public void testGetters() { + Predicate<String> nonNullFilter = new Filter(3); + Predicate<String> filter = PredicateTools.nullCheck(nonNullFilter, false); + FilteringTransformer<String> transformer = (FilteringTransformer<String>) TransformerTools.filteringTransformer(filter); + assertSame(filter, transformer.getFilter()); + assertNull(transformer.getDefaultOutput()); + } + + public void testCtor_NPE() { + boolean exCaught = false; + try { + Transformer<String, String> transformer = TransformerTools.filteringTransformer(null); + fail("bogus: " + transformer); + } catch (NullPointerException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public static class Filter + extends PredicateAdapter<String> + { + public final int length; + public Filter(int length) { + super(); + this.length =length; + } + @Override + public boolean evaluate(String variable) { + return variable.length() == this.length; + } + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/JptCommonUtilityTransformerTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/JptCommonUtilityTransformerTests.java index bf3b780746..526bf09835 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/JptCommonUtilityTransformerTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/JptCommonUtilityTransformerTests.java @@ -25,8 +25,10 @@ public class JptCommonUtilityTransformerTests { suite.addTestSuite(CollectionLastElementTransformerTests.class); suite.addTestSuite(CollectionLastElementTransformer_Tests.class); suite.addTestSuite(CollectionSingleElementTransformerTests.class); + suite.addTestSuite(FilteringTransformerTests.class); suite.addTestSuite(ListLastElementTransformerTests.class); suite.addTestSuite(ListLastElementTransformer_Tests.class); + suite.addTestSuite(NullCheckTransformerTests.class); suite.addTestSuite(XMLStringDecoderTests.class); suite.addTestSuite(XMLStringEncoderTests.class); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/NullCheckTransformerTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/NullCheckTransformerTests.java new file mode 100644 index 0000000000..23ac45c18b --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/transformer/NullCheckTransformerTests.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * 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.transformer; + +import org.eclipse.jpt.common.utility.internal.transformer.NullCheckTransformer; +import org.eclipse.jpt.common.utility.internal.transformer.TransformerTools; +import org.eclipse.jpt.common.utility.transformer.Transformer; +import junit.framework.TestCase; + +@SuppressWarnings("nls") +public class NullCheckTransformerTests + extends TestCase +{ + public NullCheckTransformerTests(String name) { + super(name); + } + + public void testTransform() { + Transformer<String, String> transformer = TransformerTools.nullCheck(""); + + assertEquals("foo", transformer.transform("foo")); + assertEquals("bar", transformer.transform("bar")); + assertEquals("barbar", transformer.transform("barbar")); + assertEquals("b", transformer.transform("b")); + assertEquals("", transformer.transform("")); + assertEquals("", transformer.transform(null)); + } + + public void testToString() { + Transformer<String, String> transformer = TransformerTools.nullCheck(""); + assertTrue(transformer.toString().indexOf("NullCheck") != -1); + } + + public void testGetters() { + NullCheckTransformer<String> transformer = (NullCheckTransformer<String>) TransformerTools.nullCheck(""); + assertEquals("", transformer.getNullOutput()); + } +} |