Merge pull request #10 from jasnell/actions05update

Reworking the Actions parameter object per the updated Action Handlers
This commit is contained in:
James M Snell 2014-05-01 21:09:03 -07:00
commit bb323cbdc3
20 changed files with 1235 additions and 1028 deletions

View File

@ -134,5 +134,9 @@
<artifactId>activitystreams-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -43,6 +43,10 @@ public abstract class ActionHandler
private final Authentication.Builder auth =
Authentication.make();
public Builder() {
writeUsing(ActionMakers.io);
}
/**
* Method confirm.
* @return B
@ -361,4 +365,5 @@ public abstract class ActionHandler
return auth != null ?
(A)auth.get(key) : null;
}
}

View File

@ -24,11 +24,18 @@ package com.ibm.common.activitystreams.actions;
import static com.ibm.common.activitystreams.Makers.object;
import com.ibm.common.activitystreams.ASObject;
import com.ibm.common.activitystreams.IO;
import com.ibm.common.activitystreams.actions.ParameterValue.SimpleParameterValue;
/**
*/
public final class ActionMakers {
public static final IO io =
IO.makeDefault(ActionsModule.instance);
public static final IO ioPretty =
IO.makeDefaultPrettyPrint(ActionsModule.instance);
public static final String TARGET_NONE = "NONE";
public static final String TARGET_DEFAULT = "DEFAULT";
public static final String TARGET_NEW = "NEW";
@ -220,7 +227,7 @@ public final class ActionMakers {
* @param id String
* @return Parameter.Builder
*/
public static Parameter.Builder parameter(String id) {
return parameter().id(id);
public static SimpleParameterValue parameter(String id) {
return new SimpleParameterValue.Builder().type(id).get();
}
}

View File

@ -22,7 +22,6 @@
package com.ibm.common.activitystreams.actions;
import static com.ibm.common.activitystreams.actions.Adapters.AUTH;
import static com.ibm.common.activitystreams.actions.Adapters.PARAMETERS;
import static com.ibm.common.activitystreams.actions.Adapters.STYLES;
import com.ibm.common.activitystreams.IO.Builder;
@ -47,10 +46,10 @@ public final class ActionsModule
.hierarchicalAdapter(IntentActionHandler.class,base)
.hierarchicalAdapter(EmbedActionHandler.class,base)
.hierarchicalAdapter(HtmlForm.class,base)
.hierarchicalAdapter(ParametersValue.class, ParametersAdapter.instance)
.hierarchicalAdapter(ParameterValue.class, ParameterAdapter.instance)
.hierarchicalAdapter(UrlTemplate.class,base)
.hierarchicalAdapter(TypedPayload.class,base)
.hierarchicalAdapter(Parameter.class,base)
.hierarchicalAdapter(ParametersValue.class, PARAMETERS)
.hierarchicalAdapter(Authentication.class, AUTH)
.hierarchicalAdapter(StylesValue.class, STYLES);
}
@ -58,7 +57,6 @@ public final class ActionsModule
@Override
public void apply(Schema.Builder builder) {
builder.map("HtmlForm", withParameters.template(HtmlForm.class, HtmlForm.Builder.class))
.map("parameter", parameter)
.map("TypedPayload", typedPayload)
.map("UrlTemplate", withParameters.template(UrlTemplate.class, UrlTemplate.Builder.class))
.map("HttpActionHandler", actionHandler.template(HttpActionHandler.class, HttpActionHandler.Builder.class))
@ -89,15 +87,11 @@ public final class ActionsModule
public final static Model typedPayload =
Model
.make("object")
.type(TypedPayload.class, TypedPayload.Builder.class)
.linkValue("schema")
.typeValue("type")
.get();
public final static Model parameter =
Model
.make("object")
.typeValue("type")
.type(Parameter.class, Parameter.Builder.class)
.get();
}

View File

@ -42,17 +42,6 @@ final class Adapters {
}
};
static final Adapter<ParametersValue> PARAMETERS =
new AbstractDictionaryObjectAdapter
<TypeValue,
ParametersValue,
ParametersValue.Builder>(TypeValue.class) {
@Override
protected ParametersValue.Builder builder() {
return ParametersValue.make();
}
};
static final Adapter<StylesValue> STYLES =
new AbstractDictionaryObjectAdapter
<String,

View File

@ -47,6 +47,10 @@ public final class Authentication
extends AbstractDictionaryObject.AbstractBuilder<
ASObject,Authentication,Builder> {
public Builder() {
writeUsing(ActionMakers.io);
}
/**
* Method get.
* @return Authentication

View File

@ -29,6 +29,7 @@ import java.io.ObjectStreamException;
import com.google.common.base.Supplier;
import com.ibm.common.activitystreams.ASObject;
import com.ibm.common.activitystreams.TypeValue;
import com.ibm.common.activitystreams.actions.UrlTemplate.Builder;
/**
*/
@ -51,86 +52,28 @@ public final class HtmlForm
private final ParametersValue.Builder params =
ParametersValue.make();
private Builder() {
public Builder() {
writeUsing(ActionMakers.io);
objectType("HtmlForm");
mediaType("application/x-www-form-urlencoded");
}
/**
* Method parameter.
* @param name String
* @param iri String
* @return Builder
*/
public Builder parameter(
String name,
String iri) {
params.set(name, type(iri));
public Builder parameter(String name, String iri) {
params.param(name, iri);
return this;
}
/**
* Method parameter.
* @param name String
* @param iri String
* @param required boolean
* @return Builder
*/
public Builder parameter(
String name,
String iri,
boolean required) {
return parameter(
name,
object()
.id(iri)
.set("required", required));
ParameterValue parameter) {
params.param(name, parameter);
return this;
}
/**
* Method parameter.
* @param name String
* @param iri String
* @param required boolean
* @param value Object
* @return Builder
*/
public Builder parameter(
String name,
String iri,
boolean required,
Object value) {
return parameter(
name,
object()
.id(iri)
.set("required", required)
.set("value", value));
}
/**
* Method parameter.
* @param name String
* @param lv TypeValue
* @return Builder
*/
public Builder parameter(
String name,
TypeValue lv) {
params.set(name, lv);
return this;
}
/**
* Method parameter.
* @param name String
* @param lv Supplier<? extends TypeValue>
* @return Builder
*/
public Builder parameter(
String name,
Supplier<? extends TypeValue> lv) {
return parameter(name, lv.get());
Supplier<? extends ParameterValue> parameter) {
return parameter(name, parameter.get());
}
/**

View File

@ -0,0 +1,260 @@
package com.ibm.common.activitystreams.actions;
import java.lang.reflect.Type;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.collect.Iterables;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.ibm.common.activitystreams.ASObject;
import com.ibm.common.activitystreams.NLV;
import com.ibm.common.activitystreams.actions.ParameterValue.SimpleParameterValue;
import com.ibm.common.activitystreams.internal.Adapter;
public final class ParameterAdapter
extends Adapter<ParameterValue> {
public static final ParameterAdapter instance =
new ParameterAdapter();
@Override
public JsonElement serialize(
ParameterValue src,
Type typeOfSrc,
JsonSerializationContext context) {
if (src == null) return null;
switch(src.valueType()) {
case OBJECT:
if (src instanceof Parameter) {
Parameter parameter = (Parameter) src;
JsonObject obj = new JsonObject();
if (parameter.has("type")) {
String type = parameter.type();
if (type != null)
obj.addProperty("type", type);
}
if (parameter.has("displayName")) {
NLV nlv = parameter.displayName();
if (nlv != null)
obj.add("displayName", context.serialize(nlv, NLV.class));
}
if (parameter.has("placeholder")) {
NLV nlv = parameter.placeholder();
if (nlv != null)
obj.add("placeholder", context.serialize(nlv, NLV.class));
}
if (parameter.has("enumeration")) {
Iterable<Object> e = parameter.enumeration();
if (e != null)
obj.add("enumeration", context.serialize(e,e.getClass()));
}
if (parameter.has("language")) {
String lang = parameter.language();
if (lang != null)
obj.addProperty("language", lang);
}
if (parameter.has("maxInclusive")) {
Object max = parameter.maxInclusive();
if (max != null)
obj.add("maxInclusive", context.serialize(max, max.getClass()));
}
if (parameter.has("maxExclusive")) {
Object max = parameter.maxExclusive();
if (max != null)
obj.add("maxExclusive", context.serialize(max, max.getClass()));
}
if (parameter.has("minInclusive")) {
Object max = parameter.minInclusive();
if (max != null)
obj.add("minInclusive", context.serialize(max, max.getClass()));
}
if (parameter.has("minExclusive")) {
Object max = parameter.minExclusive();
if (max != null)
obj.add("minExclusive", context.serialize(max, max.getClass()));
}
if (parameter.has("pattern")) {
Iterable<String> pattern = parameter.pattern();
if (pattern != null) {
if (Iterables.size(pattern) == 0) {
String first = Iterables.getFirst(pattern, null);
if (first != null)
obj.addProperty("pattern", first);
} else
obj.add("pattern", context.serialize(pattern, pattern.getClass()));
}
}
if (parameter.has("repeated")) {
boolean repeated = parameter.repeated();
if (repeated)
obj.addProperty("repeated", repeated);
}
if (parameter.has("required")) {
boolean required = parameter.required();
if (!required)
obj.addProperty("required", required);
}
if (parameter.has("step")) {
Number n = parameter.step();
if (n != null)
obj.add("step", context.serialize(n,n.getClass()));
}
if (parameter.has("default")) {
Object def = parameter.defaultValue();
if (def != null)
obj.add("default",context.serialize(def, def.getClass()));
}
if (parameter.has("minLength")) {
int len = parameter.minLength();
if (len > -1)
obj.addProperty("minLength", len);
}
if (parameter.has("maxLength")) {
int len = parameter.maxLength();
if (len > -1)
obj.addProperty("maxLength", len);
}
if (parameter.has("totalDigits")) {
int len = parameter.totalDigits();
if (len > -1)
obj.addProperty("totalDigits", len);
}
if (parameter.has("fractionDigits")) {
int len = parameter.fractionDigits();
if (len > -1)
obj.addProperty("fractionDigits", len);
}
if (parameter.has("value")) {
Object val = parameter.value();
if (val != null)
obj.add("value", context.serialize(val, val.getClass()));
}
return obj;
} else if (src instanceof UrlTemplate) {
return context.serialize(src, UrlTemplate.class);
} else if (src instanceof TypedPayload) {
return context.serialize(src, TypedPayload.class);
} else if (src instanceof ASObject) {
return context.serialize(src, ASObject.class);
} else throw new IllegalArgumentException();
case SIMPLE:
SimpleParameterValue spv = (SimpleParameterValue) src;
return context.serialize(spv.type(), String.class);
default:
throw new IllegalArgumentException();
}
}
private Object deserialize(
JsonDeserializationContext context,
JsonElement el) {
if (el.isJsonArray()) {
return context.deserialize(el, Iterable.class);
} else if (el.isJsonObject()) {
return context.deserialize(el, ASObject.class);
} else if (el.isJsonPrimitive()) {
JsonPrimitive p = el.getAsJsonPrimitive();
if (p.isBoolean())
return p.getAsBoolean();
else if (p.isNumber())
return p.getAsNumber();
else
return p.getAsString();
} else return null;
}
@Override
public ParameterValue deserialize(
JsonElement json,
Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
checkArgument(json.isJsonPrimitive() || json.isJsonObject());
if (json.isJsonPrimitive()) {
JsonPrimitive jp = json.getAsJsonPrimitive();
checkArgument(jp.isString());
return ActionMakers.parameter(jp.getAsString());
} else {
JsonObject obj = json.getAsJsonObject();
if (obj.has("objectType")) {
ASObject as = context.deserialize(obj, ASObject.class);
checkArgument(as instanceof ParameterValue);
return (ParameterValue) as;
} else {
Parameter.Builder builder =
ActionMakers.parameter();
if (obj.has("default"))
builder.defaultValue(
deserialize(context, obj.get("default")));
if (obj.has("displayName"))
builder.displayName(
context.<NLV>deserialize(
obj.get("displayName"),
NLV.class));
if (obj.has("enumeration"))
builder.enumeration(
context.<Iterable<?>>deserialize(
obj.get("enumeration"),
Iterable.class));
if (obj.has("fractionDigits"))
builder.fractionDigits(
obj.get("fractionDigits").getAsInt());
if (obj.has("language"))
builder.language(
obj.get("language").getAsString());
if (obj.has("maxExclusive"))
builder.maxExclusive(
deserialize(context, obj.get("maxExclusive")));
if (obj.has("maxInclusive"))
builder.maxInclusive(
deserialize(context, obj.get("maxInclusive")));
if (obj.has("minExclusive"))
builder.minExclusive(
deserialize(context, obj.get("minExclusive")));
if (obj.has("minInclusive"))
builder.minInclusive(
deserialize(context, obj.get("minInclusive")));
if (obj.has("maxLength"))
builder.maxLength(
obj.get("maxLength").getAsInt());
if (obj.has("minLength"))
builder.minLength(
obj.get("minLength").getAsInt());
if (obj.has("pattern"))
builder.pattern(
context.<Iterable<String>>deserialize(
obj.get("pattern"), Iterable.class));
if (obj.has("placeholder"))
builder.placeholder(
context.<NLV>deserialize(
obj.get("placeholder"), NLV.class));
if (obj.has("repeated") && obj.get("repeated").getAsBoolean())
builder.repeated();
if (obj.has("required") && !obj.get("required").getAsBoolean())
builder.optional();
if (obj.has("step"))
builder.step(
obj.get("step").getAsNumber());
if (obj.has("totalDigits"))
builder.totalDigits(
obj.get("totalDigits").getAsInt());
if (obj.has("type"))
builder.type(
obj.get("type").getAsString());
if (obj.has("value"))
builder.value(
deserialize(context, obj.get("value")));
return builder.get();
}
}
}
}

View File

@ -0,0 +1,99 @@
package com.ibm.common.activitystreams.actions;
import java.io.Serializable;
import com.ibm.common.activitystreams.IO;
import com.ibm.common.activitystreams.ValueType;
import com.ibm.common.activitystreams.Writable;
import com.ibm.common.activitystreams.util.AbstractWritable;
import com.sun.org.apache.xalan.internal.utils.Objects;
public interface ParameterValue
extends Writable, Serializable {
ValueType valueType();
String toString(IO io);
public static final class SimpleParameterValue
extends AbstractWritable
implements ParameterValue {
static final class Builder
extends AbstractWritable.AbstractWritableBuilder<SimpleParameterValue,Builder> {
private String type;
public Builder() {
writeUsing(ActionMakers.io);
}
public Builder type(String type) {
this.type = type;
return this;
}
@Override
public SimpleParameterValue get() {
return new SimpleParameterValue(this);
}
}
private final String type;
private SimpleParameterValue(Builder b) {
super(b);
this.type = b.type;
}
public ValueType valueType() {
return ValueType.SIMPLE;
}
public String type() {
return type;
}
@Override
public int hashCode() {
return Objects.hashCode(type);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleParameterValue other = (SimpleParameterValue) obj;
return Objects.equals(type,other.type);
}
public String toString(IO io) {
return super.toString(io);
}
Object writeReplace() throws java.io.ObjectStreamException {
return new SerializedForm(this);
}
private static class SerializedForm
implements Serializable {
private static final long serialVersionUID = -1975376657749952999L;
private String type;
SerializedForm(SimpleParameterValue obj) {
this.type = obj.type;
}
Object readResolve()
throws java.io.ObjectStreamException {
return new SimpleParameterValue.Builder().type(type).get();
}
}
}
}

View File

@ -0,0 +1,57 @@
package com.ibm.common.activitystreams.actions;
import java.lang.reflect.Type;
import java.util.Map;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.ibm.common.activitystreams.internal.Adapter;
public final class ParametersAdapter
extends Adapter<ParametersValue> {
public static final ParametersAdapter instance =
new ParametersAdapter();
@Override
public JsonElement serialize(
ParametersValue params,
Type type,
JsonSerializationContext context) {
if (params == null) return null;
JsonObject obj = new JsonObject();
for (String param : params) {
obj.add(
param,
context.serialize(
params.get(param),
ParameterValue.class));
}
return obj;
}
@Override
public ParametersValue deserialize(
JsonElement json,
Type type,
JsonDeserializationContext context)
throws JsonParseException {
checkArgument(json.isJsonObject());
JsonObject obj = json.getAsJsonObject();
ParametersValue.Builder builder =
ParametersValue.make();
for (Map.Entry<String,JsonElement> entry : obj.entrySet())
builder.param(
entry.getKey(),
context.<ParameterValue>deserialize(
entry.getValue(),
ParameterValue.class));
return builder.get();
}
}

View File

@ -22,13 +22,13 @@
package com.ibm.common.activitystreams.actions;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.ibm.common.activitystreams.ASObject;
import com.ibm.common.activitystreams.TypeValue;
import com.ibm.common.activitystreams.ValueType;
import com.ibm.common.activitystreams.util.AbstractDictionaryObject;
import com.google.common.collect.Maps;
import com.ibm.common.activitystreams.util.AbstractWritable;
/**
* The value of the "parameters" property...
@ -36,115 +36,81 @@ import com.ibm.common.activitystreams.util.AbstractDictionaryObject;
* @version $Revision: 1.0 $
*/
public final class ParametersValue
extends AbstractDictionaryObject<TypeValue>
implements Serializable {
extends AbstractWritable
implements Serializable, Iterable<String> {
/**
* Method make.
* @return Builder */
public static Builder make() {
return new Builder();
}
/**
* @author james
* @version $Revision: 1.0 $
*/
public static final class Builder
extends AbstractDictionaryObject.AbstractBuilder
<TypeValue,ParametersValue,Builder> {
extends AbstractWritable.AbstractWritableBuilder
<ParametersValue,Builder> {
/**
* Method set.
* @param param String
* @param iri String
private final Map<String,Object> params =
Maps.newHashMap();
* @return Builder */
public Builder set(String param, String iri) {
return super.set(
param,
TypeValue.SimpleTypeValue.make(iri));
public Builder() {
writeUsing(ActionMakers.io);
}
/**
* Method get.
public boolean notEmpty() {
return !params.isEmpty();
}
public Builder param(String param, String iri) {
params.put(param, ActionMakers.parameter(iri));
return this;
}
public Builder param(
String param,
ParameterValue parameter) {
params.put(param, parameter);
return this;
}
public Builder param(
String param,
Supplier<? extends ParameterValue> parameter) {
params.put(param, parameter);
return this;
}
* @return ParametersValue * @see com.google.common.base.Supplier#get() */
public ParametersValue get() {
return new ParametersValue(this);
}
}
private final ImmutableMap<String,Object> params;
/**
* Constructor for ParametersValue.
* @param builder Builder
*/
ParametersValue(Builder builder) {
super(builder);
this.params = ImmutableMap.copyOf(builder.params);
}
/**
* Method get.
* @param param String
* @return TypeValue */
public Iterator<String> iterator() {
return params.keySet().iterator();
}
@SuppressWarnings("unchecked")
public <T extends TypeValue>T get(String param) {
return (T)super.getSingle(param);
public <O extends ParameterValue>O get(String param) {
return (O)params.get(param);
}
/**
* Method id.
* @param param String
* @return String
*/
public String id(String param) {
TypeValue tv = get(param);
return tv != null ? tv.id() : null;
public boolean has(String param) {
return params.containsKey(param);
}
/**
* Method required.
* @param param String
* @return boolean
*/
public boolean required(String param) {
TypeValue tv = get(param);
if (tv == null)
return false;
if (tv.valueType() == ValueType.SIMPLE)
return true;
ASObject obj = (ASObject) tv;
return obj.getBoolean("required", true);
}
/**
* Method value.
* @param param String
* @param defaultValue O
* @return O
*/
public <O>O value(String param, O defaultValue) {
TypeValue tv = get(param);
if (tv == null || tv.valueType() == ValueType.SIMPLE)
return defaultValue;
ASObject obj = (ASObject) tv;
return obj.<O>get("value", defaultValue);
}
/**
* Method value.
* @param param String
* @return O
*/
public <O>O value(String param) {
return value(param,null);
}
Object writeReplace() throws java.io.ObjectStreamException {
return new SerializedForm(this);
}
@ -154,19 +120,14 @@ public final class ParametersValue
private static final long serialVersionUID = -1975376657749952999L;
private ImmutableMap<String,Object> map;
SerializedForm(ParametersValue obj) {
ImmutableMap.Builder<String,Object> builder =
ImmutableMap.builder();
for (String key : obj)
builder.put(key, obj.get(key));
this.map = builder.build();
map = obj.params;
}
Object readResolve()
throws java.io.ObjectStreamException {
ParametersValue.Builder builder =
ParametersValue.make();
for (Map.Entry<String,Object> entry : map.entrySet())
builder.set(entry.getKey(), (TypeValue) entry.getValue());
builder.params.putAll(map);
return builder.get();
}
}

View File

@ -56,6 +56,10 @@ public final class StylesValue
extends AbstractDictionaryObject.AbstractBuilder
<String, StylesValue, Builder> {
public Builder() {
writeUsing(ActionMakers.io);
}
/**
* Method media.
* @param query String

View File

@ -30,10 +30,9 @@ import com.ibm.common.activitystreams.ASObject;
import com.ibm.common.activitystreams.LinkValue;
import com.ibm.common.activitystreams.TypeValue;
/**
*/
public final class TypedPayload
extends ASObject {
extends ASObject
implements ParameterValue {
/**
* Method makeTypedPayload.
@ -62,7 +61,8 @@ public final class TypedPayload
public static final class Builder
extends ASObject.AbstractBuilder<TypedPayload, Builder> {
private Builder() {
public Builder() {
writeUsing(ActionMakers.io);
objectType("TypedPayload");
}

View File

@ -21,19 +21,16 @@
*/
package com.ibm.common.activitystreams.actions;
import static com.ibm.common.activitystreams.Makers.type;
import static com.ibm.common.activitystreams.Makers.object;
import java.io.ObjectStreamException;
import com.google.common.base.Supplier;
import com.ibm.common.activitystreams.ASObject;
import com.ibm.common.activitystreams.TypeValue;
/**
*/
public final class UrlTemplate
extends ASObject {
extends ASObject
implements ParameterValue {
/**
* Method makeUrlTemplate.
@ -51,95 +48,34 @@ public final class UrlTemplate
private final ParametersValue.Builder params =
ParametersValue.make();
private Builder() {
public Builder() {
writeUsing(ActionMakers.io);
objectType("UrlTemplate");
}
/**
* Method template.
* @param template String
* @return Builder
*/
public Builder template(String template) {
set("template", template);
return this;
}
/**
* Method parameter.
* @param name String
* @param iri String
* @return Builder
*/
public Builder parameter(String name, String iri) {
params.set(name, type(iri));
params.param(name, iri);
return this;
}
/**
* Method parameter.
* @param name String
* @param iri String
* @param required boolean
* @return Builder
*/
public Builder parameter(
String name,
String iri,
boolean required) {
return parameter(
name,
object()
.id(iri)
.set("required", required));
ParameterValue parameter) {
params.param(name, parameter);
return this;
}
/**
* Method parameter.
* @param name String
* @param iri String
* @param required boolean
* @param value Object
* @return Builder
*/
public Builder parameter(
String name,
String iri,
boolean required,
Object value) {
return parameter(
name,
object()
.id(iri)
.set("required", required)
.set("value", value));
Supplier<? extends ParameterValue> parameter) {
return parameter(name, parameter.get());
}
/**
* Method parameter.
* @param name String
* @param lv TypeValue
* @return Builder
*/
public Builder parameter(
String name,
TypeValue lv) {
params.set(name, lv);
return this;
}
/**
* Method parameter.
* @param name String
* @param lv Supplier<? extends TypeValue>
* @return Builder
*/
public Builder parameter(
String name,
Supplier<? extends TypeValue> lv) {
return parameter(name, lv.get());
}
/**
* Method get.
* @return UrlTemplate

View File

@ -0,0 +1,159 @@
package com.ibm.common.activitystreams.actions.test;
import static com.ibm.common.activitystreams.actions.ActionMakers.htmlForm;
import static com.ibm.common.activitystreams.actions.ActionMakers.io;
import static com.ibm.common.activitystreams.actions.ActionMakers.parameter;
import static com.ibm.common.activitystreams.actions.ActionMakers.typedPayload;
import static com.ibm.common.activitystreams.actions.ActionMakers.urlTemplate;
import static com.ibm.common.activitystreams.actions.ActionMakers.embedAction;
import static com.ibm.common.activitystreams.actions.ActionMakers.intentAction;
import static com.ibm.common.activitystreams.actions.ActionMakers.httpAction;
import static com.ibm.common.activitystreams.actions.ActionMakers.styles;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.junit.Test;
import static com.google.common.collect.Iterables.*;
import com.google.common.net.MediaType;
import com.ibm.common.activitystreams.ASObject;
import com.ibm.common.activitystreams.LinkValue;
import com.ibm.common.activitystreams.LinkValue.SimpleLinkValue;
import com.ibm.common.activitystreams.actions.EmbedActionHandler;
import com.ibm.common.activitystreams.actions.HtmlForm;
import com.ibm.common.activitystreams.actions.HttpActionHandler;
import com.ibm.common.activitystreams.actions.IntentActionHandler;
import com.ibm.common.activitystreams.actions.Parameter;
import com.ibm.common.activitystreams.actions.ParametersValue;
import com.ibm.common.activitystreams.actions.StylesValue;
import com.ibm.common.activitystreams.actions.TypedPayload;
import com.ibm.common.activitystreams.actions.UrlTemplate;
public final class ActionsTest {
@Test
public void roundtripHttpActionHandler() {
HttpActionHandler hah1 =
httpAction()
.url("http://example.org")
.method("GET")
.get();
HttpActionHandler hah2 =
roundTrip(hah1);
assertEquals("GET", hah2.method());
Iterable<LinkValue> i = hah2.url();
LinkValue lv = getFirst(i,null);
assertNotNull(lv);
assertTrue(lv instanceof SimpleLinkValue);
SimpleLinkValue slv = (SimpleLinkValue) lv;
assertEquals("http://example.org", slv.url());
}
@Test
public void roundtripIntentActionHandler() {
IntentActionHandler iah1 =
intentAction()
.id("urn:example:foo:1")
.get();
IntentActionHandler iah2 =
roundTrip(iah1);
assertEquals("urn:example:foo:1", iah2.id());
}
@Test
public void roundtripEmbedActionHandler() {
EmbedActionHandler eah1 =
embedAction()
.style(
styles()
.set("width", "100px")
)
.get();
EmbedActionHandler eah2 =
roundTrip(eah1);
Iterable<StylesValue> styles =
eah2.styles();
StylesValue style =
getFirst(styles, null);
assertNotNull(style);
assertTrue(style.has("width"));
assertEquals("100px", style.get("width"));
}
@Test
public void roundtripHtmlForm() {
HtmlForm htmlForm1 =
htmlForm()
.parameter("foo", parameter().optional())
.get();
HtmlForm htmlForm2 =
roundTrip(htmlForm1);
ParametersValue params =
htmlForm2.parameters();
assertTrue(params.has("foo"));
Parameter param = params.get("foo");
assertFalse(param.required());
}
@Test
public void roundtripTypedValue() {
TypedPayload typedPayload1 =
typedPayload()
.mediaType(MediaType.create("application", "json"))
.get();
TypedPayload typedPayload2 =
roundTrip(typedPayload1);
assertEquals(
typedPayload1.mediaType(),
typedPayload2.mediaType());
}
@Test
public void roundtripUrlTemplate() {
UrlTemplate template1 =
urlTemplate()
.template("http://example.org{/foo}")
.parameter("foo", "bar")
.get();
UrlTemplate template2 =
roundTrip(template1);
assertEquals(
template1.template(),
template2.template());
ParametersValue value =
template2.parameters();
assertTrue(value.has("foo"));
}
private <T extends ASObject>T roundTrip(T writable) {
ByteArrayOutputStream out =
new ByteArrayOutputStream();
writable.writeTo(out,io);
ByteArrayInputStream in =
new ByteArrayInputStream(out.toByteArray());
return io.readAs(in);
}
}

View File

@ -23,7 +23,6 @@ package com.ibm.common.activitystreams;
import static com.google.common.base.Enums.getIfPresent;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.ImmutableList.of;
import static com.google.common.collect.Iterables.filter;
@ -45,6 +44,7 @@ import static com.ibm.common.activitystreams.util.Converters.toInterval;
import static com.ibm.common.activitystreams.util.Converters.toLong;
import static com.ibm.common.activitystreams.util.Converters.toPeriod;
import static com.ibm.common.activitystreams.util.Converters.toShort;
import static com.ibm.common.activitystreams.util.Util.DEFAULT_LOCALE;
import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import static org.joda.time.DateTimeZone.UTC;
@ -53,7 +53,6 @@ import static org.joda.time.Duration.standardSeconds;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
@ -144,19 +143,7 @@ public class ASObject
checkArgument(l >= 0);
return l;
}
/**
* Method convLocale.
* @param locale Locale
* @return String
**/
private static String convLocale(Locale locale) {
return checkNotNull(locale).toString().replaceAll("_", "-");
}
private static final String DEFAULT_LOCALE =
convLocale(Locale.getDefault());
/**
* Abstract base builder for ASObject instances.
*/

View File

@ -94,4 +94,5 @@ public interface Writable {
* @return Future&lt;?>
*/
Future<?> writeTo(Writer out, IO io, ExecutorService executor);
}

View File

@ -237,7 +237,29 @@ public final class GsonWrapper {
GsonBuilder gson = new GsonBuilder()
.registerTypeHierarchyAdapter(TypeValue.class, new TypeValueAdapter(schema))
.registerTypeHierarchyAdapter(LinkValue.class, new LinkValueAdapter(schema))
.registerTypeHierarchyAdapter(Iterable.class, ITERABLE);
.registerTypeHierarchyAdapter(Iterable.class, ITERABLE)
.registerTypeHierarchyAdapter(ASObject.class, base)
.registerTypeHierarchyAdapter(Collection.class, base)
.registerTypeHierarchyAdapter(Activity.class, base)
.registerTypeHierarchyAdapter(NLV.class, NLV)
.registerTypeHierarchyAdapter(ActionsValue.class, ACTIONS)
.registerTypeHierarchyAdapter(Optional.class, OPTIONAL)
.registerTypeHierarchyAdapter(Range.class, RANGE)
.registerTypeHierarchyAdapter(Table.class, TABLE)
.registerTypeHierarchyAdapter(LazilyParsedNumber.class, NUMBER)
.registerTypeHierarchyAdapter(LazilyParsedNumberComparable.class, NUMBER)
.registerTypeHierarchyAdapter(ReadableDuration.class, DURATION)
.registerTypeHierarchyAdapter(ReadablePeriod.class, PERIOD)
.registerTypeHierarchyAdapter(ReadableInterval.class, INTERVAL)
.registerTypeAdapter(
Activity.Status.class,
forEnum(
Activity.Status.class,
Activity.Status.OTHER))
.registerTypeAdapter(Date.class, DATE)
.registerTypeAdapter(DateTime.class, DATETIME)
.registerTypeAdapter(MediaType.class, MIMETYPE)
.registerTypeHierarchyAdapter(Multimap.class, MULTIMAP);
for (AdapterEntry<?> entry : adapters) {
if (entry.hier)
@ -252,30 +274,8 @@ public final class GsonWrapper {
entry.adapter:base);
}
return gson
.registerTypeHierarchyAdapter(NLV.class, NLV)
.registerTypeHierarchyAdapter(ActionsValue.class, ACTIONS)
.registerTypeHierarchyAdapter(Optional.class, OPTIONAL)
.registerTypeHierarchyAdapter(Range.class, RANGE)
.registerTypeHierarchyAdapter(Table.class, TABLE)
.registerTypeHierarchyAdapter(LazilyParsedNumber.class, NUMBER)
.registerTypeHierarchyAdapter(LazilyParsedNumberComparable.class, NUMBER)
.registerTypeHierarchyAdapter(ASObject.class, base)
.registerTypeHierarchyAdapter(Collection.class, base)
.registerTypeHierarchyAdapter(Activity.class, base)
.registerTypeHierarchyAdapter(ReadableDuration.class, DURATION)
.registerTypeHierarchyAdapter(ReadablePeriod.class, PERIOD)
.registerTypeHierarchyAdapter(ReadableInterval.class, INTERVAL)
.registerTypeAdapter(
Activity.Status.class,
forEnum(
Activity.Status.class,
Activity.Status.OTHER))
.registerTypeAdapter(Date.class, DATE)
.registerTypeAdapter(DateTime.class, DATETIME)
.registerTypeAdapter(MediaType.class, MIMETYPE)
.registerTypeHierarchyAdapter(Multimap.class, MULTIMAP)
;
return gson;
}
/**

View File

@ -0,0 +1,23 @@
package com.ibm.common.activitystreams.util;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Locale;
public final class Util {
private Util() {}
/**
* Method convLocale.
* @param locale Locale
* @return String
**/
public static String convLocale(Locale locale) {
return checkNotNull(locale).toString().replaceAll("_", "-");
}
public static final String DEFAULT_LOCALE =
convLocale(Locale.getDefault());
}