Added Cancellable convenience methods

This commit is contained in:
Oleg Kalnichevski 2017-12-22 20:27:45 +01:00
parent d6d3d364eb
commit e262842466
5 changed files with 83 additions and 42 deletions

View File

@ -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

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
};
}
}
}
}