hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [1/2] httpcomponents-client git commit: Added Cancellable convenience methods
Date Sun, 24 Dec 2017 11:09:26 GMT
Repository: httpcomponents-client
Updated Branches:
  refs/heads/master d6d3d364e -> 1e4e20416


Added Cancellable convenience methods


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/e2628424
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/e2628424
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/e2628424

Branch: refs/heads/master
Commit: e26284246638a750d74f9d96b4f9761e914f884f
Parents: d6d3d36
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Fri Dec 22 20:27:45 2017 +0100
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Sat Dec 23 10:48:46 2017 +0100

----------------------------------------------------------------------
 .../http/cache/HttpAsyncCacheStorage.java       |  9 ---
 .../cache/HttpAsyncCacheStorageAdaptor.java     | 11 ++--
 .../AbstractSerializingAsyncCacheStorage.java   |  9 +--
 .../MemcachedHttpAsyncCacheStorage.java         | 28 ++------
 .../apache/hc/client5/http/impl/Operations.java | 68 ++++++++++++++++++++
 5 files changed, 83 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e2628424/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorage.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorage.java
b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorage.java
index 72064b3..6fdf1f8 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorage.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorage.java
@@ -42,15 +42,6 @@ import org.apache.hc.core5.concurrent.FutureCallback;
  */
 public interface HttpAsyncCacheStorage {
 
-    Cancellable NOOP_CANCELLABLE = new Cancellable() {
-
-        @Override
-        public boolean cancel() {
-            return false;
-        }
-
-    };
-
     /**
      * Store a given cache entry under the given key.
      * @param key where in the cache to store the entry

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e2628424/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorageAdaptor.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorageAdaptor.java
b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorageAdaptor.java
index d5199af..55bd095 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorageAdaptor.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/HttpAsyncCacheStorageAdaptor.java
@@ -29,6 +29,7 @@ package org.apache.hc.client5.http.cache;
 import java.util.Collection;
 import java.util.Map;
 
+import org.apache.hc.client5.http.impl.Operations;
 import org.apache.hc.core5.concurrent.Cancellable;
 import org.apache.hc.core5.concurrent.FutureCallback;
 import org.apache.hc.core5.util.Args;
@@ -57,7 +58,7 @@ public final class HttpAsyncCacheStorageAdaptor implements HttpAsyncCacheStorage
         } catch (final Exception ex) {
             callback.failed(ex);
         }
-        return NOOP_CANCELLABLE;
+        return Operations.nonCancellable();
     }
 
     public Cancellable getEntry(final String key, final FutureCallback<HttpCacheEntry>
callback) {
@@ -69,7 +70,7 @@ public final class HttpAsyncCacheStorageAdaptor implements HttpAsyncCacheStorage
         } catch (final Exception ex) {
             callback.failed(ex);
         }
-        return NOOP_CANCELLABLE;
+        return Operations.nonCancellable();
     }
 
     public Cancellable removeEntry(final String key, final FutureCallback<Boolean>
callback) {
@@ -81,7 +82,7 @@ public final class HttpAsyncCacheStorageAdaptor implements HttpAsyncCacheStorage
         } catch (final Exception ex) {
             callback.failed(ex);
         }
-        return NOOP_CANCELLABLE;
+        return Operations.nonCancellable();
     }
 
     public Cancellable updateEntry(
@@ -95,7 +96,7 @@ public final class HttpAsyncCacheStorageAdaptor implements HttpAsyncCacheStorage
         } catch (final Exception ex) {
             callback.failed(ex);
         }
-        return NOOP_CANCELLABLE;
+        return Operations.nonCancellable();
     }
 
     public Cancellable getEntries(final Collection<String> keys, final FutureCallback<Map<String,
HttpCacheEntry>> callback) {
@@ -106,7 +107,7 @@ public final class HttpAsyncCacheStorageAdaptor implements HttpAsyncCacheStorage
         } catch (final Exception ex) {
             callback.failed(ex);
         }
-        return NOOP_CANCELLABLE;
+        return Operations.nonCancellable();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e2628424/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AbstractSerializingAsyncCacheStorage.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AbstractSerializingAsyncCacheStorage.java
b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AbstractSerializingAsyncCacheStorage.java
index 60805dd..ce4ed7c 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AbstractSerializingAsyncCacheStorage.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/AbstractSerializingAsyncCacheStorage.java
@@ -40,6 +40,7 @@ import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
 import org.apache.hc.client5.http.cache.HttpCacheStorageEntry;
 import org.apache.hc.client5.http.cache.HttpCacheUpdateException;
 import org.apache.hc.client5.http.cache.ResourceIOException;
+import org.apache.hc.client5.http.impl.Operations;
 import org.apache.hc.core5.concurrent.Cancellable;
 import org.apache.hc.core5.concurrent.FutureCallback;
 import org.apache.hc.core5.util.Args;
@@ -86,7 +87,7 @@ public abstract class AbstractSerializingAsyncCacheStorage<T, CAS>
implements Ht
             return store(storageKey, storageObject, callback);
         } catch (final Exception ex) {
             callback.failed(ex);
-            return NOOP_CANCELLABLE;
+            return Operations.nonCancellable();
         }
     }
 
@@ -129,7 +130,7 @@ public abstract class AbstractSerializingAsyncCacheStorage<T, CAS>
implements Ht
             });
         } catch (final Exception ex) {
             callback.failed(ex);
-            return NOOP_CANCELLABLE;
+            return Operations.nonCancellable();
         }
     }
 
@@ -142,7 +143,7 @@ public abstract class AbstractSerializingAsyncCacheStorage<T, CAS>
implements Ht
             return delete(storageKey, callback);
         } catch (final Exception ex) {
             callback.failed(ex);
-            return NOOP_CANCELLABLE;
+            return Operations.nonCancellable();
         }
     }
 
@@ -276,7 +277,7 @@ public abstract class AbstractSerializingAsyncCacheStorage<T, CAS>
implements Ht
             });
         } catch (final Exception ex) {
             callback.failed(ex);
-            return NOOP_CANCELLABLE;
+            return Operations.nonCancellable();
         }
     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e2628424/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/memcached/MemcachedHttpAsyncCacheStorage.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/memcached/MemcachedHttpAsyncCacheStorage.java
b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/memcached/MemcachedHttpAsyncCacheStorage.java
index 296b85a..b59529d 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/memcached/MemcachedHttpAsyncCacheStorage.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/memcached/MemcachedHttpAsyncCacheStorage.java
@@ -35,6 +35,7 @@ import java.util.concurrent.ExecutionException;
 
 import org.apache.hc.client5.http.cache.HttpCacheEntrySerializer;
 import org.apache.hc.client5.http.cache.ResourceIOException;
+import org.apache.hc.client5.http.impl.Operations;
 import org.apache.hc.client5.http.impl.cache.AbstractBinaryAsyncCacheStorage;
 import org.apache.hc.client5.http.impl.cache.ByteArrayCacheEntrySerializer;
 import org.apache.hc.client5.http.impl.cache.CacheConfig;
@@ -176,14 +177,7 @@ public class MemcachedHttpAsyncCacheStorage extends AbstractBinaryAsyncCacheStor
             }
 
         });
-        return new Cancellable() {
-
-            @Override
-            public boolean cancel() {
-                return operationFuture.cancel();
-            }
-
-        };
+        return Operations.cancellable(operationFuture);
     }
 
     @Override
@@ -210,14 +204,7 @@ public class MemcachedHttpAsyncCacheStorage extends AbstractBinaryAsyncCacheStor
             }
 
         });
-        return new Cancellable() {
-
-            @Override
-            public boolean cancel() {
-                return getFuture.cancel(true);
-            }
-
-        };
+        return Operations.cancellable(getFuture);
     }
 
     @Override
@@ -268,14 +255,7 @@ public class MemcachedHttpAsyncCacheStorage extends AbstractBinaryAsyncCacheStor
                 callback.completed(resultMap);
             }
         });
-        return new Cancellable() {
-
-            @Override
-            public boolean cancel() {
-                return future.cancel(true);
-            }
-
-        };
+        return Operations.cancellable(future);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e2628424/httpclient5/src/main/java/org/apache/hc/client5/http/impl/Operations.java
----------------------------------------------------------------------
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/Operations.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/Operations.java
new file mode 100644
index 0000000..f6b6351
--- /dev/null
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/Operations.java
@@ -0,0 +1,68 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.client5.http.impl;
+
+import java.util.concurrent.Future;
+
+import org.apache.hc.core5.concurrent.Cancellable;
+
+public final class Operations {
+
+    private final static Cancellable NOOP_CANCELLABLE = new Cancellable() {
+
+        @Override
+        public boolean cancel() {
+            return false;
+        }
+
+    };
+
+    public static Cancellable nonCancellable() {
+        return NOOP_CANCELLABLE;
+    }
+
+    public static Cancellable cancellable(final Future<?> future) {
+        if (future == null) {
+            return NOOP_CANCELLABLE;
+        } else {
+            if (future instanceof Cancellable) {
+                return (Cancellable) future;
+            } else {
+                return new Cancellable() {
+
+                    @Override
+                    public boolean cancel() {
+                        return future.cancel(true);
+                    }
+
+                };
+            }
+        }
+    }
+
+}


Mime
View raw message