From 00773c548036a2906ad283876d4c94a24a4aed93 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 25 Apr 2014 12:46:27 -0700 Subject: [PATCH] Fixing silly bugs and rounding out the type registry functionality a bit more --- .../common/activitystreams/Collection.java | 17 ++++++++- .../ibm/common/activitystreams/TypeValue.java | 21 ++++++++++- .../internal/ASObjectAdapter.java | 4 +-- typext/pom.xml | 4 +++ .../registry/CachingResolutionStrategy.java | 15 ++++++-- .../registry/TypeValueRegistry.java | 29 +++++++++++++-- .../activitystreams/ext/test/ExtTest.java | 35 +++++++++++++++++++ typext/src/test/resources/typeValues.json | 15 ++++++++ 8 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 typext/src/test/java/com/ibm/common/activitystreams/ext/test/ExtTest.java create mode 100644 typext/src/test/resources/typeValues.json diff --git a/core/src/main/java/com/ibm/common/activitystreams/Collection.java b/core/src/main/java/com/ibm/common/activitystreams/Collection.java index 8d48025..83abf21 100755 --- a/core/src/main/java/com/ibm/common/activitystreams/Collection.java +++ b/core/src/main/java/com/ibm/common/activitystreams/Collection.java @@ -164,13 +164,28 @@ public class Collection **/ protected abstract A create(); + @Override + public B set(String key, Object value) { + if (key.equals("items")) { + if (value instanceof ArrayLinkValue) { + ArrayLinkValue alv = (ArrayLinkValue) value; + for (LinkValue lv : alv) { + list.add((ASObject)lv); + } + } else if (value instanceof ASObject) + list.add((ASObject) value); + return (B)this; + } else return super.set(key,value); + } + + /** * Method get. * @return A * @see com.google.common.base.Supplier#get() **/ public A get() { - set("items", list.build()); + super.set("items", list.build()); return create(); } diff --git a/core/src/main/java/com/ibm/common/activitystreams/TypeValue.java b/core/src/main/java/com/ibm/common/activitystreams/TypeValue.java index a02fd69..dffcfc6 100755 --- a/core/src/main/java/com/ibm/common/activitystreams/TypeValue.java +++ b/core/src/main/java/com/ibm/common/activitystreams/TypeValue.java @@ -22,6 +22,8 @@ package com.ibm.common.activitystreams; import java.io.Serializable; +import java.util.Objects; + import com.ibm.common.activitystreams.util.AbstractWritable; /** @@ -122,7 +124,24 @@ public interface TypeValue super(builder); this.iri = builder.iri; } - + + @Override + public int hashCode() { + return Objects.hash(iri); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SimpleTypeValue other = (SimpleTypeValue) obj; + return Objects.equals(iri,other.iri); + } + /** * Return the type value identifier * @return String diff --git a/core/src/main/java/com/ibm/common/activitystreams/internal/ASObjectAdapter.java b/core/src/main/java/com/ibm/common/activitystreams/internal/ASObjectAdapter.java index 651eb64..348fe79 100755 --- a/core/src/main/java/com/ibm/common/activitystreams/internal/ASObjectAdapter.java +++ b/core/src/main/java/com/ibm/common/activitystreams/internal/ASObjectAdapter.java @@ -223,7 +223,7 @@ public class ASObjectAdapter _class != null ? context.deserialize(val,_class) : primConverter.convert(val.getAsJsonPrimitive())); - else if (val.isJsonArray()) + else if (val.isJsonArray()) { builder.set( name, LinkValue.class.isAssignableFrom(_class!=null?_class:Object.class) ? @@ -233,7 +233,7 @@ public class ASObjectAdapter _class, context, builder())); - else if (val.isJsonObject()) + } else if (val.isJsonObject()) builder.set( name, context.deserialize( diff --git a/typext/pom.xml b/typext/pom.xml index 80fdf66..9966778 100644 --- a/typext/pom.xml +++ b/typext/pom.xml @@ -149,5 +149,9 @@ 4.3.3 pom + + junit + junit + \ No newline at end of file diff --git a/typext/src/main/java/com/ibm/common/activitystreams/registry/CachingResolutionStrategy.java b/typext/src/main/java/com/ibm/common/activitystreams/registry/CachingResolutionStrategy.java index be4d7bf..ff4455b 100644 --- a/typext/src/main/java/com/ibm/common/activitystreams/registry/CachingResolutionStrategy.java +++ b/typext/src/main/java/com/ibm/common/activitystreams/registry/CachingResolutionStrategy.java @@ -110,9 +110,18 @@ public abstract class CachingResolutionStrategy this.cache = cache; } - public void receive(TypeValue t) { - if (t.valueType() == ValueType.OBJECT && t.id() != null) - cache.put(Makers.type(t.id()),t); + public void receive(final TypeValue t) { + if (t.valueType() == ValueType.OBJECT && t.id() != null) { + final TypeValue tv = Makers.type(t.id()); + cache.invalidate(tv); + try { + TypeValue tt = cache.get(tv, new Callable() { + public TypeValue call() { + return t; + } + }); + } catch (Throwable e) {} + } } } 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 c7daa80..e829e30 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 @@ -22,6 +22,7 @@ import com.google.common.util.concurrent.ListenableFuture; 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.Makers; import com.ibm.common.activitystreams.TypeValue; import com.ibm.common.activitystreams.ValueType; import com.ibm.common.activitystreams.ext.ExtModule; @@ -165,11 +166,21 @@ public final class TypeValueRegistry future, new FutureCallback() { public void onSuccess(Object result) { - readyStatus = Status.READY; + monitor.enter(); + try { + readyStatus = Status.READY; + } finally { + monitor.leave(); + } } public void onFailure(Throwable t) { - readyStatus = Status.ERROR; - loadError = t; + monitor.enter(); + try { + readyStatus = Status.ERROR; + loadError = t; + } finally { + monitor.leave(); + } } }); return future; @@ -205,6 +216,18 @@ public final class TypeValueRegistry (ThreadPoolExecutor)newFixedThreadPool(1))); } + public FutureresolveNoWait(String id) { + return resolveNoWait(Makers.type(id)); + } + + public Futureresolve(String id) { + return resolve(Makers.type(id)); + } + + public Futureresolve(String id, long duration, TimeUnit unit) { + return resolve(Makers.type(id),duration,unit); + } + public FutureresolveNoWait(TypeValue tv) { try { if (tv == null) return immediateCancelledFuture(); diff --git a/typext/src/test/java/com/ibm/common/activitystreams/ext/test/ExtTest.java b/typext/src/test/java/com/ibm/common/activitystreams/ext/test/ExtTest.java new file mode 100644 index 0000000..1d80d54 --- /dev/null +++ b/typext/src/test/java/com/ibm/common/activitystreams/ext/test/ExtTest.java @@ -0,0 +1,35 @@ +package com.ibm.common.activitystreams.ext.test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.util.concurrent.Future; + +import org.junit.Test; + +import static com.ibm.common.activitystreams.Makers.type; + +import com.google.common.util.concurrent.Monitor; +import com.ibm.common.activitystreams.Collection; +import com.ibm.common.activitystreams.IO; +import com.ibm.common.activitystreams.Makers; +import com.ibm.common.activitystreams.TypeValue; +import com.ibm.common.activitystreams.ext.ExtModule; +import com.ibm.common.activitystreams.registry.TypeValueRegistry; + +public class ExtTest { + + private static final IO io = IO.makeDefault(ExtModule.instance); + + @Test + public void extTest() throws Exception { + + TypeValueRegistry tvr = + TypeValueRegistry + .makeDefaultSilent(io); + + Future object = tvr.resolve("urn:example:verbs:foo"); + + System.out.println(object.get().valueType()); + } + +} diff --git a/typext/src/test/resources/typeValues.json b/typext/src/test/resources/typeValues.json new file mode 100644 index 0000000..c00d402 --- /dev/null +++ b/typext/src/test/resources/typeValues.json @@ -0,0 +1,15 @@ +{ + "objectType": "collection", + "items": [ + { + "objectType": "verb", + "id": "urn:example:verbs:foo", + "displayName": "Foo" + }, + { + "objectType": "objectType", + "id": "urn:example:types:bar", + "displayName": "Bar" + } + ] +} \ No newline at end of file