From 71377b29a1826edb117121c9bec2df7e9311c853 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Thu, 29 Oct 2009 19:23:41 +0000 Subject: [PATCH] added synchronous future wrapper git-svn-id: http://jclouds.googlecode.com/svn/trunk@1991 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../concurrent/FutureExceptionParser.java | 2 +- .../concurrent/FutureFunctionCallable.java | 2 +- .../concurrent/FutureFunctionWrapper.java | 84 +++++++++++++++++++ .../concurrent/RunnableFutureTask.java | 2 +- .../concurrent/SingleThreadCompatible.java | 2 +- .../jclouds/concurrent/SingleThreaded.java | 2 +- .../concurrent/WithinThreadExecutor.java | 2 +- .../WithinThreadExecutorService.java | 2 +- .../main/java/org/jclouds/logging/Logger.java | 2 +- .../java/org/jclouds/logging/NullLogger.java | 2 +- .../concurrent/FutureExceptionParserTest.java | 2 +- 11 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionWrapper.java diff --git a/concurrent/src/main/java/org/jclouds/concurrent/FutureExceptionParser.java b/concurrent/src/main/java/org/jclouds/concurrent/FutureExceptionParser.java index 9b3ee0a146..1051dce817 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/FutureExceptionParser.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/FutureExceptionParser.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionCallable.java b/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionCallable.java index 1465399e03..0a306bbe70 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionCallable.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionCallable.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionWrapper.java b/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionWrapper.java new file mode 100644 index 0000000000..fee7ae4d60 --- /dev/null +++ b/concurrent/src/main/java/org/jclouds/concurrent/FutureFunctionWrapper.java @@ -0,0 +1,84 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * 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. + * ==================================================================== + */ +package org.jclouds.concurrent; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.jclouds.logging.Logger; + +import com.google.common.base.Function; + +/** + * Transforms the result of a future once requested via get(). + * + * @author Adrian Cole + */ +public class FutureFunctionWrapper implements Future, Function { + + private final Future future; + private final Function function; + private final Logger logger; + + public FutureFunctionWrapper(Future future, Function function) { + this(future, function, Logger.NULL); + } + + public FutureFunctionWrapper(Future future, Function function, Logger logger) { + this.future = future; + this.function = function; + this.logger = logger; + } + + public T apply(F input) { + logger.debug("Processing intermediate result for: %s", input); + T result = function.apply(input); + logger.debug("Processed intermediate result for: %s", input); + return result; + } + + public boolean cancel(boolean mayInterruptIfRunning) { + return future.cancel(mayInterruptIfRunning); + } + + public T get() throws InterruptedException, ExecutionException { + return apply(future.get()); + } + + public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, + TimeoutException { + return apply(future.get(timeout, unit)); + } + + public boolean isCancelled() { + return future.isCancelled(); + } + + public boolean isDone() { + return future.isDone(); + } + +} diff --git a/concurrent/src/main/java/org/jclouds/concurrent/RunnableFutureTask.java b/concurrent/src/main/java/org/jclouds/concurrent/RunnableFutureTask.java index b9274f97b2..3e1c1e7024 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/RunnableFutureTask.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/RunnableFutureTask.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/main/java/org/jclouds/concurrent/SingleThreadCompatible.java b/concurrent/src/main/java/org/jclouds/concurrent/SingleThreadCompatible.java index 3d8cbb10ff..a4cf3f1573 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/SingleThreadCompatible.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/SingleThreadCompatible.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/main/java/org/jclouds/concurrent/SingleThreaded.java b/concurrent/src/main/java/org/jclouds/concurrent/SingleThreaded.java index 16ea63033c..193bc0177c 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/SingleThreaded.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/SingleThreaded.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutor.java b/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutor.java index 00b48a447f..6fbbe0b04c 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutor.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutor.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutorService.java b/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutorService.java index e151e0c2b3..05403342dd 100755 --- a/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutorService.java +++ b/concurrent/src/main/java/org/jclouds/concurrent/WithinThreadExecutorService.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/main/java/org/jclouds/logging/Logger.java b/concurrent/src/main/java/org/jclouds/logging/Logger.java index 705c153f8f..125cb8df78 100755 --- a/concurrent/src/main/java/org/jclouds/logging/Logger.java +++ b/concurrent/src/main/java/org/jclouds/logging/Logger.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/main/java/org/jclouds/logging/NullLogger.java b/concurrent/src/main/java/org/jclouds/logging/NullLogger.java index 9691802492..7c124e5bab 100755 --- a/concurrent/src/main/java/org/jclouds/logging/NullLogger.java +++ b/concurrent/src/main/java/org/jclouds/logging/NullLogger.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one diff --git a/concurrent/src/test/java/org/jclouds/concurrent/FutureExceptionParserTest.java b/concurrent/src/test/java/org/jclouds/concurrent/FutureExceptionParserTest.java index 7586059ab5..296d65fd04 100755 --- a/concurrent/src/test/java/org/jclouds/concurrent/FutureExceptionParserTest.java +++ b/concurrent/src/test/java/org/jclouds/concurrent/FutureExceptionParserTest.java @@ -1,6 +1,6 @@ /** * - * Copyright (C) 2009 Global Cloud Specialists, Inc. + * Copyright (C) 2009 Cloud Conscious, LLC. * * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one