From 897d8b914a524ce0bc9accbc39c197e0a6295b3e Mon Sep 17 00:00:00 2001 From: Brian Vosburgh Date: Thu, 9 Jun 2016 16:36:55 -0400 Subject: add CollectionSizeEqualsPredicate --- .../predicate/CollectionSizeEqualsPredicate.java | 67 +++++++++++++++++ .../utility/internal/predicate/PredicateTools.java | 84 +++++++++++++++------- .../internal/transformer/TransformerTools.java | 55 ++++++++++++-- .../predicate/CollectionIsEmptyPredicateTests.java | 6 +- .../CollectionSizeEqualsPredicateTests.java | 76 ++++++++++++++++++++ .../predicate/JptCommonUtilityPredicateTests.java | 1 + 6 files changed, 257 insertions(+), 32 deletions(-) create mode 100644 common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/predicate/CollectionSizeEqualsPredicate.java create mode 100644 common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/CollectionSizeEqualsPredicateTests.java (limited to 'common') diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/predicate/CollectionSizeEqualsPredicate.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/predicate/CollectionSizeEqualsPredicate.java new file mode 100644 index 0000000000..b7c9ce5718 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/predicate/CollectionSizeEqualsPredicate.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * 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.predicate; + +import java.io.Serializable; +import java.util.Collection; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.predicate.Predicate; + +/** + * A predicate that will evaluate whether a collection's + * size is equal to a specified size. + * + * @param the type of elements held by the collection + */ +public class CollectionSizeEqualsPredicate + implements Predicate>, Serializable +{ + private final int size; + private static final long serialVersionUID = 1L; + + /** + * Construct a predicate that will evaluate whether a collection's + * size is equal to the specified size. + */ + public CollectionSizeEqualsPredicate(int size) { + super(); + this.size = size; + } + + public boolean evaluate(Collection collection) { + return collection.size() == this.size; + } + + /** + * Return the size used by the predicate. + */ + public int getSize() { + return this.size; + } + + @Override + public boolean equals(Object o) { + if ((o == null) || (o.getClass() != this.getClass())) { + return false; + } + CollectionSizeEqualsPredicate other = (CollectionSizeEqualsPredicate) o; + return this.size == other.size; + } + + @Override + public int hashCode() { + return this.size; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.size); + } +} diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/predicate/PredicateTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/predicate/PredicateTools.java index 326b48c589..ec7a0ec15d 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/predicate/PredicateTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/predicate/PredicateTools.java @@ -17,7 +17,6 @@ import org.eclipse.jpt.common.utility.internal.ClassTools; import org.eclipse.jpt.common.utility.internal.ObjectTools; import org.eclipse.jpt.common.utility.internal.collection.IdentityHashSet; import org.eclipse.jpt.common.utility.internal.iterator.IteratorTools; -import org.eclipse.jpt.common.utility.predicate.CompoundPredicate; import org.eclipse.jpt.common.utility.predicate.Predicate; import org.eclipse.jpt.common.utility.transformer.Transformer; @@ -84,7 +83,7 @@ public final class PredicateTools { * @param the type of objects to be evaluated by the predicate */ public static Predicate isEqual(V criterion) { - return new Equals<>(criterion); + return (criterion == null) ? isNull() : new Equals<>(criterion); } /** @@ -96,7 +95,7 @@ public final class PredicateTools { * @param the type of objects to be evaluated by the predicate */ public static Predicate isNotEqual(V criterion) { - return new NOT<>(isEqual(criterion)); + return (criterion == null) ? isNotNull() : not(isEqual(criterion)); } /** @@ -108,7 +107,7 @@ public final class PredicateTools { * @param the type of objects to be evaluated by the predicate */ public static Predicate isIdentical(V criterion) { - return new IsIdentical<>(criterion); + return (criterion == null) ? isNull() : new IsIdentical<>(criterion); } /** @@ -120,7 +119,7 @@ public final class PredicateTools { * @param the type of objects to be evaluated by the predicate */ public static Predicate isNotIdentical(V criterion) { - return new NOT<>(isIdentical(criterion)); + return (criterion == null) ? isNotNull() : not(isIdentical(criterion)); } @@ -130,7 +129,7 @@ public final class PredicateTools { * Return a predicate that will AND the results of the specified predicates. * @param the type of objects to be evaluated by the predicate */ - public static CompoundPredicate and(Iterable> predicates) { + public static Predicate and(Iterable> predicates) { return and(predicates.iterator()); } @@ -139,8 +138,8 @@ public final class PredicateTools { * @param the type of objects to be evaluated by the predicate */ @SuppressWarnings("unchecked") - public static CompoundPredicate and(Iterator> predicates) { - return and((Predicate[]) IteratorTools.toArray(predicates, EMPTY_ARRAY)); + public static Predicate and(Iterator> predicates) { + return predicates.hasNext() ? and((Predicate[]) IteratorTools.toArray(predicates, EMPTY_ARRAY)) : true_(); } /** @@ -148,8 +147,8 @@ public final class PredicateTools { * @param the type of objects to be evaluated by the predicate */ @SafeVarargs - public static CompoundPredicate and(Predicate... predicates) { - return new AND<>(predicates); + public static Predicate and(Predicate... predicates) { + return (predicates.length != 0) ? new AND<>(predicates) : true_(); } @@ -159,7 +158,7 @@ public final class PredicateTools { * Return a predicate that will OR the results of the specified predicates. * @param the type of objects to be evaluated by the predicate */ - public static CompoundPredicate or(Iterable> predicates) { + public static Predicate or(Iterable> predicates) { return or(predicates.iterator()); } @@ -168,8 +167,8 @@ public final class PredicateTools { * @param the type of objects to be evaluated by the predicate */ @SuppressWarnings("unchecked") - public static CompoundPredicate or(Iterator> predicates) { - return or((Predicate[]) IteratorTools.toArray(predicates, EMPTY_ARRAY)); + public static Predicate or(Iterator> predicates) { + return predicates.hasNext() ? or((Predicate[]) IteratorTools.toArray(predicates, EMPTY_ARRAY)) : false_(); } /** @@ -177,8 +176,8 @@ public final class PredicateTools { * @param the type of objects to be evaluated by the predicate */ @SafeVarargs - public static CompoundPredicate or(Predicate... predicates) { - return new OR<>(predicates); + public static Predicate or(Predicate... predicates) { + return (predicates.length != 0) ? new OR<>(predicates) : false_(); } @@ -188,7 +187,7 @@ public final class PredicateTools { * Return a predicate that will XOR the results of the specified predicates. * @param the type of objects to be evaluated by the predicate */ - public static CompoundPredicate xor(Predicate predicate1, Predicate predicate2) { + public static Predicate xor(Predicate predicate1, Predicate predicate2) { return new XOR<>(predicate1, predicate2); } @@ -209,7 +208,7 @@ public final class PredicateTools { */ @SuppressWarnings("unchecked") public static Predicate nand(Iterator> predicates) { - return nand((Predicate[]) IteratorTools.toArray(predicates, EMPTY_ARRAY)); + return predicates.hasNext() ? nand((Predicate[]) IteratorTools.toArray(predicates, EMPTY_ARRAY)) : false_(); } /** @@ -218,7 +217,7 @@ public final class PredicateTools { */ @SafeVarargs public static Predicate nand(Predicate... predicates) { - return not(and(predicates)); + return (predicates.length != 0) ? not(and(predicates)) : false_(); } @@ -238,7 +237,7 @@ public final class PredicateTools { */ @SuppressWarnings("unchecked") public static Predicate nor(Iterator> predicates) { - return nor((Predicate[]) IteratorTools.toArray(predicates, EMPTY_ARRAY)); + return predicates.hasNext() ? nor((Predicate[]) IteratorTools.toArray(predicates, EMPTY_ARRAY)) : true_(); } /** @@ -247,7 +246,7 @@ public final class PredicateTools { */ @SafeVarargs public static Predicate nor(Predicate... predicates) { - return not(or(predicates)); + return (predicates.length != 0) ? not(or(predicates)) : true_(); } @@ -307,7 +306,7 @@ public final class PredicateTools { * wrapped predicate to be changed as necessary. * @param the type of objects to be evaluated by the predicate */ - public static PredicateWrapper wrap(Predicate predicate) { + public static Predicate wrap(Predicate predicate) { return new PredicateWrapper<>(predicate); } @@ -376,6 +375,14 @@ public final class PredicateTools { // ********** collection ********** + /** + * Return a predicate that returns whether a collection is not empty. + * @param the type of elements in the collection + */ + public static Predicate> collectionIsNotEmptyPredicate() { + return not(collectionIsEmptyPredicate()); + } + /** * Return a predicate that returns whether a collection is empty. * @param the type of elements in the collection @@ -384,6 +391,33 @@ public final class PredicateTools { return CollectionIsEmptyPredicate.instance(); } + /** + * Return a predicate that returns whether a collection contains exactly one element. + * @param the type of elements in the collection + */ + @SuppressWarnings("unchecked") + public static Predicate> collectionContainsSingleElementPredicate() { + return COLLECTION_CONTAINS_SINGLE_ELEMENT_PREDICATE; + } + + /** + * A predicate that returns whether a collection contains exactly one element. + */ + @SuppressWarnings("rawtypes") + public static final Predicate COLLECTION_CONTAINS_SINGLE_ELEMENT_PREDICATE = collectionSizeEqualsPredicate_(1); + + /** + * Return a predicate that returns whether a collection's is the specified size. + * @param the type of elements in the collection + */ + public static Predicate> collectionSizeEqualsPredicate(int size) { + return (size == 0) ? collectionIsEmptyPredicate(): (size == 1) ? collectionContainsSingleElementPredicate() : collectionSizeEqualsPredicate_(size); + } + + private static Predicate> collectionSizeEqualsPredicate_(int size) { + return new CollectionSizeEqualsPredicate<>(size); + } + // ********** set ********** @@ -450,7 +484,7 @@ public final class PredicateTools { * "duck typing". * @param the type of objects to be evaluated by the predicate */ - public static Predicate get(String fieldName) { + public static FieldPredicate get(String fieldName) { return new FieldPredicate<>(fieldName); } @@ -463,7 +497,7 @@ public final class PredicateTools { * "duck typing". * @param the type of objects to be evaluated by the predicate */ - public static Predicate execute(String methodName) { + public static MethodPredicate execute(String methodName) { return execute(methodName, ClassTools.EMPTY_ARRAY, ObjectTools.EMPTY_OBJECT_ARRAY); } @@ -476,7 +510,7 @@ public final class PredicateTools { * "duck typing". * @param the type of objects to be evaluated by the predicate */ - public static Predicate execute(String methodName, Class parameterType, Object argument) { + public static MethodPredicate execute(String methodName, Class parameterType, Object argument) { return execute(methodName, new Class[] { parameterType }, new Object[] { argument }); } @@ -489,7 +523,7 @@ public final class PredicateTools { * "duck typing". * @param the type of objects to be evaluated by the predicate */ - public static Predicate execute(String methodName, Class[] parameterTypes, Object[] arguments) { + public static MethodPredicate execute(String methodName, Class[] parameterTypes, Object[] arguments) { return new MethodPredicate<>(methodName, parameterTypes, arguments); } 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 02b493f9f6..504467b2a0 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 @@ -395,6 +395,23 @@ public final class TransformerTools { return CollectionSingleElementTransformer.instance(); } + /** + * Return a transformer that converts a collection into a boolean + * that indicates whether the collection is not empty. + * @param the type of elements held by the collection + */ + @SuppressWarnings("unchecked") + public static Transformer, Boolean> collectionIsNotEmptyTransformer() { + return TransformerTools.COLLECTION_IS_NOT_EMPTY_TRANSFORMER; + } + + /** + * Transformer that converts a collection into a boolean + * that indicates whether the collection is not empty. + */ + @SuppressWarnings("rawtypes") + public static final Transformer COLLECTION_IS_NOT_EMPTY_TRANSFORMER = adapt_(PredicateTools.collectionIsNotEmptyPredicate()); + /** * Return a transformer that converts a collection into a boolean * that indicates whether the collection is empty. @@ -409,8 +426,38 @@ public final class TransformerTools { * Transformer that converts a collection into a boolean * that indicates whether the collection is empty. */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static final Transformer COLLECTION_IS_EMPTY_TRANSFORMER = new PredicateTransformer(PredicateTools.collectionIsEmptyPredicate()); + @SuppressWarnings("rawtypes") + public static final Transformer COLLECTION_IS_EMPTY_TRANSFORMER = adapt_(PredicateTools.collectionIsEmptyPredicate()); + + /** + * Return a transformer that converts a collection into a boolean + * that indicates whether the collection contains exactly one element. + * @param the type of elements held by the collection + */ + @SuppressWarnings("unchecked") + public static Transformer, Boolean> collectionContainsSingleElementTransformer() { + return TransformerTools.COLLECTION_CONTAINS_SINGLE_ELEMENT_TRANSFORMER; + } + + /** + * Transformer that converts a collection into a boolean + * that indicates whether the collection contains exactly one element. + */ + @SuppressWarnings("rawtypes") + public static final Transformer COLLECTION_CONTAINS_SINGLE_ELEMENT_TRANSFORMER = adapt_(PredicateTools.collectionContainsSingleElementPredicate()); + + /** + * Return a transformer that converts a collection into a boolean + * that indicates whether the collection's size equals the specified size. + * @param the type of elements held by the collection + */ + public static Transformer, Boolean> collectionSizeEqualsTransformer(int size) { + return (size == 0) ? collectionIsEmptyTransformer(): (size == 1) ? collectionContainsSingleElementTransformer() : collectionSizeEqualsTransformer_(size); + } + + private static Transformer, Boolean> collectionSizeEqualsTransformer_(int size) { + return adapt_(PredicateTools.collectionSizeEqualsPredicate(size)); + } // ********** XML ********** @@ -489,7 +536,7 @@ public final class TransformerTools { * @param output: the type of the object returned by the transformer * @see CachingTransformer */ - public static CachingTransformer cachingTransformer(Transformer transformer) { + public static Transformer cachingTransformer(Transformer transformer) { return new CachingTransformer<>(transformer); } @@ -501,7 +548,7 @@ public final class TransformerTools { * @param output: the type of the object returned by the transformer * @see CachingInterruptibleTransformer */ - public static CachingInterruptibleTransformer cachingInterruptibleTransformer(Transformer transformer) { + public static InterruptibleTransformer cachingInterruptibleTransformer(Transformer transformer) { return new CachingInterruptibleTransformer<>(transformer); } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/CollectionIsEmptyPredicateTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/CollectionIsEmptyPredicateTests.java index f162e801f8..e4b0db7a66 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/CollectionIsEmptyPredicateTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/CollectionIsEmptyPredicateTests.java @@ -26,7 +26,7 @@ public class CollectionIsEmptyPredicateTests public void testEvaluate() { Collection list = new ArrayList<>(); - Predicate> predicate = PredicateTools.collectionIsEmptyPredicate(); + Predicate> predicate = PredicateTools.collectionIsEmptyPredicate(); assertTrue(predicate.evaluate(list)); list.add("foo"); assertFalse(predicate.evaluate(list)); @@ -35,12 +35,12 @@ public class CollectionIsEmptyPredicateTests } public void testToString() { - Predicate> predicate = PredicateTools.collectionIsEmptyPredicate(); + Predicate> predicate = PredicateTools.collectionIsEmptyPredicate(); assertEquals("CollectionIsEmptyPredicate", predicate.toString()); } public void testSerialization() throws Exception { - Predicate> predicate = PredicateTools.collectionIsEmptyPredicate(); + Predicate> predicate = PredicateTools.collectionIsEmptyPredicate(); assertSame(predicate, TestTools.serialize(predicate)); } } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/CollectionSizeEqualsPredicateTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/CollectionSizeEqualsPredicateTests.java new file mode 100644 index 0000000000..334ccd72d5 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/CollectionSizeEqualsPredicateTests.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * 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.predicate; + +import java.util.ArrayList; +import java.util.Collection; +import org.eclipse.jpt.common.utility.internal.predicate.CollectionSizeEqualsPredicate; +import org.eclipse.jpt.common.utility.internal.predicate.PredicateTools; +import org.eclipse.jpt.common.utility.predicate.Predicate; +import org.eclipse.jpt.common.utility.tests.internal.TestTools; +import junit.framework.TestCase; + +@SuppressWarnings("nls") +public class CollectionSizeEqualsPredicateTests + extends TestCase +{ + public CollectionSizeEqualsPredicateTests(String name) { + super(name); + } + + public void testEvaluate() { + Collection list = new ArrayList<>(); + Predicate> predicate = PredicateTools.collectionSizeEqualsPredicate(2); + assertFalse(predicate.evaluate(list)); + list.add("foo"); + assertFalse(predicate.evaluate(list)); + list.add("bar"); + assertTrue(predicate.evaluate(list)); + list.add("baz"); + assertFalse(predicate.evaluate(list)); + list.remove("bar"); + assertTrue(predicate.evaluate(list)); + list.remove("foo"); + assertFalse(predicate.evaluate(list)); + list.remove("baz"); + assertFalse(predicate.evaluate(list)); + } + + public void testGetSize() throws Exception { + CollectionSizeEqualsPredicate> predicate = (CollectionSizeEqualsPredicate>) PredicateTools.>collectionSizeEqualsPredicate(2); + assertEquals(2, predicate.getSize()); + } + + public void testEquals() throws Exception { + Predicate> predicate1 = PredicateTools.collectionSizeEqualsPredicate(2); + assertFalse(predicate1.equals(null)); + Predicate> predicate2 = PredicateTools.collectionSizeEqualsPredicate(2); + assertTrue(predicate1.equals(predicate2)); + predicate2 = PredicateTools.collectionSizeEqualsPredicate(3); + assertFalse(predicate1.equals(predicate2)); + predicate2 = PredicateTools.collectionIsEmptyPredicate(); + assertFalse(predicate1.equals(predicate2)); + } + + public void testHashCode() throws Exception { + Predicate> predicate = PredicateTools.collectionSizeEqualsPredicate(2); + assertEquals(2, predicate.hashCode()); + } + + public void testToString() { + Predicate> predicate = PredicateTools.collectionSizeEqualsPredicate(2); + assertTrue(predicate.toString().indexOf("(2)") != -1); + } + + public void testSerialization() throws Exception { + Predicate> predicate = PredicateTools.collectionSizeEqualsPredicate(2); + assertEquals(predicate, TestTools.serialize(predicate)); + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/JptCommonUtilityPredicateTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/JptCommonUtilityPredicateTests.java index 561e5210a6..6450feaf13 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/JptCommonUtilityPredicateTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/predicate/JptCommonUtilityPredicateTests.java @@ -19,6 +19,7 @@ public class JptCommonUtilityPredicateTests { suite.addTestSuite(ANDTests.class); suite.addTestSuite(CollectionIsEmptyPredicateTests.class); + suite.addTestSuite(CollectionSizeEqualsPredicateTests.class); suite.addTestSuite(EqualsTests.class); suite.addTestSuite(FieldPredicateTests.class); suite.addTestSuite(IsIdenticalTests.class); -- cgit v1.2.3