diff --git a/actions/pom.xml b/actions/pom.xml index 055246c..0d074df 100644 --- a/actions/pom.xml +++ b/actions/pom.xml @@ -134,5 +134,9 @@ activitystreams-core 0.0.1-SNAPSHOT + + junit + junit + \ No newline at end of file diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionHandler.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionHandler.java index e01ef4b..843581d 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionHandler.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionHandler.java @@ -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; } + } diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionMakers.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionMakers.java index adc5b2d..71bd7fb 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionMakers.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionMakers.java @@ -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(); } } diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionsModule.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionsModule.java index 7de236c..6604b8f 100644 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionsModule.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/ActionsModule.java @@ -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(); + } diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java index a441920..1526904 100644 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/Adapters.java @@ -42,17 +42,6 @@ final class Adapters { } }; - static final Adapter PARAMETERS = - new AbstractDictionaryObjectAdapter - (TypeValue.class) { - @Override - protected ParametersValue.Builder builder() { - return ParametersValue.make(); - } - }; - static final Adapter STYLES = new AbstractDictionaryObjectAdapter { + public Builder() { + writeUsing(ActionMakers.io); + } + /** * Method get. * @return Authentication diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java index 752e962..ee30e91 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/HtmlForm.java @@ -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 - * @return Builder - */ - public Builder parameter( - String name, - Supplier lv) { - return parameter(name, lv.get()); + Supplier parameter) { + return parameter(name, parameter.get()); } /** diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/Parameter.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/Parameter.java index 02fd156..d692442 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/Parameter.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/Parameter.java @@ -21,67 +21,35 @@ */ package com.ibm.common.activitystreams.actions; -import java.io.ObjectStreamException; +import static com.ibm.common.activitystreams.Makers.nlv; +import static com.ibm.common.activitystreams.util.Util.DEFAULT_LOCALE; + +import java.io.Serializable; +import java.util.Map; import java.util.regex.Pattern; +import com.google.common.base.Objects; import com.google.common.base.Supplier; +import com.google.common.collect.BoundType; import com.google.common.collect.ImmutableList; -import com.ibm.common.activitystreams.ASObject; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; +import com.google.common.collect.Ordering; +import com.google.common.collect.Range; +import com.ibm.common.activitystreams.Makers; import com.ibm.common.activitystreams.NLV; -import com.ibm.common.activitystreams.TypeValue; +import com.ibm.common.activitystreams.ValueType; +import com.ibm.common.activitystreams.NLV.MapNLV; +import com.ibm.common.activitystreams.NLV.SimpleNLV; +import com.ibm.common.activitystreams.util.AbstractWritable; -/** - */ +@SuppressWarnings("unchecked") public class Parameter - extends ASObject { - - /** - */ - public static enum Format { - OTHER(null), - BOOLEAN("boolean"), - INT32("int32"), - INT64("int64"), - UINT32("uint32"), - UINT64("uint64"), - DOUBLE("double"), - FLOAT("float"), - BYTE("byte"), - DATE("date"), - DATETIME("date-time"), - DURATION("duration"), - LANG("lang"), - URI("uri"), - IRI("iri") - ; + extends AbstractWritable + implements ParameterValue { - private final String label; - - /** - * Constructor for Format. - * @param label String - */ - Format(String label) { - this.label = label; - } - - /** - * Method select. - * @param label String - * @return Format - */ - private static Format select(String label) { - try { - if (label == null) - return Format.OTHER; - label = label.toUpperCase().replaceAll("-", ""); - return valueOf(label); - } catch (Throwable t) { - return Format.OTHER; - } - } - } - /** * Method makeParameter. * @return Builder @@ -89,454 +57,322 @@ public class Parameter public static Builder makeParameter() { return new Builder(); } - - /** - */ - public static final class Builder - extends AbstractBuilder { - /** - * Method get. - * @return Parameter - * @see com.google.common.base.Supplier#get() - */ + public static class Builder + extends AbstractWritable.AbstractWritableBuilder { + + protected Builder() { + writeUsing(ActionMakers.io); + } + + private final Map map = + Maps.newHashMap(); + + public Builder language(String lang) { + if (lang != null) + map.put("language", lang); + else + map.remove("language"); + return this; + } + + protected Builder _nlv(String key, String value) { + if (value != null) + map.put(key, nlv(value)); + else + map.remove(key); + return this; + } + + protected Builder _nlv(String key, NLV nlv) { + if (nlv != null) + map.put(key, nlv); + else + map.remove(key); + return this; + } + + protected Builder _nlv(String key, Supplier nlv) { + return _nlv(key,nlv.get()); + } + + protected Builder _nlv(String key, Map map) { + if (map != null) + for (Map.Entry entry : map.entrySet()) + _nlv(key,entry.getKey(),entry.getValue()); + else + this.map.remove(key); + return this; + } + + protected Builder _nlv(String key, String lang, String value) { + if (map.containsKey(key)) { + Object obj = map.get(key); + if (obj instanceof NLV) { + NLV nlv = (NLV) obj; + switch(nlv.valueType()) { + case SIMPLE: + String l = (String) map.get("language"); + if (l == null) + l = DEFAULT_LOCALE; + NLV.MapNLV.Builder b = + Makers.nlv(); + if (lang.equals(l)) + b.set(lang, value); + else + b.set(l, ((NLV.SimpleNLV)obj).value()) + .set(lang, value); + map.put(key, b); + return this; + case OBJECT: + map.put(key, + Makers.nlv() + .from((NLV.MapNLV)obj, lang) + .set(lang, value)); + return this; + default: + throw new IllegalArgumentException(); + } + } else if (obj instanceof NLV.MapNLV.Builder) { + ((NLV.MapNLV.Builder) obj).set(lang, value); + return this; + } + } + map.put(key, Makers.nlv().set(lang,value)); + return this; + } + + public Builder displayName(String val) { + return _nlv("displayName",val); + } + + public Builder displayName(NLV nlv) { + return _nlv("displayName", nlv); + } + + public Builder displayName(Supplier nlv) { + return _nlv("displayName", nlv); + } + + public Builder displayName(String lang, String val) { + return _nlv("displayName", lang, val); + } + + public Builder placeholder(String placeholder) { + return _nlv("placeholder", placeholder); + } + + public Builder placeholder(NLV placeholder) { + return _nlv("placeholder", placeholder); + } + + public Builder placeholder(Supplier nlv) { + return _nlv("placeholder", nlv); + } + + public Builder placeholder(String lang, String val) { + return _nlv("placeholder", lang, val); + } + + public Builder type(String type) { + if (type != null) + map.put("type", type); + else + map.remove("type"); + return this; + } + + public Builder required() { + map.remove("required"); + return this; + } + + public Builder optional() { + map.put("required", false); + return this; + } + + public Builder repeated() { + map.put("repeated", true); + return this; + } + + public Builder notRepeated() { + map.remove("repeated"); + return this; + } + + public Builder value(Object value) { + if (value != null) + map.put("value", value); + else + map.remove("value"); + return this; + } + + public Builder defaultValue(Object value) { + if (value != null) + map.put("default", value); + else + map.remove("default"); + return this; + } + + public Builder pattern(Pattern... pattern) { + if (pattern != null && pattern.length > 0) { + ImmutableSet.Builder patterns = + ImmutableSet.builder(); + for (Pattern p : pattern) + patterns.add(p.pattern()); + map.put("pattern",patterns.build()); + } else { + map.remove("pattern"); + } + return this; + } + + public Builder pattern(String... pattern) { + if (pattern != null && pattern.length > 0) { + ImmutableSet patterns = + ImmutableSet.copyOf(pattern); + map.put("pattern",patterns); + } else { + map.remove("pattern"); + } + return this; + } + + public Builder pattern(Iterable patterns) { + if (patterns != null && Iterables.size(patterns) > 0) { + map.put("pattern", ImmutableSet.copyOf(patterns)); + } else + map.remove("pattern"); + return this; + } + + public Builder minInclusive(Object min) { + if (min != null) + map.put("minInclusive", min); + else + map.remove("minInclusive"); + return this; + } + + public Builder minExclusive(Object min) { + if (min != null) + map.put("minExclusive", min); + else + map.remove("minExclusive"); + return this; + } + + public Builder maxInclusive(Object max) { + if (max != null) + map.put("maxInclusive", max); + else + map.remove("maxInclusive"); + return this; + } + + public Builder maxExclusive(Object max) { + if (max != null) + map.put("maxExclusive", max); + else + map.remove("maxExclusive"); + return this; + } + + public Builder bound(Range range) { + if (range != null) { + if (range.hasLowerBound()) { + switch(range.lowerBoundType()) { + case CLOSED: + minInclusive(range.lowerEndpoint()); + break; + case OPEN: + minExclusive(range.lowerEndpoint()); + break; + default: + break; + } + } else { + minInclusive(null); + minExclusive(null); + } + if (range.hasUpperBound()) { + switch(range.upperBoundType()) { + case CLOSED: + maxInclusive(range.upperEndpoint()); + break; + case OPEN: + maxExclusive(range.upperEndpoint()); + break; + default: + break; + } + } else { + maxInclusive(null); + maxExclusive(null); + } + } + return this; + } + + public Builder step(Number step) { + if (step != null) + map.put("step", step); + else + map.remove("step"); + return this; + } + + public Builder enumeration(Object... vals) { + if (vals != null && vals.length > 0) + map.put("enumeration", ImmutableList.copyOf(vals)); + else + map.remove("enumeration"); + return this; + } + + public Builder maxLength(int length) { + if (length > -1) + map.put("maxLength", length); + else + map.remove("maxLength"); + return this; + } + + public Builder minLength(int length) { + if (length > -1) + map.put("minLength", length); + else + map.remove("minLength"); + return this; + } + + public Builder totalDigits(int num) { + if (num > -1) + map.put("totalDigits", num); + else + map.remove("totalDigits"); + return this; + } + + public Builder fractionDigits(int num) { + if (num > -1) + map.put("fractionDigits", num); + else + map.remove("fractionDigits"); + return this; + } + public Parameter get() { return new Parameter(this); } - + } - /** - */ - public static abstract class AbstractBuilder

> - extends ASObject.AbstractBuilder { - - protected AbstractBuilder() { - objectType("parameter"); - } - - /** - * Method placeholder. - * @param val String - * @return B - */ - public B placeholder(String val) { - return this._nlv("placeholder", val); - } - - /** - * Method placeholder. - * @param nlv NLV - * @return B - */ - public B placeholder(NLV nlv) { - return this._nlv("placeholder", nlv); - } - - /** - * Method placeholder. - * @param nlv Supplier - * @return B - */ - public B placeholder(Supplier nlv) { - return this._nlv("placeholder", nlv); - } - - /** - * Method placeholder. - * @param lang String - * @param val String - * @return B - */ - public B placeholder(String lang, String val) { - return this._nlv("placeholder", lang, val); - } - - /** - * Method type. - * @param iri String - * @return B - */ - public B type(String iri) { - return type(TypeValue.SimpleTypeValue.make(iri)); - } - - /** - * Method type. - * @param tv TypeValue - * @return B - */ - public B type(TypeValue tv) { - return set("type", tv); - } - - /** - * Method type. - * @param tv Supplier - * @return B - */ - public B type(Supplier tv) { - return type(tv.get()); - } - - /** - * Method required. - * @param on boolean - * @return B - */ - public B required(boolean on) { - return set("required", on); - } - - /** - * Method repeated. - * @param on boolean - * @return B - */ - public B repeated(boolean on) { - return set("repeated", on); - } - - /** - * Method required. - * @return B - */ - public B required() { - return required(true); - } - - /** - * Method repeated. - * @return B - */ - public B repeated() { - return repeated(true); - } - - /** - * Method value. - * @param value Object - * @return B - */ - public B value(Object value) { - return set("value", value); - } - - /** - * Method defaultValue. - * @param value Object - * @return B - */ - public B defaultValue(Object value) { - return set("default", value); - } - - /** - * Method format. - * @param format Format - * @return B - */ - @SuppressWarnings("unchecked") - public B format(Format format) { - if (format == Format.OTHER) - return (B)this; - return format(format.label); - } - - /** - * Method formatInt32. - * @return B - */ - public B formatInt32() { - return format(Format.INT32); - } - - /** - * Method formatInt64. - * @return B - */ - public B formatInt64() { - return format(Format.INT64); - } - - /** - * Method formatUint32. - * @return B - */ - public B formatUint32() { - return format(Format.UINT32); - } - - /** - * Method formatUint64. - * @return B - */ - public B formatUint64() { - return format(Format.UINT64); - } - - /** - * Method formatDouble. - * @return B - */ - public B formatDouble() { - return format(Format.DOUBLE); - } - - /** - * Method formatFloat. - * @return B - */ - public B formatFloat() { - return format(Format.FLOAT); - } - - /** - * Method formatByte. - * @return B - */ - public B formatByte() { - return format(Format.BYTE); - } - - /** - * Method formatDate. - * @return B - */ - public B formatDate() { - return format(Format.DATE); - } - - /** - * Method formatDateTime. - * @return B - */ - public B formatDateTime() { - return format(Format.DATETIME); - } - - /** - * Method formatDuration. - * @return B - */ - public B formatDuration() { - return format(Format.DURATION); - } - - /** - * Method formatLang. - * @return B - */ - public B formatLang() { - return format(Format.LANG); - } - - /** - * Method formatUri. - * @return B - */ - public B formatUri() { - return format(Format.URI); - } - - /** - * Method formatIri. - * @return B - */ - public B formatIri() { - return format(Format.IRI); - } - - /** - * Method format. - * @param format String - * @return B - */ - public B format(String format) { - return set("format", format); - } - - /** - * Method pattern. - * @param pattern Pattern - * @return B - */ - public B pattern(Pattern pattern) { - return pattern(pattern.pattern()); - } - - /** - * Method pattern. - * @param pattern String - * @return B - */ - public B pattern(String pattern) { - return set("pattern", pattern); - } - - /** - * Method minimum. - * @param min String - * @return B - */ - public B minimum(String min) { - return set("minimum", min); - } - - /** - * Method maximum. - * @param max String - * @return B - */ - public B maximum(String max) { - return set("maximum", max); - } - - /** - * Method minimum. - * @param min int - * @return B - */ - public B minimum(int min) { - return set("minimum", min); - } - - /** - * Method maximum. - * @param max int - * @return B - */ - public B maximum(int max) { - return set("maximum", max); - } - - /** - * Method minimum. - * @param min long - * @return B - */ - public B minimum(long min) { - return set("minimum", min); - } - - /** - * Method maximum. - * @param max long - * @return B - */ - public B maximum(long max) { - return set("maximum", max); - } - - /** - * Method minimum. - * @param min short - * @return B - */ - public B minimum(short min) { - return set("minimum", min); - } - - /** - * Method maximum. - * @param max short - * @return B - */ - public B maximum(short max) { - return set("maximum", max); - } - - /** - * Method minimum. - * @param min double - * @return B - */ - public B minimum(double min) { - return set("minimum", min); - } - - /** - * Method maximum. - * @param max double - * @return B - */ - public B maximum(double max) { - return set("maximum", max); - } - - /** - * Method minimum. - * @param min float - * @return B - */ - public B minimum(float min) { - return set("minimum", min); - } - - /** - * Method maximum. - * @param max float - * @return B - */ - public B maximum(float max) { - return set("maximum", max); - } - - /** - * Method step. - * @param step int - * @return B - */ - public B step(int step) { - return set("step", step); - } - - /** - * Method step. - * @param step long - * @return B - */ - public B step(long step) { - return set("step", step); - } - - /** - * Method step. - * @param step short - * @return B - */ - public B step(short step) { - return set("step", step); - } - - /** - * Method step. - * @param step double - * @return B - */ - public B step(double step) { - return set("step", step); - } - - /** - * Method step. - * @param step float - * @return B - */ - public B step(float step) { - return set("step", step); - } - - /** - * Method enumVals. - * @param vals Object[] - * @return B - */ - public B enumVals(Object... vals) { - return set("enum", ImmutableList.copyOf(vals)); - } - - } + private final ImmutableMap map; /** * Constructor for Parameter. @@ -544,6 +380,7 @@ public class Parameter */ protected Parameter(Builder builder) { super(builder); + this.map = ImmutableMap.copyOf(builder.map); } /** @@ -551,7 +388,8 @@ public class Parameter * @return boolean */ public boolean required() { - return getBoolean("required"); + return !has("required") ? + true : (Boolean)map.get("required"); } /** @@ -559,7 +397,8 @@ public class Parameter * @return boolean */ public boolean repeated() { - return getBoolean("repeated"); + return !has("repeated") ? + false : (Boolean)map.get("repeated"); } /** @@ -567,7 +406,7 @@ public class Parameter * @return O */ public O value() { - return this.get("value"); + return (O)map.get("value"); } /** @@ -576,7 +415,8 @@ public class Parameter * @return O */ public O value(O defaultValue) { - return this.get("value", defaultValue); + O val = value(); + return val != null ? val : defaultValue; } /** @@ -584,7 +424,7 @@ public class Parameter * @return O */ public O defaultValue() { - return this.get("default"); + return (O)map.get("default"); } /** @@ -593,235 +433,84 @@ public class Parameter * @return O */ public O defaultValue(O defaultValue) { - return this.get("default", defaultValue); + O val = defaultValue(); + return val != null ? val : defaultValue; } - /** - * Method formatString. - * @return String - */ - public String formatString() { - return getString("format"); - } - - /** - * Method format. - * @return Format - */ - public Format format() { - return Format.select(getString("format")); + public String type() { + return (String)map.get("type"); } /** * Method pattern. * @return String */ - public String pattern() { - return getString("pattern"); + public Iterable pattern() { + return (Iterable)map.get("pattern"); } - /** - * Method maximum. - * @return String - */ - public String maximum() { - return getString("maximum"); + public O maxInclusive() { + return (O)map.get("maxInclusive"); } - /** - * Method minimum. - * @return String - */ - public String minimum() { - return getString("minimum"); + public O maxExclusive() { + return (O)map.get("maxExclusive"); } - /** - * Method maximumInt. - * @return int - */ - public int maximumInt() { - return getInt("maximum"); + public O minInclusive() { + return (O)map.get("minInclusive"); } - /** - * Method minimumInt. - * @return int - */ - public int minimumInt() { - return getInt("minimum"); + public O minExclusive() { + return (O)map.get("minExclusive"); } - /** - * Method maximumLong. - * @return long - */ - public long maximumLong() { - return getLong("maximum"); + public boolean has(String key) { + return map.containsKey(key); } - /** - * Method minimumLong. - * @return long - */ - public long minimumLong() { - return getLong("minimum"); + public boolean hasUpperBound() { + return has("maxInclusive") || has("maxExclusive"); } - /** - * Method maximumShort. - * @return short - */ - public short maximumShort() { - return getShort("maximum"); + public >Range bounds() { + O mini = minInclusive(); + O mine = minExclusive(); + O maxi = maxInclusive(); + O maxe = maxExclusive(); + Ordering ordering = Ordering.natural(); + O min = ordering.nullsLast().min(mini,mine); + O max = ordering.nullsFirst().max(maxi,maxe); + BoundType lower = + min == null ? null : + min == mini ? BoundType.CLOSED : + BoundType.OPEN; + BoundType upper = + max == null ? null : + max == maxi ? BoundType.CLOSED : + BoundType.OPEN; + if (lower == null && upper == null) + return Range.all(); + else if (lower != null && upper == null) + return lower == BoundType.CLOSED ? + Range.atLeast(min) : + Range.greaterThan(min); + else if (lower == null && upper != null) + return upper == BoundType.CLOSED ? + Range.atMost(max) : + Range.lessThan(max); + else { + return Range.range(min, lower, max, upper); + } } - /** - * Method minimumShort. - * @return short - */ - public short minimumShort() { - return getShort("minimum"); + public N step() { + return (N)map.get("step"); } - /** - * Method maximumDouble. - * @return double - */ - public double maximumDouble() { - return getDouble("maximum"); - } - - /** - * Method minimumDouble. - * @return double - */ - public double minimumDouble() { - return getDouble("minimum"); - } - - /** - * Method maximumFloat. - * @return float - */ - public float maximumFloat() { - return getFloat("maximum"); - } - - /** - * Method minimumFloat. - * @return float - */ - public float minimumFloat() { - return getFloat("minimum"); - } - - /** - * Method maximum. - * @param defaultValue String - * @return String - */ - public String maximum(String defaultValue) { - return getString("maximum", defaultValue); - } - - /** - * Method minimum. - * @param defaultValue String - * @return String - */ - public String minimum(String defaultValue) { - return getString("minimum", defaultValue); - } - - /** - * Method maximumInt. - * @param defaultValue int - * @return int - */ - public int maximumInt(int defaultValue) { - return getInt("maximum", defaultValue); - } - - /** - * Method minimumInt. - * @param defaultValue int - * @return int - */ - public int minimumInt(int defaultValue) { - return getInt("minimum", defaultValue); - } - - /** - * Method maximumLong. - * @param defaultValue long - * @return long - */ - public long maximumLong(long defaultValue) { - return getLong("maximum", defaultValue); - } - - /** - * Method minimumLong. - * @param defaultValue long - * @return long - */ - public long minimumLong(long defaultValue) { - return getLong("minimum", defaultValue); - } - - /** - * Method maximumShort. - * @param defaultValue short - * @return short - */ - public short maximumShort(short defaultValue) { - return getShort("maximum", defaultValue); - } - - /** - * Method minimumShort. - * @param defaultValue short - * @return short - */ - public short minimumShort(short defaultValue) { - return getShort("minimum", defaultValue); - } - - /** - * Method maximumDouble. - * @param defaultValue double - * @return double - */ - public double maximumDouble(double defaultValue) { - return getDouble("maximum", defaultValue); - } - - /** - * Method minimumDouble. - * @param defaultValue double - * @return double - */ - public double minimumDouble(double defaultValue) { - return getDouble("minimum", defaultValue); - } - - /** - * Method maximumFloat. - * @param defaultValue float - * @return float - */ - public float maximumFloat(float defaultValue) { - return getFloat("maximum", defaultValue); - } - - /** - * Method minimumFloat. - * @param defaultValue float - * @return float - */ - public float minimumFloat(float defaultValue) { - return getFloat("minimum", defaultValue); + public N step(N defaultValue) { + N n = (N)map.get("step"); + return n != null ? n : defaultValue; } /** @@ -829,7 +518,7 @@ public class Parameter * @return int */ public int stepInt() { - return getInt("step"); + return step(); } /** @@ -838,7 +527,7 @@ public class Parameter * @return int */ public int stepInt(int defaultValue) { - return getInt("step", defaultValue); + return step(defaultValue); } /** @@ -846,7 +535,7 @@ public class Parameter * @return long */ public long stepLong() { - return getLong("step"); + return step(); } /** @@ -854,8 +543,8 @@ public class Parameter * @param defaultValue long * @return long */ - public long getLong(long defaultValue) { - return getLong("step", defaultValue); + public long stepLong(long defaultValue) { + return step(defaultValue); } /** @@ -863,7 +552,7 @@ public class Parameter * @return short */ public short stepShort() { - return getShort("step"); + return step(); } /** @@ -872,7 +561,7 @@ public class Parameter * @return short */ public short stepShort(short defaultValue) { - return getShort("step", defaultValue); + return step(defaultValue); } /** @@ -880,7 +569,7 @@ public class Parameter * @return double */ public double stepDouble() { - return getDouble("step"); + return step(); } /** @@ -889,7 +578,7 @@ public class Parameter * @return double */ public double stepDouble(double defaultValue) { - return getDouble("step", defaultValue); + return step(defaultValue); } /** @@ -897,7 +586,7 @@ public class Parameter * @return float */ public float stepFloat() { - return getFloat("step"); + return step(); } /** @@ -906,23 +595,18 @@ public class Parameter * @return float */ public float stepFloat(float defaultValue) { - return getFloat("step", defaultValue); + return step(defaultValue); } /** * Method enumVals. * @return Iterable */ - public Iterable enumVals() { - return this.>get("enum"); - } - - /** - * Method type. - * @return T - */ - public T type() { - return this.get("type"); + public Iterable enumeration() { + if (has("enumeration")) + return (Iterable)map.get("enumeration"); + else + return ImmutableSet.of(); } /** @@ -930,7 +614,7 @@ public class Parameter * @return NLV */ public NLV placeholder() { - return this.get("placeholder"); + return (NLV)map.get("placeholder"); } /** @@ -950,21 +634,111 @@ public class Parameter return _nlv("placeholder", lang); } + public int maxLength() { + Integer i = (Integer) map.get("maxLength"); + return i != null ? i : -1; + } + + public int minLength() { + Integer i = (Integer) map.get("minLength"); + return i != null ? i : -1; + } + + public int totalDigits() { + Integer i = (Integer) map.get("totalDigits"); + return i != null ? i : -1; + } + + public int fractionDigits() { + Integer i = (Integer) map.get("fractionDigits"); + return i != null ? i : -1; + } + + /** + * Method placeholder. + * @return NLV + */ + public NLV displayName() { + return (NLV)map.get("displayName"); + } + + /** + * Method placeholderString. + * @return String + */ + public String displayNameString() { + return _nlv("displayName"); + } + + /** + * Method placeholderString. + * @param lang String + * @return String + */ + public String displayNameString(String lang) { + return _nlv("displayName", lang); + } + + public String language() { + return (String)map.get("language"); + } + + /** + * Method _nlv. + * @param key String + * @return String + **/ + protected String _nlv(String key) { + String lang = language(); + return _nlv(key, lang != null ? lang : DEFAULT_LOCALE); + } + + /** + * Method _nlv. + * @param key String + * @param lang String + * @return String + **/ + protected String _nlv(String key, String lang) { + NLV nlv = + (NLV)map.get(key); + switch(nlv.valueType()) { + case SIMPLE: + NLV.SimpleNLV sim = + (SimpleNLV) nlv; + String l = language(); + return l == null || Objects.equal(l,lang) ? sim.value() : null; + case OBJECT: + NLV.MapNLV map = + (MapNLV) nlv; + return map.value(lang); + default: + return null; + } + } + + public ValueType valueType() { + return ValueType.OBJECT; + } + Object writeReplace() throws java.io.ObjectStreamException { return new SerializedForm(this); } private static class SerializedForm - extends AbstractSerializedForm { - private static final long serialVersionUID = -2060301713159936285L; - protected SerializedForm(Parameter obj) { - super(obj); + implements Serializable { + private static final long serialVersionUID = -1975376657749952999L; + private ImmutableMap map; + SerializedForm(Parameter obj) { + this.map = obj.map; } - Object readResolve() throws ObjectStreamException { - return super.doReadResolve(); - } - protected Parameter.Builder builder() { - return ActionMakers.parameter(); + + Object readResolve() + throws java.io.ObjectStreamException { + Parameter.Builder builder = new Parameter.Builder(); + builder.map.putAll(map); + return builder.get(); } + } } diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/ParameterAdapter.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/ParameterAdapter.java new file mode 100644 index 0000000..e75bd13 --- /dev/null +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/ParameterAdapter.java @@ -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 { + + 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 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 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.deserialize( + obj.get("displayName"), + NLV.class)); + if (obj.has("enumeration")) + builder.enumeration( + context.>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.>deserialize( + obj.get("pattern"), Iterable.class)); + if (obj.has("placeholder")) + builder.placeholder( + context.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(); + } + } + } + +} diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/ParameterValue.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/ParameterValue.java new file mode 100644 index 0000000..0fe5933 --- /dev/null +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/ParameterValue.java @@ -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 { + + 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(); + } + } + + } + +} diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersAdapter.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersAdapter.java new file mode 100644 index 0000000..f65726a --- /dev/null +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersAdapter.java @@ -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 { + + 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 entry : obj.entrySet()) + builder.param( + entry.getKey(), + context.deserialize( + entry.getValue(), + ParameterValue.class)); + return builder.get(); + } + +} diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersValue.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersValue.java index ec350d9..c11a701 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersValue.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/ParametersValue.java @@ -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 - implements Serializable { + extends AbstractWritable + implements Serializable, Iterable { - /** - * 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 - { + extends AbstractWritable.AbstractWritableBuilder + { - /** - * Method set. - * @param param String - * @param iri String + private final Map 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 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 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 iterator() { + return params.keySet().iterator(); + } + @SuppressWarnings("unchecked") - public T get(String param) { - return (T)super.getSingle(param); + public 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 value(String param, O defaultValue) { - TypeValue tv = get(param); - if (tv == null || tv.valueType() == ValueType.SIMPLE) - return defaultValue; - ASObject obj = (ASObject) tv; - return obj.get("value", defaultValue); - } - - /** - * Method value. - * @param param String - * @return O - */ - public 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 map; SerializedForm(ParametersValue obj) { - ImmutableMap.Builder 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 entry : map.entrySet()) - builder.set(entry.getKey(), (TypeValue) entry.getValue()); + builder.params.putAll(map); return builder.get(); } } diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java index 7d934f4..b31f4eb 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/StylesValue.java @@ -56,6 +56,10 @@ public final class StylesValue extends AbstractDictionaryObject.AbstractBuilder { + public Builder() { + writeUsing(ActionMakers.io); + } + /** * Method media. * @param query String diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/TypedPayload.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/TypedPayload.java index af7167f..d7dd26f 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/TypedPayload.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/TypedPayload.java @@ -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 { - private Builder() { + public Builder() { + writeUsing(ActionMakers.io); objectType("TypedPayload"); } diff --git a/actions/src/main/java/com/ibm/common/activitystreams/actions/UrlTemplate.java b/actions/src/main/java/com/ibm/common/activitystreams/actions/UrlTemplate.java index e1c6731..6221109 100755 --- a/actions/src/main/java/com/ibm/common/activitystreams/actions/UrlTemplate.java +++ b/actions/src/main/java/com/ibm/common/activitystreams/actions/UrlTemplate.java @@ -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 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 - * @return Builder - */ - public Builder parameter( - String name, - Supplier lv) { - return parameter(name, lv.get()); - } - + /** * Method get. * @return UrlTemplate diff --git a/actions/src/test/java/com/ibm/common/activitystreams/actions/test/ActionsTest.java b/actions/src/test/java/com/ibm/common/activitystreams/actions/test/ActionsTest.java new file mode 100644 index 0000000..97eeccb --- /dev/null +++ b/actions/src/test/java/com/ibm/common/activitystreams/actions/test/ActionsTest.java @@ -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 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 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 roundTrip(T writable) { + ByteArrayOutputStream out = + new ByteArrayOutputStream(); + writable.writeTo(out,io); + ByteArrayInputStream in = + new ByteArrayInputStream(out.toByteArray()); + return io.readAs(in); + } +} diff --git a/core/src/main/java/com/ibm/common/activitystreams/ASObject.java b/core/src/main/java/com/ibm/common/activitystreams/ASObject.java index 5dacbe5..005bf07 100755 --- a/core/src/main/java/com/ibm/common/activitystreams/ASObject.java +++ b/core/src/main/java/com/ibm/common/activitystreams/ASObject.java @@ -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. */ diff --git a/core/src/main/java/com/ibm/common/activitystreams/Writable.java b/core/src/main/java/com/ibm/common/activitystreams/Writable.java index cdad795..816f5bf 100755 --- a/core/src/main/java/com/ibm/common/activitystreams/Writable.java +++ b/core/src/main/java/com/ibm/common/activitystreams/Writable.java @@ -94,4 +94,5 @@ public interface Writable { * @return Future<?> */ Future writeTo(Writer out, IO io, ExecutorService executor); + } diff --git a/core/src/main/java/com/ibm/common/activitystreams/internal/GsonWrapper.java b/core/src/main/java/com/ibm/common/activitystreams/internal/GsonWrapper.java index 9c6db62..1f786bd 100755 --- a/core/src/main/java/com/ibm/common/activitystreams/internal/GsonWrapper.java +++ b/core/src/main/java/com/ibm/common/activitystreams/internal/GsonWrapper.java @@ -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; + } /** diff --git a/core/src/main/java/com/ibm/common/activitystreams/util/Util.java b/core/src/main/java/com/ibm/common/activitystreams/util/Util.java new file mode 100644 index 0000000..664b13a --- /dev/null +++ b/core/src/main/java/com/ibm/common/activitystreams/util/Util.java @@ -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()); + +}