[java-support] branch master updated: More Guava and legacy class removal.

Scott Cantor cantor.2 at osu.edu
Thu Nov 14 20:08:42 EST 2019


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

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

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=1330e1f96eafa6f03fd059d17a049bac6b606e11

The following commit(s) were added to refs/heads/master by this push:
       new  1330e1f   More Guava and legacy class removal.
1330e1f is described below

commit 1330e1f96eafa6f03fd059d17a049bac6b606e11
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Nov 14 20:08:39 2019 -0500

    More Guava and legacy class removal.
---
 .../java/support/collection/IterableSupport.java   |  53 ----------
 .../java/support/httpclient/HttpClientBuilder.java | 107 +++++++++------------
 .../support/collection/IterableSupportTest.java    |  70 --------------
 .../support/httpclient/HttpClientBuilderTest.java  |   7 +-
 4 files changed, 51 insertions(+), 186 deletions(-)

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
deleted file mode 100644
index 50cd54c..0000000
--- a/src/main/java/net/shibboleth/utilities/java/support/collection/IterableSupport.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * 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 java.util.function.Predicate;
-
-import javax.annotation.Nonnull;
-
-import net.shibboleth.utilities.java.support.logic.Constraint;
-
-import com.google.common.base.Optional;
-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");
-        
-        final Predicate<Object> instanceOf = Predicates.instanceOf(clazz);
-        final Optional<?> result = Iterables.tryFind(target, instanceOf::test);
-        return result.isPresent();
-    }
-
-}
diff --git a/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientBuilder.java b/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientBuilder.java
index 52b669a..52c26a6 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientBuilder.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientBuilder.java
@@ -20,7 +20,6 @@ package net.shibboleth.utilities.java.support.httpclient;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.time.Duration;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -42,14 +41,9 @@ import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
 import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.util.CharsetUtils;
 
-import com.google.common.base.Predicates;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.ImmutableList;
-
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotLive;
 import net.shibboleth.utilities.java.support.annotation.constraint.Unmodifiable;
-import net.shibboleth.utilities.java.support.collection.IterableSupport;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 import net.shibboleth.utilities.java.support.primitive.StringSupport;
 
@@ -240,19 +234,19 @@ public class HttpClientBuilder {
     private boolean useSystemProperties;
 
     /** List of request interceptors to add first. */
-    @Nonnull @NonnullElements private List<HttpRequestInterceptor> requestInterceptorsFirst;
+    @Nonnull @NonnullElements @Unmodifiable @NotLive private List<HttpRequestInterceptor> requestInterceptorsFirst;
 
     /** List of request interceptors to add last. */
-    @Nonnull @NonnullElements private List<HttpRequestInterceptor> requestInterceptorsLast;
+    @Nonnull @NonnullElements @Unmodifiable @NotLive private List<HttpRequestInterceptor> requestInterceptorsLast;
 
     /** List of response interceptors to add first. */
-    @Nonnull @NonnullElements private List<HttpResponseInterceptor> responseInterceptorsFirst;
+    @Nonnull @NonnullElements @Unmodifiable @NotLive private List<HttpResponseInterceptor> responseInterceptorsFirst;
 
     /** List of response interceptors to add last. */
-    @Nonnull @NonnullElements private List<HttpResponseInterceptor> responseInterceptorsLast;
+    @Nonnull @NonnullElements @Unmodifiable @NotLive private List<HttpResponseInterceptor> responseInterceptorsLast;
     
     /** List of static context handlers. */
-    @Nonnull @NonnullElements private List<HttpClientContextHandler> staticContextHandlers;
+    @Nonnull @NonnullElements @Unmodifiable @NotLive private List<HttpClientContextHandler> staticContextHandlers;
 
     /** The Apache HttpClientBuilder 4.3+ instance over which to layer this builder. */
     private org.apache.http.impl.client.HttpClientBuilder apacheBuilder;
@@ -863,7 +857,7 @@ public class HttpClientBuilder {
      */
     @Nonnull @NonnullElements @NotLive @Unmodifiable
     public List<HttpRequestInterceptor> getFirstRequestInterceptors() {
-        return ImmutableList.copyOf(requestInterceptorsFirst);
+        return requestInterceptorsFirst;
     }
     
     /**
@@ -871,8 +865,13 @@ public class HttpClientBuilder {
      * 
      * @param interceptors the list of interceptors, may be null
      */
-    public void setFirstRequestInterceptors(@Nullable final List<HttpRequestInterceptor> interceptors) {
-        requestInterceptorsFirst = (List<HttpRequestInterceptor>) normalizeList(interceptors);
+    public void setFirstRequestInterceptors(
+            @Nullable @NonnullElements final List<HttpRequestInterceptor> interceptors) {
+        if (interceptors != null) {
+            requestInterceptorsFirst = List.copyOf(interceptors);
+        } else {
+            requestInterceptorsFirst = Collections.emptyList();
+        }
     }
 
     /**
@@ -882,7 +881,7 @@ public class HttpClientBuilder {
      */
     @Nonnull @NonnullElements @NotLive @Unmodifiable
     public List<HttpRequestInterceptor> getLastRequestInterceptors() {
-        return ImmutableList.copyOf(requestInterceptorsLast);
+        return requestInterceptorsLast;
     }
 
     /**
@@ -890,8 +889,12 @@ public class HttpClientBuilder {
      * 
      * @param interceptors the list of interceptors, may be null
      */
-    public void setLastRequestInterceptors(@Nullable final List<HttpRequestInterceptor> interceptors) {
-        requestInterceptorsLast = normalizeList(interceptors);
+    public void setLastRequestInterceptors(@Nullable @NonnullElements final List<HttpRequestInterceptor> interceptors) {
+        if (interceptors != null) {
+            requestInterceptorsLast = List.copyOf(interceptors);
+        } else {
+            requestInterceptorsLast = Collections.emptyList();
+        }
     }
 
     /**
@@ -901,7 +904,7 @@ public class HttpClientBuilder {
      */
     @Nonnull @NonnullElements @NotLive @Unmodifiable
     public List<HttpResponseInterceptor> getFirstResponseInterceptors() {
-        return ImmutableList.copyOf(responseInterceptorsFirst);
+        return responseInterceptorsFirst;
     }
 
     /**
@@ -909,8 +912,13 @@ public class HttpClientBuilder {
      * 
      * @param interceptors the list of interceptors, may be null
      */
-    public void setFirstResponseInterceptors(@Nullable final List<HttpResponseInterceptor> interceptors) {
-        responseInterceptorsFirst = normalizeList(interceptors);
+    public void setFirstResponseInterceptors(
+            @Nullable @NonnullElements final List<HttpResponseInterceptor> interceptors) {
+        if (interceptors != null) {
+            responseInterceptorsFirst = List.copyOf(interceptors);
+        } else {
+            responseInterceptorsFirst = Collections.emptyList();
+        }
     }
 
     /**
@@ -920,7 +928,7 @@ public class HttpClientBuilder {
      */
     @Nonnull @NonnullElements @NotLive @Unmodifiable
     public List<HttpResponseInterceptor> getLastResponseInterceptors() {
-        return ImmutableList.copyOf(responseInterceptorsLast);
+        return responseInterceptorsLast;
     }
 
     /**
@@ -928,8 +936,13 @@ public class HttpClientBuilder {
      * 
      * @param interceptors the list of interceptors, may be null
      */
-    public void setLastResponseInterceptors(@Nullable final List<HttpResponseInterceptor> interceptors) {
-        responseInterceptorsLast = normalizeList(interceptors);
+    public void setLastResponseInterceptors(
+            @Nullable @NonnullElements final List<HttpResponseInterceptor> interceptors) {
+        if (interceptors != null) {
+            responseInterceptorsLast = List.copyOf(interceptors);
+        } else {
+            responseInterceptorsLast = Collections.emptyList();
+        }
     }
 
     /**
@@ -939,31 +952,19 @@ public class HttpClientBuilder {
      */
     @Nonnull @NonnullElements @NotLive @Unmodifiable
     public List<HttpClientContextHandler> getStaticContextHandlers() {
-        return ImmutableList.copyOf(staticContextHandlers);
+        return staticContextHandlers;
     }
 
     /**
      * Set the list of static {@link HttpClientContextHandler}.
      * 
-     * @param handlers the list of handlers , may be null
-     */
-    public void setStaticContextHandlers(@Nullable final List<HttpClientContextHandler> handlers) {
-        staticContextHandlers = normalizeList(handlers);
-    }
-
-    /**
-     * Normalize and copy the supplied list to remove nulls.
-     * 
-     * @param <T> type of collection to normalize
-     * 
-     * @param items the list of items to normalize
-     * @return copy of input list without nulls
+     * @param handlers the list of handlers, may be null
      */
-    @Nonnull @NonnullElements private <T> List<T> normalizeList(@Nullable final List<T> items) {
-        if (items == null) {
-            return Collections.emptyList();
+    public void setStaticContextHandlers(@Nullable @NonnullElements final List<HttpClientContextHandler> handlers) {
+        if (handlers != null) {
+            staticContextHandlers = List.copyOf(handlers);
         } else {
-            return new ArrayList<>(Collections2.filter(items, Predicates.notNull()));
+            staticContextHandlers = Collections.emptyList();
         }
     }
 
@@ -998,9 +999,8 @@ public class HttpClientBuilder {
         }
 
         if (connectionCloseAfterResponse) {
-            if (!IterableSupport.containsInstance(getFirstRequestInterceptors(), RequestConnectionClose.class)
-                    && !IterableSupport.containsInstance(getLastRequestInterceptors(), RequestConnectionClose.class)) {
-                
+            if (!getFirstRequestInterceptors().stream().anyMatch(RequestConnectionClose.class::isInstance)
+                    && !getLastRequestInterceptors().stream().anyMatch(RequestConnectionClose.class::isInstance)) {
                 builder.addInterceptorLast(new RequestConnectionClose());
             }
         }
@@ -1052,21 +1052,10 @@ public class HttpClientBuilder {
             builder.useSystemProperties();
         }
 
-        for (final HttpRequestInterceptor interceptor : getFirstRequestInterceptors()) {
-            builder.addInterceptorFirst(interceptor);
-        }
-
-        for (final HttpRequestInterceptor interceptor : getLastRequestInterceptors()) {
-            builder.addInterceptorLast(interceptor);
-        }
-
-        for (final HttpResponseInterceptor interceptor : getFirstResponseInterceptors()) {
-            builder.addInterceptorFirst(interceptor);
-        }
-
-        for (final HttpResponseInterceptor interceptor : getLastResponseInterceptors()) {
-            builder.addInterceptorLast(interceptor);
-        }
+        getFirstRequestInterceptors().forEach(builder::addInterceptorFirst);
+        getLastRequestInterceptors().forEach(builder::addInterceptorLast);
+        getFirstResponseInterceptors().forEach(builder::addInterceptorFirst);
+        getLastResponseInterceptors().forEach(builder::addInterceptorLast);
 
         // RequestConfig params
         final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
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
deleted file mode 100644
index 3ca3c9a..0000000
--- a/src/test/java/net/shibboleth/utilities/java/support/collection/IterableSupportTest.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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 {
-        
-    }
-
-}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/httpclient/HttpClientBuilderTest.java b/src/test/java/net/shibboleth/utilities/java/support/httpclient/HttpClientBuilderTest.java
index d8d0369..69b948b 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/httpclient/HttpClientBuilderTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/httpclient/HttpClientBuilderTest.java
@@ -20,6 +20,7 @@ package net.shibboleth.utilities.java.support.httpclient;
 
 import java.io.IOException;
 import java.time.Duration;
+import java.util.List;
 
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpUriRequest;
@@ -27,8 +28,6 @@ import org.apache.http.client.protocol.HttpClientContext;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
-import com.google.common.collect.Lists;
-
 import net.shibboleth.utilities.java.support.logic.ConstraintViolationException;
 
 public class HttpClientBuilderTest {
@@ -66,8 +65,8 @@ public class HttpClientBuilderTest {
         Assert.assertNotNull(builder.getStaticContextHandlers());
         Assert.assertTrue(builder.getStaticContextHandlers().isEmpty());
         
-        builder.setStaticContextHandlers(Lists.newArrayList(null, handler1, null, handler2, null, handler3));
-        Assert.assertEquals(builder.getStaticContextHandlers(), Lists.newArrayList(handler1, handler2, handler3));
+        builder.setStaticContextHandlers(List.of(handler1, handler2, handler3));
+        Assert.assertEquals(builder.getStaticContextHandlers(), List.of(handler1, handler2, handler3));
         
         try {
             builder.getStaticContextHandlers().add(new TestContextHandler());

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


More information about the commits mailing list