You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by ie...@apache.org on 2009/02/15 15:27:58 UTC
svn commit: r744669 - in /incubator/shindig/trunk/java/common/src:
main/java/org/apache/shindig/common/util/FutureUtil.java
test/java/org/apache/shindig/common/util/FutureUtilTest.java
Author: ieb
Date: Sun Feb 15 14:27:58 2009
New Revision: 744669
URL: http://svn.apache.org/viewvc?rev=744669&view=rev
Log:
SHINDIG-917
Patch by Ben Smith.
Adding files missed of the earlier commit on this issue.
Added:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/FutureUtil.java (with props)
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/FutureUtilTest.java (with props)
Added: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/FutureUtil.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/FutureUtil.java?rev=744669&view=auto
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/FutureUtil.java (added)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/FutureUtil.java Sun Feb 15 14:27:58 2009
@@ -0,0 +1,70 @@
+/*
+ * 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.apache.shindig.common.util;
+
+import org.apache.shindig.protocol.RestfulCollection;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * Utility methods for processing {@link Future} wrapped objects
+ */
+public class FutureUtil {
+ /**
+ * Process a {@link Future} wrapped {@link RestfulCollection}
+ * to return the first (if any) object, as a {@link Future}
+ * @param collection the collection to retrieve the first object from
+ * @return the {@link Future} wrapped object
+ */
+ public static <T> Future<T> getFirstFromCollection(final Future<RestfulCollection<T>> collection) {
+ return new Future<T>() {
+ public boolean cancel(boolean mayInterruptIfRunning) {
+ return collection.cancel(mayInterruptIfRunning);
+ }
+
+ public T get() throws InterruptedException, ExecutionException {
+ return getFirstFromCollection(collection.get());
+ }
+
+ public T get(long timeout, TimeUnit unit)
+ throws InterruptedException, ExecutionException, TimeoutException {
+ return getFirstFromCollection(collection.get(timeout, unit));
+ }
+
+ public boolean isCancelled() {
+ return collection.isCancelled();
+ }
+
+ public boolean isDone() {
+ return collection.isDone();
+ }
+
+ @SuppressWarnings("unchecked")
+ private T getFirstFromCollection(RestfulCollection<?> collection) {
+ if (collection.getTotalResults() > 0) {
+ return (T) collection.getEntry().get(0);
+ }
+
+ return null;
+ }
+ };
+ }
+}
Propchange: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/FutureUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/FutureUtil.java
------------------------------------------------------------------------------
svn:keywords = Date Revision Author HeadURL Id
Added: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/FutureUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/FutureUtilTest.java?rev=744669&view=auto
==============================================================================
--- incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/FutureUtilTest.java (added)
+++ incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/FutureUtilTest.java Sun Feb 15 14:27:58 2009
@@ -0,0 +1,61 @@
+/*
+ * 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.apache.shindig.common.util;
+
+import org.apache.shindig.protocol.RestfulCollection;
+
+import java.util.List;
+import java.util.concurrent.Future;
+
+import junit.framework.TestCase;
+
+import com.google.common.collect.Lists;
+
+/**
+ * Tests the FutureUtils utility methods
+ */
+public class FutureUtilTest extends TestCase {
+ String firstWord = "hello";
+
+ public void testGetFirstOfMany() throws Exception {
+ List<String> words = Lists.newArrayList(new String[]{firstWord, "goodbye", "blah"});
+ Future<RestfulCollection<String>> collection = ImmediateFuture.newInstance(
+ new RestfulCollection<String>(words));
+
+ Future<String> futureWord = FutureUtil.getFirstFromCollection(collection);
+ assertEquals(firstWord, futureWord.get());
+ }
+
+ public void testGetFirstOfSingle() throws Exception {
+ List<String> words = Lists.newArrayList(new String[]{firstWord});
+ Future<RestfulCollection<String>> collection = ImmediateFuture.newInstance(
+ new RestfulCollection<String>(words));
+
+ Future<String> futureWord = FutureUtil.getFirstFromCollection(collection);
+ assertEquals(firstWord, futureWord.get());
+ }
+
+ public void testGetFirstOfNone() throws Exception {
+ List<String> words = Lists.newArrayList(new String[]{});
+ Future<RestfulCollection<String>> collection = ImmediateFuture.newInstance(
+ new RestfulCollection<String>(words));
+
+ Future<String> futureWord = FutureUtil.getFirstFromCollection(collection);
+ assertNull(futureWord.get());
+ }
+}
Propchange: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/FutureUtilTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/util/FutureUtilTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision Author HeadURL Id