Fixing silly bugs and rounding out the type registry functionality a

bit more
This commit is contained in:
James M Snell 2014-04-25 12:46:27 -07:00
parent 8b63519dd7
commit 00773c5480
8 changed files with 130 additions and 10 deletions

View File

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

View File

@ -22,6 +22,8 @@
package com.ibm.common.activitystreams;
import java.io.Serializable;
import java.util.Objects;
import com.ibm.common.activitystreams.util.AbstractWritable;
/**
@ -123,6 +125,23 @@ public interface TypeValue
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

View File

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

View File

@ -149,5 +149,9 @@
<version>4.3.3</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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<TypeValue>() {
public TypeValue call() {
return t;
}
});
} catch (Throwable e) {}
}
}
}

View File

@ -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<Object>() {
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 Future<TypeValue>resolveNoWait(String id) {
return resolveNoWait(Makers.type(id));
}
public Future<TypeValue>resolve(String id) {
return resolve(Makers.type(id));
}
public Future<TypeValue>resolve(String id, long duration, TimeUnit unit) {
return resolve(Makers.type(id),duration,unit);
}
public Future<TypeValue>resolveNoWait(TypeValue tv) {
try {
if (tv == null) return immediateCancelledFuture();

View File

@ -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<TypeValue> object = tvr.resolve("urn:example:verbs:foo");
System.out.println(object.get().valueType());
}
}

View File

@ -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"
}
]
}