[java-support] 02/03: Add support method for testing an Iterable for instance of a class.

Brent Putman putmanb at georgetown.edu
Wed Dec 16 20:11:27 EST 2015


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch master
in repository java-support.

commit c80d265999c57909e0ac2cfc84c1f6f239087daf
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Wed Dec 16 19:47:47 2015 -0500

    Add support method for testing an Iterable for instance of a class.
---
 .../java/support/collection/IterableSupport.java   | 52 ++++++++++++++++
 .../support/collection/IterableSupportTest.java    | 70 ++++++++++++++++++++++
 2 files changed, 122 insertions(+)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/collection/IterableSupport.java b/src/main/java/net/shibboleth/utilities/java/support/collection/IterableSupport.java
new file mode 100644
index 0000000..6884e08
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/collection/IterableSupport.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache 
+ * License, Version 2.0 (the "License"); you may not use this file except in 
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.utilities.java.support.collection;
+
+import javax.annotation.Nonnull;
+
+import net.shibboleth.utilities.java.support.logic.Constraint;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.Iterables;
+
+/** Helper methods for working with {@link Iterable} instances. */
+public final class IterableSupport {
+    
+    /** Constructor. */
+    private IterableSupport() { }
+    
+    /**
+     * Checks whether the {@link Iterable} contains at least one instance of the supplied class.
+     * 
+     * @param target iterable to evaluate
+     * @param clazz the class to evaluate
+     * 
+     * @return true if an instance of the given class is present in the iterable
+     */
+    public static boolean containsInstance(@Nonnull final Iterable<?> target, @Nonnull final Class<?> clazz) {
+        Constraint.isNotNull(target, "Target collection can not be null");
+        Constraint.isNotNull(clazz, "Class can not be null");
+        
+        Predicate<Object> instanceOf = Predicates.instanceOf(clazz);
+        Optional<?> result = Iterables.tryFind(target, instanceOf);
+        return result.isPresent();
+    }
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/collection/IterableSupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/collection/IterableSupportTest.java
new file mode 100644
index 0000000..c151535
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/collection/IterableSupportTest.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache 
+ * License, Version 2.0 (the "License"); you may not use this file except in 
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.shibboleth.utilities.java.support.collection;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.Lists;
+
+public class IterableSupportTest {
+
+    @Test
+    public void testContainsInstance() {
+        //Empty
+        Assert.assertFalse(IterableSupport.containsInstance(Lists.<Object>newArrayList(), Foo.class));
+        Assert.assertFalse(IterableSupport.containsInstance(Lists.<Foo>newArrayList(), Foo.class));
+        Assert.assertFalse(IterableSupport.containsInstance(Lists.<Object>newArrayList(), Object.class));
+        
+        // Class = Object
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Object>newArrayList(new Object(), new Foo(), new Object()), Object.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Object>newArrayList(new Object(), new Object()), Object.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Foo>newArrayList(new Foo(), new Foo()), Object.class));
+        
+        // Class = Foo
+        Assert.assertFalse(IterableSupport.containsInstance(Lists.<Object>newArrayList(new Object(), new Object()), Foo.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Object>newArrayList(new Object(), new Foo(), new Object()), Foo.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Object>newArrayList(new Foo(), new Foo()), Foo.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Foo>newArrayList(new Foo(), new Foo()), Foo.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Bar>newArrayList(new Bar(), new Bar()), Foo.class));
+        
+        // Class = Bar
+        Assert.assertFalse(IterableSupport.containsInstance(Lists.<Foo>newArrayList(new Foo(), new Foo()), Bar.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Bar>newArrayList(new Bar(), new Bar()), Bar.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Foo>newArrayList(new Foo(), new Bar()), Bar.class));
+        
+        // Class = Baz
+        Assert.assertFalse(IterableSupport.containsInstance(Lists.<Foo>newArrayList(new Foo(), new Foo()), Baz.class));
+        Assert.assertFalse(IterableSupport.containsInstance(Lists.<Foo>newArrayList(new Foo(), new Bar()), Baz.class));
+        Assert.assertFalse(IterableSupport.containsInstance(Lists.<Bar>newArrayList(new Bar(), new Bar()), Baz.class));
+        Assert.assertTrue(IterableSupport.containsInstance(Lists.<Foo>newArrayList(new Foo(), new Bar(), new Baz()), Baz.class));
+    }
+    
+    private static class Foo {
+        
+    }
+    
+    private static class Bar extends Foo {
+        
+    }
+    
+    private static class Baz extends Foo {
+        
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list