From 8b63519dd702a19ef7ff39f47906299277908ca6 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 25 Apr 2014 09:24:07 -0700 Subject: [PATCH] =?UTF-8?q?*=20Ensure=20proper=20handling=20of=20simple=20?= =?UTF-8?q?token=20type=20id=E2=80=99s=20(they=20cannot=20be=20resolved=20?= =?UTF-8?q?into=20object=20form)=E2=80=A6=20*=20Add=20resolveNoWait=20?= =?UTF-8?q?=E2=80=A6=20resolves=20without=20first=20waiting=20on=20preload?= =?UTF-8?q?=20to=20complete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ibm/common/activitystreams/ext/Test.java | 2 +- .../registry/TypeValueRegistry.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/typext/src/main/java/com/ibm/common/activitystreams/ext/Test.java b/typext/src/main/java/com/ibm/common/activitystreams/ext/Test.java index ebfc72a..44545af 100644 --- a/typext/src/main/java/com/ibm/common/activitystreams/ext/Test.java +++ b/typext/src/main/java/com/ibm/common/activitystreams/ext/Test.java @@ -15,7 +15,7 @@ public class Test { .makeDefaultSilent(); Future tv = - reg.resolve(type("urn:example:foo")); + reg.resolveNoWait(type("post")); System.out.println(tv.get().valueType()); } diff --git a/typext/src/main/java/com/ibm/common/activitystreams/registry/TypeValueRegistry.java b/typext/src/main/java/com/ibm/common/activitystreams/registry/TypeValueRegistry.java index 7ab98f9..c7daa80 100644 --- a/typext/src/main/java/com/ibm/common/activitystreams/registry/TypeValueRegistry.java +++ b/typext/src/main/java/com/ibm/common/activitystreams/registry/TypeValueRegistry.java @@ -1,6 +1,8 @@ package com.ibm.common.activitystreams.registry; import static com.google.common.base.Throwables.propagate; +import static com.google.common.util.concurrent.Futures.immediateFuture; +import static com.google.common.util.concurrent.Futures.immediateCancelledFuture; import static com.google.common.util.concurrent.Futures.addCallback; import static com.google.common.util.concurrent.MoreExecutors.getExitingExecutorService; import static com.google.common.util.concurrent.MoreExecutors.listeningDecorator; @@ -21,6 +23,7 @@ import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.Monitor; import com.ibm.common.activitystreams.IO; import com.ibm.common.activitystreams.TypeValue; +import com.ibm.common.activitystreams.ValueType; import com.ibm.common.activitystreams.ext.ExtModule; /** @@ -202,8 +205,22 @@ public final class TypeValueRegistry (ThreadPoolExecutor)newFixedThreadPool(1))); } + public FutureresolveNoWait(TypeValue tv) { + try { + if (tv == null) return immediateCancelledFuture(); + return tv.valueType() == ValueType.OBJECT || isToken(tv) ? + immediateFuture(tv) : + executor.submit(strategy.resolverFor(tv)); + } catch (Throwable t) { + throw propagate(t); + } + } + public Future resolve(TypeValue tv) { try { + if (tv == null) return immediateCancelledFuture(); + if (tv.valueType() == ValueType.OBJECT || isToken(tv)) + return immediateFuture(tv); monitor.enterWhen(ready); return executor.submit(strategy.resolverFor(tv)); } catch (Throwable t) { @@ -218,6 +235,9 @@ public final class TypeValueRegistry long timeout, TimeUnit unit) { try { + if (tv == null) return immediateCancelledFuture(); + if (tv.valueType() == ValueType.OBJECT || isToken(tv)) + return immediateFuture(tv); if (monitor.enterWhen(ready, timeout, unit)) { return executor.submit(strategy.resolverFor(tv)); } else throw new IllegalStateException(); @@ -228,6 +248,13 @@ public final class TypeValueRegistry } } + private boolean isToken(TypeValue value) { + String id = value.id(); + return id != null ? + id.matches("[A-Za-z0-9\\!\\#\\$\\%\\&\\'\\*\\+\\-\\.\\^\\_\\`\\|\\~]+") : + false; + } + public Future apply(TypeValue input) { return resolve(input); }