gradle化とKotlin化
This commit is contained in:
parent
e488827d5a
commit
4b0eca63e1
|
|
@ -6,3 +6,5 @@ target
|
|||
*/.classpath
|
||||
*/.project
|
||||
*/.settings
|
||||
*/build/
|
||||
.gradle
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
# デフォルトの無視対象ファイル
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# エディターベースの HTTP クライアントリクエスト
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="11">
|
||||
<module name="activitystreams-actions" target="1.8" />
|
||||
<module name="activitystreams-core" target="1.8" />
|
||||
<module name="activitystreams-ext" target="1.8" />
|
||||
<module name="activitystreams-geo" target="1.8" />
|
||||
<module name="activitystreams-legacy" target="1.8" />
|
||||
<module name="activitystreams.buildSrc" target="22" />
|
||||
<module name="activitystreams.buildSrc.main" target="22" />
|
||||
<module name="activitystreams.buildSrc.test" target="22" />
|
||||
<module name="assembly" target="1.5" />
|
||||
</bytecodeTargetLevel>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/actions/src/main/kotlin" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/actions/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/assembly/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/assembly/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core/src/main/kotlin" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/geo/src/main/kotlin" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/geo/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/legacy/src/main/kotlin" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/legacy/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/typext/src/main/kotlin" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/typext/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/actions" />
|
||||
<option value="$PROJECT_DIR$/assembly" />
|
||||
<option value="$PROJECT_DIR$/core" />
|
||||
<option value="$PROJECT_DIR$/geo" />
|
||||
<option value="$PROJECT_DIR$/legacy" />
|
||||
<option value="$PROJECT_DIR$/typext" />
|
||||
</set>
|
||||
</option>
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="MavenLocal" />
|
||||
<option name="name" value="MavenLocal" />
|
||||
<option name="url" value="file:/$MAVEN_REPOSITORY$/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="maven" />
|
||||
<option name="name" value="maven" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="Gradle Central Plugin Repository" />
|
||||
<option name="name" value="Gradle Central Plugin Repository" />
|
||||
<option name="url" value="https://plugins.gradle.org/m2" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="KotlinJpsPluginSettings">
|
||||
<option name="version" value="2.0.21" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="ignoredFiles">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$/assembly/pom.xml" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK" />
|
||||
</project>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":core"))
|
||||
testImplementation(libs.junit.junit)
|
||||
implementation(kotlin("stdlib-jdk8"))
|
||||
}
|
||||
|
||||
description = "Activity Streams 2.0 - Actions"
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
kotlin {
|
||||
jvmToolchain(11)
|
||||
}
|
||||
tasks.test {
|
||||
useJUnit()
|
||||
}
|
||||
142
actions/pom.xml
142
actions/pom.xml
|
|
@ -1,142 +0,0 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.ibm.common</groupId>
|
||||
<artifactId>activitystreams</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>activitystreams-actions</artifactId>
|
||||
<name>Activity Streams 2.0 - Actions</name>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.9.1</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<docencoding>UTF-8</docencoding>
|
||||
<charset>UTF-8</charset>
|
||||
<additionalparam>-XDignore.symbol.file</additionalparam>
|
||||
<show>public</show>
|
||||
<links>
|
||||
<link>http://www.joda.org/joda-time/apidocs</link>
|
||||
<link>http://docs.guava-libraries.googlecode.com/git-history/v16.0.1/javadoc/</link>
|
||||
</links>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.3.1</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>2.3.7</version>
|
||||
<extensions>true</extensions>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>bundle-manifest</id>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>manifest</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Export-Package>com.ibm.common.activitystreams.actions.*</Export-Package>
|
||||
<Import-Package>
|
||||
com.ibm.common.activitystreams.*,
|
||||
com.google.gson.*,
|
||||
com.google.common.*,
|
||||
org.joda.time.*
|
||||
</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.2.2</version>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>assembly.xml</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.m2e</groupId>
|
||||
<artifactId>lifecycle-mapping</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<configuration>
|
||||
<lifecycleMappingMetadata>
|
||||
<pluginExecutions>
|
||||
<pluginExecution>
|
||||
<pluginExecutionFilter>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<versionRange>[2.3.7,)</versionRange>
|
||||
<goals>
|
||||
<goal>manifest</goal>
|
||||
</goals>
|
||||
</pluginExecutionFilter>
|
||||
<action>
|
||||
<ignore></ignore>
|
||||
</action>
|
||||
</pluginExecution>
|
||||
</pluginExecutions>
|
||||
</lifecycleMappingMetadata>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ibm.common</groupId>
|
||||
<artifactId>activitystreams-core</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -1,369 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.LinkValue;
|
||||
|
||||
/**
|
||||
*/
|
||||
public abstract class ActionHandler
|
||||
extends ASObject
|
||||
implements Serializable {
|
||||
|
||||
/**
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static abstract class Builder<A extends ActionHandler, B extends Builder<A,B>>
|
||||
extends ASObject.AbstractBuilder<A, B> {
|
||||
|
||||
private final Authentication.Builder auth =
|
||||
Authentication.make();
|
||||
|
||||
public Builder() {
|
||||
writeUsing(ActionMakers.io);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method confirm.
|
||||
* @return B
|
||||
*/
|
||||
public B confirm() {
|
||||
return confirm(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method confirm.
|
||||
* @param on boolean
|
||||
* @return B
|
||||
*/
|
||||
public B confirm(boolean on) {
|
||||
set("confirm", on);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method context.
|
||||
* @param obj ASObject
|
||||
* @return B
|
||||
*/
|
||||
public B context(ASObject obj) {
|
||||
set("context", obj);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method context.
|
||||
* @param obj Supplier<? extends ASObject>
|
||||
* @return B
|
||||
*/
|
||||
public B context(Supplier<? extends ASObject> obj) {
|
||||
return context(obj.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method requires.
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
public B requires(String iri, String... iris) {
|
||||
requires(LinkValue.SimpleLinkValue.make(iri));
|
||||
if (iris != null)
|
||||
for (String i : iris)
|
||||
requires(i);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method requires.
|
||||
* @param lv LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B requires(LinkValue lv, LinkValue... lvs) {
|
||||
link("requires", lv);
|
||||
if (lvs != null)
|
||||
for (LinkValue i : lvs)
|
||||
requires(i);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method requires.
|
||||
* @param lv Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B requires(Supplier<? extends LinkValue> lv) {
|
||||
return requires(lv.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prefers.
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
public B prefers(String iri, String... iris) {
|
||||
prefers(LinkValue.SimpleLinkValue.make(iri));
|
||||
if (iris != null)
|
||||
for (String i : iris)
|
||||
prefers(i);
|
||||
return (B) this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prefers.
|
||||
* @param lv LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B prefers(LinkValue lv, LinkValue... lvs) {
|
||||
link("prefers", lv);
|
||||
if (lvs != null)
|
||||
for (LinkValue l : lvs)
|
||||
prefers(l);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prefers.
|
||||
* @param lv Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B prefers(Supplier<? extends LinkValue> lv) {
|
||||
return prefers(lv.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method expects.
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
public B expects(String iri, String... iris) {
|
||||
expects(LinkValue.SimpleLinkValue.make(iri));
|
||||
if (iris != null)
|
||||
for (String i : iris)
|
||||
expects(i);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method expects.
|
||||
* @param tv LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B expects(LinkValue tv, LinkValue... lvs) {
|
||||
link("expects", tv);
|
||||
if (lvs != null)
|
||||
for (LinkValue lv : lvs)
|
||||
expects(lv);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method expects.
|
||||
* @param tv Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B expects(Supplier<? extends LinkValue> tv) {
|
||||
return expects(tv.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method returns.
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
public B returns(String iri, String... iris) {
|
||||
returns(LinkValue.SimpleLinkValue.make(iri));
|
||||
if (iris != null)
|
||||
for (String i : iris)
|
||||
returns(i);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method returns.
|
||||
* @param tv LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B returns(LinkValue tv, LinkValue... lvs) {
|
||||
link("returns", tv);
|
||||
if (lvs != null)
|
||||
for (LinkValue lv : lvs)
|
||||
returns(lv);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method returns.
|
||||
* @param tv Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B returns(Supplier<? extends LinkValue> tv) {
|
||||
return returns(tv.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method auth.
|
||||
* @param key String
|
||||
* @param obj ASObject
|
||||
* @return B
|
||||
*/
|
||||
public B auth(String key, ASObject obj) {
|
||||
auth.set(key, obj);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method auth.
|
||||
* @param key String
|
||||
* @param obj Supplier<? extends ASObject>
|
||||
* @return B
|
||||
*/
|
||||
public B auth(String key, Supplier<? extends ASObject> obj) {
|
||||
return auth(key, obj.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return A
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
public A get() {
|
||||
if (auth.notEmpty())
|
||||
set("auth", auth.get());
|
||||
return actualGet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method actualGet.
|
||||
* @return A
|
||||
*/
|
||||
protected abstract A actualGet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for ActionHandler.
|
||||
* @param builder Builder<?,?>
|
||||
*/
|
||||
protected ActionHandler(Builder<?,?> builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method confirm.
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean confirm() {
|
||||
return this.getBoolean("confirm");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method context.
|
||||
* @return A
|
||||
*/
|
||||
public <A extends ASObject>A context() {
|
||||
return this.<A>get("context");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method expects.
|
||||
* @return Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> expects() {
|
||||
return this.links("expects");
|
||||
}
|
||||
|
||||
public Iterable<LinkValue> expects(Predicate<? super LinkValue> filter) {
|
||||
return this.links("expects", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method requires.
|
||||
* @return Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> requires() {
|
||||
return this.links("requires");
|
||||
}
|
||||
|
||||
public Iterable<LinkValue> requires(Predicate<? super LinkValue> filter) {
|
||||
return this.links("requires", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prefers.
|
||||
* @return Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> prefers() {
|
||||
return this.links("prefers");
|
||||
}
|
||||
|
||||
public Iterable<LinkValue> prefers(Predicate<? super LinkValue> filter) {
|
||||
return this.links("prefers", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method returns.
|
||||
* @return Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> returns() {
|
||||
return this.links("returns");
|
||||
}
|
||||
|
||||
public Iterable<LinkValue> returns(Predicate<? super LinkValue> filter) {
|
||||
return this.links("returns", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method auth.
|
||||
* @return Authentication
|
||||
*/
|
||||
public Authentication auth() {
|
||||
return this.<Authentication>get("auth");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method hasAuth.
|
||||
* @param key String
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean hasAuth(String key) {
|
||||
Authentication auth = auth();
|
||||
return auth != null ?
|
||||
auth.has(key) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method auth.
|
||||
* @param key String
|
||||
* @return A
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <A extends ASObject>A auth(String key) {
|
||||
Authentication auth = auth();
|
||||
return auth != null ?
|
||||
(A)auth.get(key) : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,233 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
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";
|
||||
public static final String TARGET_CURRENT = "CURRENT";
|
||||
public static final String TARGET_TAB = "TAB";
|
||||
public static final String TARGET_DIALOG = "DIALOG";
|
||||
public static final String TARGET_MODALDIALOG = "MODALDIALOG";
|
||||
public static final String TARGET_FLOAT = "FLOAT";
|
||||
public static final String TARGET_SIDEBAR = "SIDEBAR";
|
||||
|
||||
public static final String METHOD_GET = "GET";
|
||||
public static final String METHOD_POST = "POST";
|
||||
public static final String METHOD_PUT = "PUT";
|
||||
public static final String METHOD_DELETE = "DELETE";
|
||||
public static final String METHOD_PATCH = "PATCH";
|
||||
|
||||
private ActionMakers() {}
|
||||
|
||||
/**
|
||||
* Method application.
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
public static ASObject.Builder application() {
|
||||
return object("application");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method service.
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
public static ASObject.Builder service() {
|
||||
return object("service");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @return StylesValue.Builder
|
||||
*/
|
||||
public static StylesValue.Builder styles() {
|
||||
return StylesValue.make();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @param media String
|
||||
* @return StylesValue.Builder
|
||||
*/
|
||||
public static StylesValue.Builder styles(String media) {
|
||||
return styles().media(media);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method httpAction.
|
||||
* @return HttpActionHandler.Builder
|
||||
*/
|
||||
public static HttpActionHandler.Builder httpAction() {
|
||||
return HttpActionHandler.makeHttpActionHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method httpAction.
|
||||
* @param url String
|
||||
* @return HttpActionHandler.Builder
|
||||
*/
|
||||
public static HttpActionHandler.Builder httpAction(String url) {
|
||||
return httpAction().url(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method httpAction.
|
||||
* @param url String
|
||||
* @param method String
|
||||
* @return HttpActionHandler.Builder
|
||||
*/
|
||||
public static HttpActionHandler.Builder httpAction(
|
||||
String url,
|
||||
String method) {
|
||||
return httpAction()
|
||||
.url(url)
|
||||
.method(method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method embedAction.
|
||||
* @return EmbedActionHandler.Builder
|
||||
*/
|
||||
public static EmbedActionHandler.Builder embedAction() {
|
||||
return EmbedActionHandler.makeEmbedActionHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method embedAction.
|
||||
* @param url String
|
||||
* @return EmbedActionHandler.Builder
|
||||
*/
|
||||
public static EmbedActionHandler.Builder embedAction(String url) {
|
||||
return embedAction().url(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method embedAction.
|
||||
* @param url String
|
||||
* @param mediaType String
|
||||
* @return EmbedActionHandler.Builder
|
||||
*/
|
||||
public static EmbedActionHandler.Builder embedAction(
|
||||
String url,
|
||||
String mediaType) {
|
||||
return embedAction(url).mediaType(mediaType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method staticEmbedAction.
|
||||
* @param mediaType String
|
||||
* @param content String
|
||||
* @return EmbedActionHandler.Builder
|
||||
*/
|
||||
public static EmbedActionHandler.Builder staticEmbedAction(
|
||||
String mediaType,
|
||||
String content) {
|
||||
return embedAction().mediaType(mediaType).content(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method intentAction.
|
||||
* @return IntentActionHandler.Builder
|
||||
*/
|
||||
public static IntentActionHandler.Builder intentAction() {
|
||||
return IntentActionHandler.makeIntentActionHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method intentAction.
|
||||
* @param url String
|
||||
* @return IntentActionHandler.Builder
|
||||
*/
|
||||
public static IntentActionHandler.Builder intentAction(String url) {
|
||||
return intentAction().url(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method htmlForm.
|
||||
* @return HtmlForm.Builder
|
||||
*/
|
||||
public static HtmlForm.Builder htmlForm() {
|
||||
return HtmlForm.makeHtmlForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method typedPayload.
|
||||
* @param mediaType String
|
||||
* @return TypedPayload.Builder
|
||||
*/
|
||||
public static TypedPayload.Builder typedPayload(String mediaType) {
|
||||
return TypedPayload.makeTypedPayload(mediaType);
|
||||
}
|
||||
|
||||
public static TypedPayload.Builder typedPayload() {
|
||||
return TypedPayload.make();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method urlTemplate.
|
||||
* @return UrlTemplate.Builder
|
||||
*/
|
||||
public static UrlTemplate.Builder urlTemplate() {
|
||||
return UrlTemplate.makeUrlTemplate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method urlTemplate.
|
||||
* @param template String
|
||||
* @return UrlTemplate.Builder
|
||||
*/
|
||||
public static UrlTemplate.Builder urlTemplate(String template) {
|
||||
return urlTemplate().template(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parameter.
|
||||
* @return Parameter.Builder
|
||||
*/
|
||||
public static Parameter.Builder parameter() {
|
||||
return Parameter.makeParameter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parameter.
|
||||
* @param id String
|
||||
* @return Parameter.Builder
|
||||
*/
|
||||
public static SimpleParameterValue parameter(String id) {
|
||||
return new SimpleParameterValue.Builder().type(id).get();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,106 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.ibm.common.activitystreams.ASObject.AbstractBuilder;
|
||||
import com.ibm.common.activitystreams.internal.ASObjectAdapter;
|
||||
import com.ibm.common.activitystreams.internal.Model;
|
||||
import com.ibm.common.activitystreams.internal.Schema;
|
||||
|
||||
public final class ActionObjectAdapter
|
||||
extends ASObjectAdapter {
|
||||
|
||||
ActionObjectAdapter(Schema schema) {
|
||||
super(schema);
|
||||
}
|
||||
|
||||
private static final ImmutableSet<Class<?>> knownTypes =
|
||||
ImmutableSet.<Class<?>>of(
|
||||
HttpActionHandler.class,
|
||||
IntentActionHandler.class,
|
||||
EmbedActionHandler.class,
|
||||
HtmlForm.class,
|
||||
UrlTemplate.class,
|
||||
TypedPayload.class
|
||||
);
|
||||
|
||||
@Override
|
||||
protected boolean knowsType(Type type) {
|
||||
if (super.knowsType(type))
|
||||
return true;
|
||||
return knownTypes.contains(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractBuilder<?, ?> builderFor(Type type) {
|
||||
if (super.knowsType(type))
|
||||
return super.builderFor(type);
|
||||
if (type == HttpActionHandler.class)
|
||||
return ActionMakers.httpAction();
|
||||
else if (type == IntentActionHandler.class)
|
||||
return ActionMakers.intentAction();
|
||||
else if (type == EmbedActionHandler.class)
|
||||
return ActionMakers.embedAction();
|
||||
else if (type == HtmlForm.class)
|
||||
return ActionMakers.htmlForm();
|
||||
else if (type == UrlTemplate.class)
|
||||
return ActionMakers.urlTemplate();
|
||||
else if (type == TypedPayload.class)
|
||||
return ActionMakers.typedPayload();
|
||||
else return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Model modelFor(Type type) {
|
||||
if (super.knowsType(type))
|
||||
return super.modelFor(type);
|
||||
if (type == HttpActionHandler.class)
|
||||
return schema().forObjectClassOrType(
|
||||
HttpActionHandler.Builder.class,
|
||||
"HttpActionHandler");
|
||||
else if (type == IntentActionHandler.class)
|
||||
return schema().forObjectClassOrType(
|
||||
IntentActionHandler.Builder.class,
|
||||
"IntentActionHandler");
|
||||
else if (type == EmbedActionHandler.class)
|
||||
return schema().forObjectClassOrType(
|
||||
EmbedActionHandler.Builder.class,
|
||||
"EmbedActionHandler");
|
||||
else if (type == HtmlForm.class)
|
||||
return schema().forObjectClassOrType(
|
||||
HtmlForm.Builder.class,
|
||||
"HtmlForm");
|
||||
else if (type == UrlTemplate.class)
|
||||
return schema().forObjectClassOrType(
|
||||
UrlTemplate.Builder.class,
|
||||
"UrlTemplate");
|
||||
else if (type == TypedPayload.class)
|
||||
return schema().forObjectClassOrType(
|
||||
TypedPayload.Builder.class,
|
||||
"TypedPayload");
|
||||
else return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import static com.ibm.common.activitystreams.actions.Adapters.AUTH;
|
||||
import static com.ibm.common.activitystreams.actions.Adapters.STYLES;
|
||||
|
||||
import com.ibm.common.activitystreams.IO.Builder;
|
||||
import com.ibm.common.activitystreams.internal.Model;
|
||||
import com.ibm.common.activitystreams.internal.Schema;
|
||||
import com.ibm.common.activitystreams.util.Module;
|
||||
|
||||
public final class ActionsModule
|
||||
implements Module {
|
||||
|
||||
public static final Module instance = new ActionsModule();
|
||||
|
||||
@Override
|
||||
public void apply(Builder builder, Schema schema) {
|
||||
|
||||
ActionObjectAdapter base =
|
||||
new ActionObjectAdapter(schema);
|
||||
|
||||
// Register Adapters
|
||||
builder.hierarchicalAdapter(ActionHandler.class,base)
|
||||
.hierarchicalAdapter(HttpActionHandler.class,base)
|
||||
.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(Authentication.class, AUTH)
|
||||
.hierarchicalAdapter(StylesValue.class, STYLES);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(Schema.Builder builder) {
|
||||
builder.map("HtmlForm", withParameters.template(HtmlForm.class, HtmlForm.Builder.class))
|
||||
.map("TypedPayload", typedPayload)
|
||||
.map("UrlTemplate", withParameters.template(UrlTemplate.class, UrlTemplate.Builder.class))
|
||||
.map("HttpActionHandler", actionHandler.template(HttpActionHandler.class, HttpActionHandler.Builder.class))
|
||||
.map("IntentActionHandler", actionHandler.template(IntentActionHandler.class, IntentActionHandler.Builder.class))
|
||||
.map("EmbedActionHandler", actionHandler.template(EmbedActionHandler.class, EmbedActionHandler.Builder.class));
|
||||
}
|
||||
|
||||
public final static Model actionHandler =
|
||||
Model
|
||||
.make("object")
|
||||
.linkValue(
|
||||
"expects",
|
||||
"returns",
|
||||
"requires",
|
||||
"prefers")
|
||||
.object("context")
|
||||
.as("auth", Authentication.class)
|
||||
.as("style", StylesValue.class)
|
||||
.get();
|
||||
|
||||
|
||||
public final static Model withParameters =
|
||||
Model
|
||||
.make("object")
|
||||
.as("parameters", ParametersValue.class)
|
||||
.get();
|
||||
|
||||
public final static Model typedPayload =
|
||||
Model
|
||||
.make("object")
|
||||
.type(TypedPayload.class, TypedPayload.Builder.class)
|
||||
.linkValue("schema")
|
||||
.typeValue("type")
|
||||
.get();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,108 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObject;
|
||||
|
||||
/**
|
||||
*/
|
||||
public final class Authentication
|
||||
extends AbstractDictionaryObject<ASObject> {
|
||||
|
||||
/**
|
||||
* Method make.
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder make() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static final class Builder
|
||||
extends AbstractDictionaryObject.AbstractBuilder<
|
||||
ASObject,Authentication,Builder> {
|
||||
|
||||
public Builder() {
|
||||
writeUsing(ActionMakers.io);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return Authentication
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
public Authentication get() {
|
||||
return new Authentication(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for Authentication.
|
||||
* @param builder Builder
|
||||
*/
|
||||
protected Authentication(Builder builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @param key String
|
||||
* @return A
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <A extends ASObject>A get(String key) {
|
||||
return (A)this.getSingle(key);
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private ImmutableMap<String,Object> map;
|
||||
SerializedForm(Authentication obj) {
|
||||
ImmutableMap.Builder<String,Object> builder =
|
||||
ImmutableMap.builder();
|
||||
for (String key : obj)
|
||||
builder.put(key, obj.get(key));
|
||||
this.map = builder.build();
|
||||
}
|
||||
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
Authentication.Builder builder =
|
||||
Authentication.make();
|
||||
for (Map.Entry<String,Object> entry : map.entrySet())
|
||||
builder.set(entry.getKey(), (ASObject) entry.getValue());
|
||||
return builder.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,234 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.collect.Iterables.filter;
|
||||
import static com.google.common.collect.Iterables.getFirst;
|
||||
import static com.google.common.collect.Iterables.size;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class EmbedActionHandler
|
||||
extends ActionHandler
|
||||
implements Serializable {
|
||||
|
||||
/**
|
||||
* Method makeEmbedActionHandler.
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder makeEmbedActionHandler() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static final class Builder
|
||||
extends AbstractBuilder<EmbedActionHandler, Builder> {
|
||||
|
||||
public Builder() {
|
||||
super("EmbedActionHandler");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getActual.
|
||||
* @return EmbedActionHandler
|
||||
*/
|
||||
protected EmbedActionHandler getActual() {
|
||||
return new EmbedActionHandler(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static abstract class AbstractBuilder<A extends EmbedActionHandler, B extends AbstractBuilder<A,B>>
|
||||
extends ActionHandler.Builder<A,B> {
|
||||
|
||||
private boolean styleset = false;
|
||||
private static ImmutableList.Builder<StylesValue> styles =
|
||||
ImmutableList.builder();
|
||||
|
||||
/**
|
||||
* Constructor for AbstractBuilder.
|
||||
* @param objectType String
|
||||
*/
|
||||
protected AbstractBuilder(String objectType) {
|
||||
objectType(objectType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method target.
|
||||
* @param target String
|
||||
* @return B
|
||||
*/
|
||||
public B target(String target) {
|
||||
set("target", target);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method style.
|
||||
* @param style StylesValue
|
||||
* @return B
|
||||
*/
|
||||
public B style(StylesValue style) {
|
||||
styleset = true;
|
||||
styles.add(style);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method style.
|
||||
* @param style Supplier<StylesValue>
|
||||
* @return B
|
||||
*/
|
||||
public B style(Supplier<StylesValue> style) {
|
||||
return style(style.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method style.
|
||||
* @param styles StylesValue[]
|
||||
* @return B
|
||||
*/
|
||||
public B style(StylesValue... styles) {
|
||||
for (StylesValue s : styles)
|
||||
style(s);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method style.
|
||||
* @param styles Supplier<StylesValue>[]
|
||||
* @return B
|
||||
*/
|
||||
public B style(Supplier<StylesValue>... styles) {
|
||||
for (Supplier<StylesValue> s : styles)
|
||||
style(s);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method actualGet.
|
||||
* @return A
|
||||
*/
|
||||
protected final A actualGet() {
|
||||
if (styleset) {
|
||||
ImmutableList<StylesValue> val = styles.build();
|
||||
if (size(val) == 1) {
|
||||
set("style", getFirst(val,null));
|
||||
} else {
|
||||
set("style", val);
|
||||
}
|
||||
}
|
||||
return getActual();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getActual.
|
||||
* @return A
|
||||
*/
|
||||
protected abstract A getActual();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for EmbedActionHandler.
|
||||
* @param builder AbstractBuilder<?,?>
|
||||
*/
|
||||
protected EmbedActionHandler(AbstractBuilder<?,?> builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @return Iterable<StylesValue>
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Iterable<StylesValue> styles() {
|
||||
Object styles = get("style");
|
||||
if (styles instanceof StylesValue)
|
||||
return ImmutableList.<StylesValue>of((StylesValue)styles);
|
||||
else if (styles instanceof Iterable)
|
||||
return (Iterable<StylesValue>)styles;
|
||||
else return ImmutableList.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @param media String
|
||||
* @return Iterable<StylesValue>
|
||||
*/
|
||||
public Iterable<StylesValue> styles(final String media) {
|
||||
checkNotNull(media);
|
||||
return styles(new Predicate<StylesValue>() {
|
||||
public boolean apply(StylesValue style) {
|
||||
return media.equalsIgnoreCase(style.media());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @param matcher Predicate<StylesValue>
|
||||
* @return Iterable<StylesValue>
|
||||
*/
|
||||
public Iterable<StylesValue> styles(Predicate<StylesValue> matcher) {
|
||||
return filter(styles(), matcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method target.
|
||||
* @return String
|
||||
*/
|
||||
public String target() {
|
||||
return getString(
|
||||
"target",
|
||||
ActionMakers.TARGET_DEFAULT);
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
extends AbstractSerializedForm<EmbedActionHandler> {
|
||||
private static final long serialVersionUID = -2060301713159936285L;
|
||||
protected SerializedForm(EmbedActionHandler obj) {
|
||||
super(obj);
|
||||
}
|
||||
Object readResolve() throws ObjectStreamException {
|
||||
return super.doReadResolve();
|
||||
}
|
||||
protected EmbedActionHandler.Builder builder() {
|
||||
return ActionMakers.embedAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,120 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
|
||||
/**
|
||||
*/
|
||||
public final class HtmlForm
|
||||
extends ASObject {
|
||||
|
||||
/**
|
||||
* Method makeHtmlForm.
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder makeHtmlForm() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static final class Builder
|
||||
extends ASObject.AbstractBuilder<HtmlForm, Builder> {
|
||||
|
||||
private final ParametersValue.Builder params =
|
||||
ParametersValue.make();
|
||||
|
||||
public Builder() {
|
||||
writeUsing(ActionMakers.io);
|
||||
objectType("HtmlForm");
|
||||
mediaType("application/x-www-form-urlencoded");
|
||||
}
|
||||
|
||||
public Builder parameter(String name, String iri) {
|
||||
params.param(name, iri);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder parameter(
|
||||
String name,
|
||||
ParameterValue parameter) {
|
||||
params.param(name, parameter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder parameter(
|
||||
String name,
|
||||
Supplier<? extends ParameterValue> parameter) {
|
||||
return parameter(name, parameter.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return HtmlForm
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
public HtmlForm get() {
|
||||
if (params.notEmpty())
|
||||
set("parameters", params.get());
|
||||
return new HtmlForm(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for HtmlForm.
|
||||
* @param builder Builder
|
||||
*/
|
||||
private HtmlForm(Builder builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parameters.
|
||||
* @return ParametersValue
|
||||
*/
|
||||
public ParametersValue parameters() {
|
||||
return this.<ParametersValue>get("parameters");
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
extends AbstractSerializedForm<HtmlForm> {
|
||||
private static final long serialVersionUID = -2060301713159936285L;
|
||||
protected SerializedForm(HtmlForm obj) {
|
||||
super(obj);
|
||||
}
|
||||
Object readResolve() throws ObjectStreamException {
|
||||
return super.doReadResolve();
|
||||
}
|
||||
protected HtmlForm.Builder builder() {
|
||||
return ActionMakers.htmlForm();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,137 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
public class HttpActionHandler
|
||||
extends ActionHandler {
|
||||
|
||||
/**
|
||||
* Method makeHttpActionHandler.
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder makeHttpActionHandler() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static final class Builder
|
||||
extends AbstractBuilder<HttpActionHandler, Builder> {
|
||||
|
||||
public Builder() {
|
||||
super("HttpActionHandler");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method actualGet.
|
||||
* @return HttpActionHandler
|
||||
*/
|
||||
protected HttpActionHandler actualGet() {
|
||||
return new HttpActionHandler(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static abstract class AbstractBuilder
|
||||
<A extends HttpActionHandler, B extends AbstractBuilder<A,B>>
|
||||
extends ActionHandler.Builder<A,B> {
|
||||
/**
|
||||
* Constructor for AbstractBuilder.
|
||||
* @param objectType String
|
||||
*/
|
||||
protected AbstractBuilder(String objectType) {
|
||||
objectType(objectType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method method.
|
||||
* @param method String
|
||||
* @return B
|
||||
*/
|
||||
public B method(String method) {
|
||||
set("method", method);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method target.
|
||||
* @param target String
|
||||
* @return B
|
||||
*/
|
||||
public B target(String target) {
|
||||
set("target", target);
|
||||
return (B)this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for HttpActionHandler.
|
||||
* @param builder AbstractBuilder<?,?>
|
||||
*/
|
||||
protected HttpActionHandler(AbstractBuilder<?,?> builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method method.
|
||||
* @return String
|
||||
*/
|
||||
public String method() {
|
||||
return this.getString("method");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method target.
|
||||
* @return String
|
||||
*/
|
||||
public String target() {
|
||||
return has("target") ?
|
||||
this.<String>get("target") :
|
||||
ActionMakers.TARGET_DEFAULT;
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
extends AbstractSerializedForm<HttpActionHandler> {
|
||||
private static final long serialVersionUID = -2060301713159936285L;
|
||||
protected SerializedForm(HttpActionHandler obj) {
|
||||
super(obj);
|
||||
}
|
||||
Object readResolve() throws ObjectStreamException {
|
||||
return super.doReadResolve();
|
||||
}
|
||||
protected HttpActionHandler.Builder builder() {
|
||||
return ActionMakers.httpAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
public class IntentActionHandler
|
||||
extends ActionHandler {
|
||||
|
||||
/**
|
||||
* Method makeIntentActionHandler.
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder makeIntentActionHandler() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static final class Builder
|
||||
extends AbstractBuilder<IntentActionHandler, Builder> {
|
||||
|
||||
public Builder() {
|
||||
super("IntentActionHandler");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method actualGet.
|
||||
* @return IntentActionHandler
|
||||
*/
|
||||
protected IntentActionHandler actualGet() {
|
||||
return new IntentActionHandler(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static abstract class AbstractBuilder
|
||||
<A extends IntentActionHandler, B extends AbstractBuilder<A,B>>
|
||||
extends ActionHandler.Builder<A,B> {
|
||||
/**
|
||||
* Constructor for AbstractBuilder.
|
||||
* @param objectType String
|
||||
*/
|
||||
protected AbstractBuilder(String objectType) {
|
||||
objectType(objectType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for IntentActionHandler.
|
||||
* @param builder AbstractBuilder<?,?>
|
||||
*/
|
||||
protected IntentActionHandler(AbstractBuilder<?,?> builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
extends AbstractSerializedForm<IntentActionHandler> {
|
||||
private static final long serialVersionUID = -2060301713159936285L;
|
||||
protected SerializedForm(IntentActionHandler obj) {
|
||||
super(obj);
|
||||
}
|
||||
Object readResolve() throws ObjectStreamException {
|
||||
return super.doReadResolve();
|
||||
}
|
||||
protected IntentActionHandler.Builder builder() {
|
||||
return ActionMakers.intentAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,744 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
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.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.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 AbstractWritable
|
||||
implements ParameterValue {
|
||||
|
||||
/**
|
||||
* Method makeParameter.
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder makeParameter() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder
|
||||
extends AbstractWritable.AbstractWritableBuilder<Parameter,Builder> {
|
||||
|
||||
protected Builder() {
|
||||
writeUsing(ActionMakers.io);
|
||||
}
|
||||
|
||||
private final Map<String,Object> 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<? extends NLV> nlv) {
|
||||
return _nlv(key,nlv.get());
|
||||
}
|
||||
|
||||
protected Builder _nlv(String key, Map<String,String> map) {
|
||||
if (map != null)
|
||||
for (Map.Entry<String,String> 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<? extends NLV> 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<? extends NLV> 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<String> 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<String> patterns =
|
||||
ImmutableSet.copyOf(pattern);
|
||||
map.put("pattern",patterns);
|
||||
} else {
|
||||
map.remove("pattern");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder pattern(Iterable<String> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final ImmutableMap<String,Object> map;
|
||||
|
||||
/**
|
||||
* Constructor for Parameter.
|
||||
* @param builder Builder
|
||||
*/
|
||||
protected Parameter(Builder builder) {
|
||||
super(builder);
|
||||
this.map = ImmutableMap.copyOf(builder.map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method required.
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean required() {
|
||||
return !has("required") ?
|
||||
true : (Boolean)map.get("required");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method repeated.
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean repeated() {
|
||||
return !has("repeated") ?
|
||||
false : (Boolean)map.get("repeated");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method value.
|
||||
* @return O
|
||||
*/
|
||||
public <O>O value() {
|
||||
return (O)map.get("value");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method value.
|
||||
* @param defaultValue O
|
||||
* @return O
|
||||
*/
|
||||
public <O>O value(O defaultValue) {
|
||||
O val = value();
|
||||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method defaultValue.
|
||||
* @return O
|
||||
*/
|
||||
public <O>O defaultValue() {
|
||||
return (O)map.get("default");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method defaultValue.
|
||||
* @param defaultValue O
|
||||
* @return O
|
||||
*/
|
||||
public <O>O defaultValue(O defaultValue) {
|
||||
O val = defaultValue();
|
||||
return val != null ? val : defaultValue;
|
||||
}
|
||||
|
||||
public String type() {
|
||||
return (String)map.get("type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method pattern.
|
||||
* @return String
|
||||
*/
|
||||
public Iterable<String> pattern() {
|
||||
return (Iterable<String>)map.get("pattern");
|
||||
}
|
||||
|
||||
public <O>O maxInclusive() {
|
||||
return (O)map.get("maxInclusive");
|
||||
}
|
||||
|
||||
public <O>O maxExclusive() {
|
||||
return (O)map.get("maxExclusive");
|
||||
}
|
||||
|
||||
public <O>O minInclusive() {
|
||||
return (O)map.get("minInclusive");
|
||||
}
|
||||
|
||||
public <O>O minExclusive() {
|
||||
return (O)map.get("minExclusive");
|
||||
}
|
||||
|
||||
public boolean has(String key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean hasUpperBound() {
|
||||
return has("maxInclusive") || has("maxExclusive");
|
||||
}
|
||||
|
||||
public <O extends Comparable<? super O>>Range<O> bounds() {
|
||||
O mini = minInclusive();
|
||||
O mine = minExclusive();
|
||||
O maxi = maxInclusive();
|
||||
O maxe = maxExclusive();
|
||||
Ordering<O> ordering = Ordering.<O>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.<O>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);
|
||||
}
|
||||
}
|
||||
|
||||
public <N extends Number>N step() {
|
||||
return (N)map.get("step");
|
||||
}
|
||||
|
||||
public <N extends Number>N step(N defaultValue) {
|
||||
N n = (N)map.get("step");
|
||||
return n != null ? n : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepInt.
|
||||
* @return int
|
||||
*/
|
||||
public int stepInt() {
|
||||
return step();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepInt.
|
||||
* @param defaultValue int
|
||||
* @return int
|
||||
*/
|
||||
public int stepInt(int defaultValue) {
|
||||
return step(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepLong.
|
||||
* @return long
|
||||
*/
|
||||
public long stepLong() {
|
||||
return step();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getLong.
|
||||
* @param defaultValue long
|
||||
* @return long
|
||||
*/
|
||||
public long stepLong(long defaultValue) {
|
||||
return step(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepShort.
|
||||
* @return short
|
||||
*/
|
||||
public short stepShort() {
|
||||
return step();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepShort.
|
||||
* @param defaultValue short
|
||||
* @return short
|
||||
*/
|
||||
public short stepShort(short defaultValue) {
|
||||
return step(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepDouble.
|
||||
* @return double
|
||||
*/
|
||||
public double stepDouble() {
|
||||
return step();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepDouble.
|
||||
* @param defaultValue double
|
||||
* @return double
|
||||
*/
|
||||
public double stepDouble(double defaultValue) {
|
||||
return step(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepFloat.
|
||||
* @return float
|
||||
*/
|
||||
public float stepFloat() {
|
||||
return step();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepFloat.
|
||||
* @param defaultValue float
|
||||
* @return float
|
||||
*/
|
||||
public float stepFloat(float defaultValue) {
|
||||
return step(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method enumVals.
|
||||
* @return Iterable<Object>
|
||||
*/
|
||||
public Iterable<Object> enumeration() {
|
||||
if (has("enumeration"))
|
||||
return (Iterable<Object>)map.get("enumeration");
|
||||
else
|
||||
return ImmutableSet.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholder.
|
||||
* @return NLV
|
||||
*/
|
||||
public NLV placeholder() {
|
||||
return (NLV)map.get("placeholder");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholderString.
|
||||
* @return String
|
||||
*/
|
||||
public String placeholderString() {
|
||||
return _nlv("placeholder");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholderString.
|
||||
* @param lang String
|
||||
* @return String
|
||||
*/
|
||||
public String placeholderString(String lang) {
|
||||
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
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private ImmutableMap<String,Object> map;
|
||||
SerializedForm(Parameter obj) {
|
||||
this.map = obj.map;
|
||||
}
|
||||
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
Parameter.Builder builder = new Parameter.Builder();
|
||||
builder.map.putAll(map);
|
||||
return builder.get();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,260 +0,0 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,99 +0,0 @@
|
|||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
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;
|
||||
|
||||
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.equal(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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,134 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
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.google.common.collect.Maps;
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable;
|
||||
|
||||
/**
|
||||
* The value of the "parameters" property...
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public final class ParametersValue
|
||||
extends AbstractWritable
|
||||
implements Serializable, Iterable<String> {
|
||||
|
||||
public static Builder make() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public static final class Builder
|
||||
extends AbstractWritable.AbstractWritableBuilder
|
||||
<ParametersValue,Builder> {
|
||||
|
||||
private final Map<String,Object> params =
|
||||
Maps.newHashMap();
|
||||
|
||||
public Builder() {
|
||||
writeUsing(ActionMakers.io);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public Iterator<String> iterator() {
|
||||
return params.keySet().iterator();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <O extends ParameterValue>O get(String param) {
|
||||
return (O)params.get(param);
|
||||
}
|
||||
|
||||
public boolean has(String param) {
|
||||
return params.containsKey(param);
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private ImmutableMap<String,Object> map;
|
||||
SerializedForm(ParametersValue obj) {
|
||||
map = obj.params;
|
||||
}
|
||||
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
ParametersValue.Builder builder =
|
||||
ParametersValue.make();
|
||||
builder.params.putAll(map);
|
||||
return builder.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,155 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObject;
|
||||
|
||||
/**
|
||||
* The value of the "parameters" property...
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public final class StylesValue
|
||||
extends AbstractDictionaryObject<String>
|
||||
implements Serializable {
|
||||
|
||||
/**
|
||||
* 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
|
||||
<String, StylesValue, Builder> {
|
||||
|
||||
public Builder() {
|
||||
writeUsing(ActionMakers.io);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method media.
|
||||
* @param query String
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder media(String query) {
|
||||
return super.set("media", query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method set.
|
||||
|
||||
|
||||
|
||||
* @param name String
|
||||
* @param value String
|
||||
* @return Builder */
|
||||
public Builder set(String name, String value) {
|
||||
checkArgument(!name.equalsIgnoreCase("query"));
|
||||
return super.set(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
|
||||
|
||||
* @return ParametersValue * @see com.google.common.base.Supplier#get() */
|
||||
public StylesValue get() {
|
||||
return new StylesValue(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for ParametersValue.
|
||||
* @param builder Builder
|
||||
*/
|
||||
StylesValue(Builder builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method media.
|
||||
* @return String
|
||||
*/
|
||||
public String media() {
|
||||
return getSingle("media");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @param key String
|
||||
* @return String
|
||||
*/
|
||||
public String get(String key) {
|
||||
return super.getSingle(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @param key String
|
||||
* @param defaultValue String
|
||||
* @return String
|
||||
*/
|
||||
public String get(String key, String defaultValue) {
|
||||
return super.getSingle(key, defaultValue);
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private ImmutableMap<String,String> map;
|
||||
SerializedForm(StylesValue obj) {
|
||||
ImmutableMap.Builder<String,String> builder =
|
||||
ImmutableMap.builder();
|
||||
for (String key : obj)
|
||||
builder.put(key, obj.get(key));
|
||||
this.map = builder.build();
|
||||
}
|
||||
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
StylesValue.Builder builder =
|
||||
StylesValue.make();
|
||||
for (Map.Entry<String,String> entry : map.entrySet())
|
||||
builder.set(entry.getKey(), entry.getValue());
|
||||
return builder.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,181 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.net.MediaType;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.LinkValue;
|
||||
import com.ibm.common.activitystreams.TypeValue;
|
||||
|
||||
public final class TypedPayload
|
||||
extends ASObject
|
||||
implements ParameterValue {
|
||||
|
||||
/**
|
||||
* Method makeTypedPayload.
|
||||
* @param mediaType MediaType
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder makeTypedPayload(MediaType mediaType) {
|
||||
return new Builder().mediaType(mediaType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method makeTypedPayload.
|
||||
* @param mediaType String
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder makeTypedPayload(String mediaType) {
|
||||
return new Builder().mediaType(mediaType);
|
||||
}
|
||||
|
||||
public static Builder make() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static final class Builder
|
||||
extends ASObject.AbstractBuilder<TypedPayload, Builder> {
|
||||
|
||||
public Builder() {
|
||||
writeUsing(ActionMakers.io);
|
||||
objectType("TypedPayload");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder schema(String iri) {
|
||||
return schema(LinkValue.SimpleLinkValue.make(iri));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param lv LinkValue
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder schema(LinkValue lv) {
|
||||
return link("schema", lv);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param lv Supplier<? extends LinkValue>
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder schema(Supplier<? extends LinkValue> lv) {
|
||||
return schema(lv.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder type(String iri) {
|
||||
return type(TypeValue.SimpleTypeValue.make(iri));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @param tv TypeValue
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder type(TypeValue tv) {
|
||||
return set("type", tv);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @param tv Supplier<? extends TypeValue>
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder type(Supplier<? extends TypeValue> tv) {
|
||||
return type(tv.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return TypedPayload
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
public TypedPayload get() {
|
||||
return new TypedPayload(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for TypedPayload.
|
||||
* @param builder Builder
|
||||
*/
|
||||
private TypedPayload(Builder builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @return L
|
||||
*/
|
||||
public Iterable<LinkValue> schema() {
|
||||
return this.links("schema");
|
||||
}
|
||||
|
||||
public Iterable<LinkValue> schema(Predicate<? super LinkValue> filter) {
|
||||
return this.links("schema", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @return TypeValue
|
||||
*/
|
||||
public TypeValue type() {
|
||||
return this.<TypeValue>get("type");
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
extends AbstractSerializedForm<TypedPayload> {
|
||||
private static final long serialVersionUID = -2060301713159936285L;
|
||||
private String mediaType;
|
||||
protected SerializedForm(TypedPayload obj) {
|
||||
super(obj);
|
||||
this.mediaType = obj.mediaType().toString();
|
||||
}
|
||||
Object readResolve() throws ObjectStreamException {
|
||||
return super.doReadResolve();
|
||||
}
|
||||
protected TypedPayload.Builder builder() {
|
||||
return ActionMakers.typedPayload(mediaType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,133 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
|
||||
/**
|
||||
*/
|
||||
public final class UrlTemplate
|
||||
extends ASObject
|
||||
implements ParameterValue {
|
||||
|
||||
/**
|
||||
* Method makeUrlTemplate.
|
||||
* @return Builder
|
||||
*/
|
||||
public static Builder makeUrlTemplate() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public static final class Builder
|
||||
extends ASObject.AbstractBuilder<UrlTemplate, Builder> {
|
||||
|
||||
private final ParametersValue.Builder params =
|
||||
ParametersValue.make();
|
||||
|
||||
public Builder() {
|
||||
writeUsing(ActionMakers.io);
|
||||
objectType("UrlTemplate");
|
||||
}
|
||||
|
||||
public Builder template(String template) {
|
||||
set("template", template);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder parameter(String name, String iri) {
|
||||
params.param(name, iri);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder parameter(
|
||||
String name,
|
||||
ParameterValue parameter) {
|
||||
params.param(name, parameter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder parameter(
|
||||
String name,
|
||||
Supplier<? extends ParameterValue> parameter) {
|
||||
return parameter(name, parameter.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return UrlTemplate
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
public UrlTemplate get() {
|
||||
if (params.notEmpty())
|
||||
set("parameters", params.get());
|
||||
return new UrlTemplate(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for UrlTemplate.
|
||||
* @param builder Builder
|
||||
*/
|
||||
private UrlTemplate(Builder builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parameters.
|
||||
* @return ParametersValue
|
||||
*/
|
||||
public ParametersValue parameters() {
|
||||
return this.<ParametersValue>get("parameters");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method template.
|
||||
* @return String
|
||||
*/
|
||||
public String template() {
|
||||
return this.getString("template");
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
extends AbstractSerializedForm<UrlTemplate> {
|
||||
private static final long serialVersionUID = -2060301713159936285L;
|
||||
protected SerializedForm(UrlTemplate obj) {
|
||||
super(obj);
|
||||
}
|
||||
Object readResolve() throws ObjectStreamException {
|
||||
return super.doReadResolve();
|
||||
}
|
||||
protected UrlTemplate.Builder builder() {
|
||||
return ActionMakers.urlTemplate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,337 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Predicate
|
||||
import com.google.common.base.Supplier
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.LinkValue
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
*/
|
||||
abstract class ActionHandler
|
||||
|
||||
/**
|
||||
* Constructor for ActionHandler.
|
||||
* @param builder Builder
|
||||
*/
|
||||
protected constructor(builder: Builder<*, *>) : ASObject(builder),
|
||||
Serializable {
|
||||
/**
|
||||
*/
|
||||
abstract class Builder<A : ActionHandler?, B : Builder<A, B>?>
|
||||
: AbstractBuilder<A, B>() {
|
||||
private val auth: Authentication.Builder = Authentication.Companion.make()
|
||||
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method confirm.
|
||||
* @param on boolean
|
||||
* @return B
|
||||
*/
|
||||
/**
|
||||
* Method confirm.
|
||||
* @return B
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun confirm(on: Boolean = true): B {
|
||||
set("confirm", on)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method context.
|
||||
* @param obj ASObject
|
||||
* @return B
|
||||
*/
|
||||
fun context(obj: ASObject?): B {
|
||||
set("context", obj)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method context.
|
||||
* @param obj Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun context(obj: Supplier<out ASObject?>): B {
|
||||
return context(obj.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method requires.
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
fun requires(iri: String?, vararg iris: String?): B {
|
||||
requires(LinkValue.SimpleLinkValue.make(iri))
|
||||
if (iris != null) for (i in iris) requires(i)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method requires.
|
||||
* @param lv LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun requires(lv: LinkValue?, vararg lvs: LinkValue?): B {
|
||||
link("requires", lv)
|
||||
if (lvs != null) for (i in lvs) requires(i)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method requires.
|
||||
* @param lv Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun requires(lv: Supplier<out LinkValue?>): B {
|
||||
return requires(lv.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prefers.
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
fun prefers(iri: String?, vararg iris: String?): B {
|
||||
prefers(LinkValue.SimpleLinkValue.make(iri))
|
||||
if (iris != null) for (i in iris) prefers(i)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prefers.
|
||||
* @param lv LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun prefers(lv: LinkValue?, vararg lvs: LinkValue?): B {
|
||||
link("prefers", lv)
|
||||
if (lvs != null) for (l in lvs) prefers(l)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prefers.
|
||||
* @param lv Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun prefers(lv: Supplier<out LinkValue?>): B {
|
||||
return prefers(lv.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method expects.
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
fun expects(iri: String?, vararg iris: String?): B {
|
||||
expects(LinkValue.SimpleLinkValue.make(iri))
|
||||
if (iris != null) for (i in iris) expects(i)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method expects.
|
||||
* @param tv LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun expects(tv: LinkValue?, vararg lvs: LinkValue?): B {
|
||||
link("expects", tv)
|
||||
if (lvs != null) for (lv in lvs) expects(lv)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method expects.
|
||||
* @param tv Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun expects(tv: Supplier<out LinkValue?>): B {
|
||||
return expects(tv.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method returns.
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
fun returns(iri: String?, vararg iris: String?): B {
|
||||
returns(LinkValue.SimpleLinkValue.make(iri))
|
||||
if (iris != null) for (i in iris) returns(i)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method returns.
|
||||
* @param tv LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun returns(tv: LinkValue?, vararg lvs: LinkValue?): B {
|
||||
link("returns", tv)
|
||||
if (lvs != null) for (lv in lvs) returns(lv)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method returns.
|
||||
* @param tv Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun returns(tv: Supplier<out LinkValue?>): B {
|
||||
return returns(tv.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method auth.
|
||||
* @param key String
|
||||
* @param obj ASObject
|
||||
* @return B
|
||||
*/
|
||||
fun auth(key: String, obj: ASObject): B {
|
||||
auth[key] = obj
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method auth.
|
||||
* @param key String
|
||||
* @param obj Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun auth(key: String, obj: Supplier<out ASObject>): B {
|
||||
return auth(key, obj.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return A
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): A {
|
||||
if (auth.notEmpty()) set("auth", auth.get())
|
||||
return actualGet()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method actualGet.
|
||||
* @return A
|
||||
*/
|
||||
protected abstract fun actualGet(): A
|
||||
}
|
||||
|
||||
/**
|
||||
* Method confirm.
|
||||
* @return boolean
|
||||
*/
|
||||
fun confirm(): Boolean {
|
||||
return this.getBoolean("confirm")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method context.
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> context(): A? {
|
||||
return this.get<A>("context")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method expects.
|
||||
* @return Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun expects(): Iterable<LinkValue> {
|
||||
return this.links("expects")
|
||||
}
|
||||
|
||||
fun expects(filter: Predicate<in LinkValue?>): Iterable<LinkValue?> {
|
||||
return this.links("expects", filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method requires.
|
||||
* @return Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun requires(): Iterable<LinkValue> {
|
||||
return this.links("requires")
|
||||
}
|
||||
|
||||
fun requires(filter: Predicate<in LinkValue?>): Iterable<LinkValue?> {
|
||||
return this.links("requires", filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prefers.
|
||||
* @return Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun prefers(): Iterable<LinkValue> {
|
||||
return this.links("prefers")
|
||||
}
|
||||
|
||||
fun prefers(filter: Predicate<in LinkValue?>): Iterable<LinkValue?> {
|
||||
return this.links("prefers", filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method returns.
|
||||
* @return Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun returns(): Iterable<LinkValue> {
|
||||
return this.links("returns")
|
||||
}
|
||||
|
||||
fun returns(filter: Predicate<in LinkValue?>): Iterable<LinkValue?> {
|
||||
return this.links("returns", filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method auth.
|
||||
* @return Authentication
|
||||
*/
|
||||
fun auth(): Authentication? {
|
||||
return this.get<Authentication>("auth")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method hasAuth.
|
||||
* @param key String
|
||||
* @return boolean
|
||||
*/
|
||||
fun hasAuth(key: String?): Boolean {
|
||||
val auth = auth()
|
||||
return auth?.has(key) ?: false
|
||||
}
|
||||
|
||||
/**
|
||||
* Method auth.
|
||||
* @param key String
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> auth(key: String?): A? {
|
||||
val auth = auth()
|
||||
return if (auth != null) auth.get<ASObject>(key) as A else null
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,250 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.IO
|
||||
import com.ibm.common.activitystreams.IO.Companion.makeDefault
|
||||
import com.ibm.common.activitystreams.IO.Companion.makeDefaultPrettyPrint
|
||||
import com.ibm.common.activitystreams.Makers.`object`
|
||||
import com.ibm.common.activitystreams.actions.ParameterValue.SimpleParameterValue
|
||||
|
||||
/**
|
||||
*/
|
||||
object ActionMakers {
|
||||
@JvmField
|
||||
val io: IO = makeDefault(ActionsModule.Companion.instance)
|
||||
val ioPretty: IO = makeDefaultPrettyPrint(ActionsModule.Companion.instance)
|
||||
|
||||
const val TARGET_NONE: String = "NONE"
|
||||
const val TARGET_DEFAULT: String = "DEFAULT"
|
||||
const val TARGET_NEW: String = "NEW"
|
||||
const val TARGET_CURRENT: String = "CURRENT"
|
||||
const val TARGET_TAB: String = "TAB"
|
||||
const val TARGET_DIALOG: String = "DIALOG"
|
||||
const val TARGET_MODALDIALOG: String = "MODALDIALOG"
|
||||
const val TARGET_FLOAT: String = "FLOAT"
|
||||
const val TARGET_SIDEBAR: String = "SIDEBAR"
|
||||
|
||||
const val METHOD_GET: String = "GET"
|
||||
const val METHOD_POST: String = "POST"
|
||||
const val METHOD_PUT: String = "PUT"
|
||||
const val METHOD_DELETE: String = "DELETE"
|
||||
const val METHOD_PATCH: String = "PATCH"
|
||||
|
||||
/**
|
||||
* Method application.
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
fun application(): ASObject.Builder? {
|
||||
return `object`("application")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method service.
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
fun service(): ASObject.Builder? {
|
||||
return `object`("service")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @return StylesValue.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun styles(): StylesValue.Builder {
|
||||
return StylesValue.Companion.make()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @param media String
|
||||
* @return StylesValue.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun styles(media: String?): StylesValue.Builder? {
|
||||
return styles().media(media)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method httpAction.
|
||||
* @return HttpActionHandler.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun httpAction(): HttpActionHandler.Builder {
|
||||
return HttpActionHandler.Companion.makeHttpActionHandler()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method httpAction.
|
||||
* @param url String
|
||||
* @return HttpActionHandler.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun httpAction(url: String?): HttpActionHandler.Builder? {
|
||||
return httpAction().url(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method httpAction.
|
||||
* @param url String
|
||||
* @param method String
|
||||
* @return HttpActionHandler.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun httpAction(
|
||||
url: String?,
|
||||
method: String?
|
||||
): HttpActionHandler.Builder? {
|
||||
return httpAction()
|
||||
.url(url)
|
||||
?.method(method)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method embedAction.
|
||||
* @return EmbedActionHandler.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun embedAction(): EmbedActionHandler.Builder {
|
||||
return EmbedActionHandler.Companion.makeEmbedActionHandler()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method embedAction.
|
||||
* @param url String
|
||||
* @return EmbedActionHandler.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun embedAction(url: String?): EmbedActionHandler.Builder? {
|
||||
return embedAction().url(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method embedAction.
|
||||
* @param url String
|
||||
* @param mediaType String
|
||||
* @return EmbedActionHandler.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun embedAction(
|
||||
url: String?,
|
||||
mediaType: String
|
||||
): EmbedActionHandler.Builder? {
|
||||
return embedAction(url)!!.mediaType(mediaType)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method staticEmbedAction.
|
||||
* @param mediaType String
|
||||
* @param content String
|
||||
* @return EmbedActionHandler.Builder
|
||||
*/
|
||||
fun staticEmbedAction(
|
||||
mediaType: String,
|
||||
content: String?
|
||||
): EmbedActionHandler.Builder? {
|
||||
return embedAction().mediaType(mediaType)!!.content(content)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method intentAction.
|
||||
* @return IntentActionHandler.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun intentAction(): IntentActionHandler.Builder {
|
||||
return IntentActionHandler.Companion.makeIntentActionHandler()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method intentAction.
|
||||
* @param url String
|
||||
* @return IntentActionHandler.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun intentAction(url: String?): IntentActionHandler.Builder? {
|
||||
return intentAction().url(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method htmlForm.
|
||||
* @return HtmlForm.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun htmlForm(): HtmlForm.Builder {
|
||||
return HtmlForm.Companion.makeHtmlForm()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method typedPayload.
|
||||
* @param mediaType String
|
||||
* @return TypedPayload.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun typedPayload(mediaType: String): TypedPayload.Builder? {
|
||||
return TypedPayload.Companion.makeTypedPayload(mediaType)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun typedPayload(): TypedPayload.Builder {
|
||||
return TypedPayload.Companion.make()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method urlTemplate.
|
||||
* @return UrlTemplate.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun urlTemplate(): UrlTemplate.Builder {
|
||||
return UrlTemplate.Companion.makeUrlTemplate()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method urlTemplate.
|
||||
* @param template String
|
||||
* @return UrlTemplate.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun urlTemplate(template: String?): UrlTemplate.Builder {
|
||||
return urlTemplate().template(template)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parameter.
|
||||
* @return Parameter.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun parameter(): Parameter.Builder {
|
||||
return Parameter.Companion.makeParameter()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parameter.
|
||||
* @param id String
|
||||
* @return Parameter.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun parameter(id: String?): SimpleParameterValue {
|
||||
return SimpleParameterValue.Builder().type(id).get()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.i.ASObjectAdapter
|
||||
import com.ibm.common.activitystreams.i.Model
|
||||
import com.ibm.common.activitystreams.i.Schema
|
||||
import java.lang.reflect.Type
|
||||
|
||||
class ActionObjectAdapter
|
||||
internal constructor(schema: Schema) : ASObjectAdapter(schema) {
|
||||
override fun knowsType(type: Type?): Boolean {
|
||||
if (super.knowsType(type)) return true
|
||||
return knownTypes.contains(type)
|
||||
}
|
||||
|
||||
override fun builderFor(type: Type): ASObject.AbstractBuilder<*, *>? {
|
||||
if (super.knowsType(type)) return super.builderFor(type)
|
||||
return if (type === HttpActionHandler::class.java) ActionMakers.httpAction()
|
||||
else if (type === IntentActionHandler::class.java) ActionMakers.intentAction()
|
||||
else if (type === EmbedActionHandler::class.java) ActionMakers.embedAction()
|
||||
else if (type === HtmlForm::class.java) ActionMakers.htmlForm()
|
||||
else if (type === UrlTemplate::class.java) ActionMakers.urlTemplate()
|
||||
else if (type === TypedPayload::class.java) ActionMakers.typedPayload()
|
||||
else null
|
||||
}
|
||||
|
||||
override fun modelFor(type: Type): Model? {
|
||||
if (super.knowsType(type)) return super.modelFor(type)
|
||||
return if (type === HttpActionHandler::class.java) schema().forObjectClassOrType(
|
||||
HttpActionHandler.Builder::class.java,
|
||||
"HttpActionHandler"
|
||||
)
|
||||
else if (type === IntentActionHandler::class.java) schema().forObjectClassOrType(
|
||||
IntentActionHandler.Builder::class.java,
|
||||
"IntentActionHandler"
|
||||
)
|
||||
else if (type === EmbedActionHandler::class.java) schema().forObjectClassOrType(
|
||||
EmbedActionHandler.Builder::class.java,
|
||||
"EmbedActionHandler"
|
||||
)
|
||||
else if (type === HtmlForm::class.java) schema().forObjectClassOrType(
|
||||
HtmlForm.Builder::class.java,
|
||||
"HtmlForm"
|
||||
)
|
||||
else if (type === UrlTemplate::class.java) schema().forObjectClassOrType(
|
||||
UrlTemplate.Builder::class.java,
|
||||
"UrlTemplate"
|
||||
)
|
||||
else if (type === TypedPayload::class.java) schema().forObjectClassOrType(
|
||||
TypedPayload.Builder::class.java,
|
||||
"TypedPayload"
|
||||
)
|
||||
else null
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val knownTypes: ImmutableSet<Class<*>> = ImmutableSet.of(
|
||||
HttpActionHandler::class.java,
|
||||
IntentActionHandler::class.java,
|
||||
EmbedActionHandler::class.java,
|
||||
HtmlForm::class.java,
|
||||
UrlTemplate::class.java,
|
||||
TypedPayload::class.java
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.IO
|
||||
import com.ibm.common.activitystreams.i.Model
|
||||
import com.ibm.common.activitystreams.i.Schema
|
||||
import com.ibm.common.activitystreams.util.Module
|
||||
|
||||
class ActionsModule
|
||||
|
||||
: Module {
|
||||
override fun apply(builder: IO.Builder, schema: Schema) {
|
||||
val base =
|
||||
ActionObjectAdapter(schema)
|
||||
|
||||
|
||||
// Register Adapters
|
||||
builder.hierarchicalAdapter<ASObject>(ActionHandler::class.java, base)
|
||||
.hierarchicalAdapter<ASObject>(HttpActionHandler::class.java, base)
|
||||
.hierarchicalAdapter<ASObject>(IntentActionHandler::class.java, base)
|
||||
.hierarchicalAdapter<ASObject>(EmbedActionHandler::class.java, base)
|
||||
.hierarchicalAdapter<ASObject>(HtmlForm::class.java, base)
|
||||
.hierarchicalAdapter<ParametersValue>(ParametersValue::class.java, ParametersAdapter.instance)
|
||||
.hierarchicalAdapter<ParameterValue>(ParameterValue::class.java, ParameterAdapter.instance)
|
||||
.hierarchicalAdapter<ASObject>(UrlTemplate::class.java, base)
|
||||
.hierarchicalAdapter<ASObject>(TypedPayload::class.java, base)
|
||||
.hierarchicalAdapter<Authentication>(Authentication::class.java, Adapters.AUTH)
|
||||
.hierarchicalAdapter<StylesValue>(StylesValue::class.java, Adapters.STYLES)
|
||||
}
|
||||
|
||||
override fun apply(builder: Schema.Builder) {
|
||||
builder.map(
|
||||
"HtmlForm", withParameters.template(
|
||||
HtmlForm::class.java,
|
||||
HtmlForm.Builder::class.java
|
||||
)
|
||||
)
|
||||
.map("TypedPayload", typedPayload)
|
||||
.map(
|
||||
"UrlTemplate", withParameters.template(
|
||||
UrlTemplate::class.java,
|
||||
UrlTemplate.Builder::class.java
|
||||
)
|
||||
)
|
||||
.map(
|
||||
"HttpActionHandler", actionHandler.template(
|
||||
HttpActionHandler::class.java,
|
||||
HttpActionHandler.Builder::class.java
|
||||
)
|
||||
)
|
||||
.map(
|
||||
"IntentActionHandler", actionHandler.template(
|
||||
IntentActionHandler::class.java,
|
||||
IntentActionHandler.Builder::class.java
|
||||
)
|
||||
)
|
||||
.map(
|
||||
"EmbedActionHandler", actionHandler.template(
|
||||
EmbedActionHandler::class.java,
|
||||
EmbedActionHandler.Builder::class.java
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val instance: Module = ActionsModule()
|
||||
|
||||
val actionHandler: Model = Model
|
||||
.make("object")
|
||||
.linkValue(
|
||||
"expects",
|
||||
"returns",
|
||||
"requires",
|
||||
"prefers"
|
||||
)
|
||||
.`object`("context")
|
||||
.`as`("auth", Authentication::class.java)
|
||||
.`as`("style", StylesValue::class.java)
|
||||
.get()
|
||||
|
||||
|
||||
val withParameters: Model = Model
|
||||
.make("object")
|
||||
.`as`("parameters", ParametersValue::class.java)
|
||||
.get()
|
||||
|
||||
val typedPayload: Model = Model
|
||||
.make("object")
|
||||
.type(TypedPayload::class.java, TypedPayload.Builder::class.java)
|
||||
.linkValue("schema")
|
||||
.typeValue("type")
|
||||
.get()
|
||||
}
|
||||
}
|
||||
|
|
@ -19,37 +19,28 @@
|
|||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions;
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.actions.StylesValue.Builder;
|
||||
import com.ibm.common.activitystreams.internal.Adapter;
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObjectAdapter;
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.i.Adapter
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObjectAdapter
|
||||
|
||||
final class Adapters {
|
||||
internal object Adapters {
|
||||
val AUTH: Adapter<Authentication> =
|
||||
object : AbstractDictionaryObjectAdapter<ASObject, Authentication, Authentication.Builder>(
|
||||
ASObject::class.java
|
||||
) {
|
||||
override fun builder(): Authentication.Builder {
|
||||
return Authentication.Companion.make()
|
||||
}
|
||||
}
|
||||
|
||||
private Adapters() {}
|
||||
|
||||
static final Adapter<Authentication> AUTH =
|
||||
new AbstractDictionaryObjectAdapter
|
||||
<ASObject,
|
||||
Authentication,
|
||||
Authentication.Builder>(ASObject.class) {
|
||||
@Override
|
||||
protected Authentication.Builder builder() {
|
||||
return Authentication.make();
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<StylesValue> STYLES =
|
||||
new AbstractDictionaryObjectAdapter
|
||||
<String,
|
||||
StylesValue,
|
||||
StylesValue.Builder>(String.class) {
|
||||
@Override
|
||||
protected Builder builder() {
|
||||
return StylesValue.make();
|
||||
}
|
||||
};
|
||||
|
||||
val STYLES: Adapter<StylesValue> =
|
||||
object : AbstractDictionaryObjectAdapter<String, StylesValue, StylesValue.Builder>(
|
||||
String::class.java
|
||||
) {
|
||||
override fun builder(): StylesValue.Builder {
|
||||
return StylesValue.Companion.make()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObject
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
*/
|
||||
class Authentication
|
||||
|
||||
/**
|
||||
* Constructor for Authentication.
|
||||
* @param builder Builder
|
||||
*/
|
||||
protected constructor(builder: Builder) :
|
||||
AbstractDictionaryObject<ASObject>(builder) {
|
||||
/**
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<ASObject, Authentication, Builder>() {
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return Authentication
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): Authentication {
|
||||
return Authentication(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @param key String
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> get(key: String?): A? {
|
||||
return getSingle(key) as A?
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: Authentication) : Serializable {
|
||||
private val map: ImmutableMap<String, Any>
|
||||
|
||||
init {
|
||||
val builder =
|
||||
ImmutableMap.builder<String, Any>()
|
||||
for (key in obj) builder.put(key, obj.get(key))
|
||||
this.map = builder.build()
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
val builder =
|
||||
make()
|
||||
for ((key, value) in map) builder[key] =
|
||||
value as ASObject
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method make.
|
||||
* @return Builder
|
||||
*/
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,218 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.common.base.Predicate
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.collect.Iterables
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
*/
|
||||
class EmbedActionHandler
|
||||
|
||||
/**
|
||||
* Constructor for EmbedActionHandler.
|
||||
* @param builder AbstractBuilder
|
||||
*/
|
||||
protected constructor(builder: AbstractBuilder<*, *>) :
|
||||
ActionHandler(builder), Serializable {
|
||||
/**
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<EmbedActionHandler, Builder?>("EmbedActionHandler") {
|
||||
override val actual: EmbedActionHandler
|
||||
/**
|
||||
* Method getActual.
|
||||
* @return EmbedActionHandler
|
||||
*/
|
||||
get() = EmbedActionHandler(this)
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
abstract class AbstractBuilder<A : EmbedActionHandler?, B : AbstractBuilder<A, B>?>
|
||||
protected constructor(objectType: String?) :
|
||||
ActionHandler.Builder<A, B>() {
|
||||
private var styleset = false
|
||||
|
||||
/**
|
||||
* Constructor for AbstractBuilder.
|
||||
* @param objectType String
|
||||
*/
|
||||
init {
|
||||
objectType(objectType)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method target.
|
||||
* @param target String
|
||||
* @return B
|
||||
*/
|
||||
fun target(target: String?): B {
|
||||
set("target", target)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method style.
|
||||
* @param style StylesValue
|
||||
* @return B
|
||||
*/
|
||||
fun style(style: StylesValue): B {
|
||||
styleset = true
|
||||
styles.add(style)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method style.
|
||||
* @param style Supplier<StylesValue>
|
||||
* @return B
|
||||
</StylesValue> */
|
||||
fun style(style: Supplier<StylesValue?>): B {
|
||||
return style(style.get()!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method style.
|
||||
* @param styles StylesValue[]
|
||||
* @return B
|
||||
*/
|
||||
fun style(vararg styles: StylesValue): B {
|
||||
for (s in styles) style(s)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method style.
|
||||
* @param styles Supplier<StylesValue>[]
|
||||
* @return B
|
||||
</StylesValue> */
|
||||
fun style(vararg styles: Supplier<StylesValue?>): B {
|
||||
for (s in styles) style(s)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method actualGet.
|
||||
* @return A
|
||||
*/
|
||||
override fun actualGet(): A {
|
||||
if (styleset) {
|
||||
val `val` = styles.build()
|
||||
if (Iterables.size(`val`) == 1) {
|
||||
set("style", Iterables.getFirst(`val`, null))
|
||||
} else {
|
||||
set("style", `val`)
|
||||
}
|
||||
}
|
||||
return actual
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getActual.
|
||||
* @return A
|
||||
*/
|
||||
protected abstract val actual: A
|
||||
|
||||
companion object {
|
||||
private val styles: ImmutableList.Builder<StylesValue?> = ImmutableList.builder()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @return Iterable<StylesValue>
|
||||
</StylesValue> */
|
||||
fun styles(): Iterable<StylesValue> {
|
||||
val styles = get<Any>("style")
|
||||
return if (styles is StylesValue) ImmutableList.of(styles)
|
||||
else if (styles is Iterable<*>) styles as Iterable<StylesValue>
|
||||
else ImmutableList.of()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @param media String
|
||||
* @return Iterable<StylesValue>
|
||||
</StylesValue> */
|
||||
fun styles(media: String): Iterable<StylesValue> {
|
||||
Preconditions.checkNotNull(media)
|
||||
return styles { style -> media.equals(style?.media(), ignoreCase = true) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Method styles.
|
||||
* @param matcher Predicate<StylesValue>
|
||||
* @return Iterable<StylesValue>
|
||||
</StylesValue></StylesValue> */
|
||||
fun styles(matcher: Predicate<StylesValue>): Iterable<StylesValue> {
|
||||
return Iterables.filter(styles(), matcher)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method target.
|
||||
* @return String
|
||||
*/
|
||||
fun target(): String? {
|
||||
return getString(
|
||||
"target",
|
||||
ActionMakers.TARGET_DEFAULT
|
||||
)
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
override fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: EmbedActionHandler?) : AbstractSerializedForm<EmbedActionHandler?>(obj) {
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any? {
|
||||
return super.doReadResolve()
|
||||
}
|
||||
|
||||
override fun builder(): Builder {
|
||||
return ActionMakers.embedAction()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -2060301713159936285L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method makeEmbedActionHandler.
|
||||
* @return Builder
|
||||
*/
|
||||
fun makeEmbedActionHandler(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Supplier
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import java.io.ObjectStreamException
|
||||
|
||||
/**
|
||||
*/
|
||||
class HtmlForm
|
||||
|
||||
/**
|
||||
* Constructor for HtmlForm.
|
||||
* @param builder Builder
|
||||
*/
|
||||
private constructor(builder: Builder) : ASObject(builder) {
|
||||
/**
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<HtmlForm?, Builder?>() {
|
||||
private val params: ParametersValue.Builder = ParametersValue.Companion.make()
|
||||
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
objectType("HtmlForm")
|
||||
mediaType("application/x-www-form-urlencoded")
|
||||
}
|
||||
|
||||
fun parameter(name: String, iri: String?): Builder {
|
||||
params.param(name, iri)
|
||||
return this
|
||||
}
|
||||
|
||||
fun parameter(
|
||||
name: String,
|
||||
parameter: ParameterValue?
|
||||
): Builder {
|
||||
params.param(name, parameter)
|
||||
return this
|
||||
}
|
||||
|
||||
fun parameter(
|
||||
name: String,
|
||||
parameter: Supplier<out ParameterValue?>
|
||||
): Builder {
|
||||
return parameter(name, parameter.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return HtmlForm
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): HtmlForm {
|
||||
if (params.notEmpty()) set("parameters", params.get())
|
||||
return HtmlForm(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parameters.
|
||||
* @return ParametersValue
|
||||
*/
|
||||
fun parameters(): ParametersValue? {
|
||||
return this.get<ParametersValue>("parameters")
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
override fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: HtmlForm?) : AbstractSerializedForm<HtmlForm?>(obj) {
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any? {
|
||||
return super.doReadResolve()
|
||||
}
|
||||
|
||||
override fun builder(): Builder {
|
||||
return ActionMakers.htmlForm()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -2060301713159936285L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method makeHtmlForm.
|
||||
* @return Builder
|
||||
*/
|
||||
fun makeHtmlForm(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,131 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import java.io.ObjectStreamException
|
||||
|
||||
/**
|
||||
*/
|
||||
class HttpActionHandler
|
||||
|
||||
/**
|
||||
* Constructor for HttpActionHandler.
|
||||
* @param builder AbstractBuilder
|
||||
*/
|
||||
protected constructor(builder: AbstractBuilder<*, *>) :
|
||||
ActionHandler(builder) {
|
||||
/**
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<HttpActionHandler?, Builder?>("HttpActionHandler") {
|
||||
/**
|
||||
* Method actualGet.
|
||||
* @return HttpActionHandler
|
||||
*/
|
||||
override fun actualGet(): HttpActionHandler {
|
||||
return HttpActionHandler(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
abstract class AbstractBuilder
|
||||
<A : HttpActionHandler?, B : AbstractBuilder<A, B>?>
|
||||
protected constructor(objectType: String?) :
|
||||
ActionHandler.Builder<A, B>() {
|
||||
/**
|
||||
* Constructor for AbstractBuilder.
|
||||
* @param objectType String
|
||||
*/
|
||||
init {
|
||||
objectType(objectType)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method method.
|
||||
* @param method String
|
||||
* @return B
|
||||
*/
|
||||
fun method(method: String?): B {
|
||||
set("method", method)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Method target.
|
||||
* @param target String
|
||||
* @return B
|
||||
*/
|
||||
fun target(target: String?): B {
|
||||
set("target", target)
|
||||
return this as B
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method method.
|
||||
* @return String
|
||||
*/
|
||||
fun method(): String? {
|
||||
return this.getString("method")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method target.
|
||||
* @return String
|
||||
*/
|
||||
fun target(): String? {
|
||||
return if (has("target")) this.get<String>("target") else ActionMakers.TARGET_DEFAULT
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
override fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: HttpActionHandler?) : AbstractSerializedForm<HttpActionHandler?>(obj) {
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any? {
|
||||
return super.doReadResolve()
|
||||
}
|
||||
|
||||
override fun builder(): Builder {
|
||||
return ActionMakers.httpAction()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -2060301713159936285L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method makeHttpActionHandler.
|
||||
* @return Builder
|
||||
*/
|
||||
fun makeHttpActionHandler(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import java.io.ObjectStreamException
|
||||
|
||||
/**
|
||||
*/
|
||||
class IntentActionHandler
|
||||
|
||||
/**
|
||||
* Constructor for IntentActionHandler.
|
||||
* @param builder AbstractBuilder
|
||||
*/
|
||||
protected constructor(builder: AbstractBuilder<*, *>) :
|
||||
ActionHandler(builder) {
|
||||
/**
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<IntentActionHandler?, Builder?>("IntentActionHandler") {
|
||||
/**
|
||||
* Method actualGet.
|
||||
* @return IntentActionHandler
|
||||
*/
|
||||
override fun actualGet(): IntentActionHandler {
|
||||
return IntentActionHandler(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
abstract class AbstractBuilder
|
||||
<A : IntentActionHandler?, B : AbstractBuilder<A, B>?>
|
||||
protected constructor(objectType: String?) :
|
||||
ActionHandler.Builder<A, B>() {
|
||||
/**
|
||||
* Constructor for AbstractBuilder.
|
||||
* @param objectType String
|
||||
*/
|
||||
init {
|
||||
objectType(objectType)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
override fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: IntentActionHandler?) : AbstractSerializedForm<IntentActionHandler?>(obj) {
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any? {
|
||||
return super.doReadResolve()
|
||||
}
|
||||
|
||||
override fun builder(): Builder {
|
||||
return ActionMakers.intentAction()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -2060301713159936285L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method makeIntentActionHandler.
|
||||
* @return Builder
|
||||
*/
|
||||
fun makeIntentActionHandler(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,670 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Objects
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.*
|
||||
import com.ibm.common.activitystreams.*
|
||||
import com.ibm.common.activitystreams.Makers.nlv
|
||||
import com.ibm.common.activitystreams.NLV.MapNLV
|
||||
import com.ibm.common.activitystreams.NLV.SimpleNLV
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable
|
||||
import com.ibm.common.activitystreams.util.Util.DEFAULT_LOCALE
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class Parameter
|
||||
protected constructor(builder: Builder) : AbstractWritable(builder),
|
||||
ParameterValue {
|
||||
class Builder
|
||||
|
||||
: AbstractWritableBuilder<Parameter?, Builder?>() {
|
||||
val map: MutableMap<String, Any> = Maps.newHashMap()
|
||||
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
}
|
||||
|
||||
fun language(lang: String?): Builder {
|
||||
if (lang != null) map["language"] = lang
|
||||
else map.remove("language")
|
||||
return this
|
||||
}
|
||||
|
||||
protected fun _nlv(key: String, value: String?): Builder {
|
||||
if (value != null) map[key] = nlv(value)
|
||||
else map.remove(key)
|
||||
return this
|
||||
}
|
||||
|
||||
protected fun _nlv(key: String, nlv: NLV?): Builder {
|
||||
if (nlv != null) map[key] = nlv
|
||||
else map.remove(key)
|
||||
return this
|
||||
}
|
||||
|
||||
protected fun _nlv(key: String, nlv: Supplier<out NLV?>): Builder {
|
||||
return _nlv(key, nlv.get())
|
||||
}
|
||||
|
||||
protected fun _nlv(key: String, map: Map<String, String?>?): Builder {
|
||||
if (map != null) for ((key1, value) in map) _nlv(
|
||||
key,
|
||||
key1,
|
||||
value
|
||||
)
|
||||
else this.map.remove(key)
|
||||
return this
|
||||
}
|
||||
|
||||
protected fun _nlv(key: String, lang: String, value: String?): Builder {
|
||||
if (map.containsKey(key)) {
|
||||
val obj = map[key]
|
||||
if (obj is NLV) {
|
||||
when (obj.valueType()) {
|
||||
ValueType.SIMPLE -> {
|
||||
var l = map["language"] as String?
|
||||
if (l == null) l = DEFAULT_LOCALE
|
||||
val b =
|
||||
nlv()
|
||||
if (lang == l) b[lang] = value
|
||||
else b.set(l, (obj as SimpleNLV).value())[lang] = value
|
||||
map[key] = b
|
||||
return this
|
||||
}
|
||||
|
||||
ValueType.OBJECT -> {
|
||||
map[key] = nlv()
|
||||
.from(obj as MapNLV, lang)
|
||||
.set(lang, value)
|
||||
return this
|
||||
}
|
||||
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
} else if (obj is MapNLV.Builder) {
|
||||
obj[lang] = value
|
||||
return this
|
||||
}
|
||||
}
|
||||
map[key] = nlv().set(lang, value)
|
||||
return this
|
||||
}
|
||||
|
||||
fun displayName(`val`: String?): Builder {
|
||||
return _nlv("displayName", `val`)
|
||||
}
|
||||
|
||||
fun displayName(nlv: NLV?): Builder {
|
||||
return _nlv("displayName", nlv)
|
||||
}
|
||||
|
||||
fun displayName(nlv: Supplier<out NLV?>): Builder {
|
||||
return _nlv("displayName", nlv)
|
||||
}
|
||||
|
||||
fun displayName(lang: String, `val`: String?): Builder {
|
||||
return _nlv("displayName", lang, `val`)
|
||||
}
|
||||
|
||||
fun placeholder(placeholder: String?): Builder {
|
||||
return _nlv("placeholder", placeholder)
|
||||
}
|
||||
|
||||
fun placeholder(placeholder: NLV?): Builder {
|
||||
return _nlv("placeholder", placeholder)
|
||||
}
|
||||
|
||||
fun placeholder(nlv: Supplier<out NLV?>): Builder {
|
||||
return _nlv("placeholder", nlv)
|
||||
}
|
||||
|
||||
fun placeholder(lang: String, `val`: String?): Builder {
|
||||
return _nlv("placeholder", lang, `val`)
|
||||
}
|
||||
|
||||
fun type(type: String?): Builder {
|
||||
if (type != null) map["type"] = type
|
||||
else map.remove("type")
|
||||
return this
|
||||
}
|
||||
|
||||
fun required(): Builder {
|
||||
map.remove("required")
|
||||
return this
|
||||
}
|
||||
|
||||
fun optional(): Builder {
|
||||
map["required"] = false
|
||||
return this
|
||||
}
|
||||
|
||||
fun repeated(): Builder {
|
||||
map["repeated"] = true
|
||||
return this
|
||||
}
|
||||
|
||||
fun notRepeated(): Builder {
|
||||
map.remove("repeated")
|
||||
return this
|
||||
}
|
||||
|
||||
fun value(value: Any?): Builder {
|
||||
if (value != null) map["value"] = value
|
||||
else map.remove("value")
|
||||
return this
|
||||
}
|
||||
|
||||
fun defaultValue(value: Any?): Builder {
|
||||
if (value != null) map["default"] = value
|
||||
else map.remove("default")
|
||||
return this
|
||||
}
|
||||
|
||||
fun pattern(vararg pattern: Pattern): Builder {
|
||||
if (pattern != null && pattern.size > 0) {
|
||||
val patterns =
|
||||
ImmutableSet.builder<String>()
|
||||
for (p in pattern) patterns.add(p.pattern())
|
||||
map["pattern"] = patterns.build()
|
||||
} else {
|
||||
map.remove("pattern")
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun pattern(vararg pattern: String): Builder {
|
||||
if (pattern != null && pattern.size > 0) {
|
||||
val patterns =
|
||||
ImmutableSet.copyOf(pattern)
|
||||
map["pattern"] = patterns
|
||||
} else {
|
||||
map.remove("pattern")
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun pattern(patterns: Iterable<String?>?): Builder {
|
||||
if (patterns != null && Iterables.size(patterns) > 0) {
|
||||
map["pattern"] = ImmutableSet.copyOf(patterns)
|
||||
} else map.remove("pattern")
|
||||
return this
|
||||
}
|
||||
|
||||
fun minInclusive(min: Any?): Builder {
|
||||
if (min != null) map["minInclusive"] = min
|
||||
else map.remove("minInclusive")
|
||||
return this
|
||||
}
|
||||
|
||||
fun minExclusive(min: Any?): Builder {
|
||||
if (min != null) map["minExclusive"] = min
|
||||
else map.remove("minExclusive")
|
||||
return this
|
||||
}
|
||||
|
||||
fun maxInclusive(max: Any?): Builder {
|
||||
if (max != null) map["maxInclusive"] = max
|
||||
else map.remove("maxInclusive")
|
||||
return this
|
||||
}
|
||||
|
||||
fun maxExclusive(max: Any?): Builder {
|
||||
if (max != null) map["maxExclusive"] = max
|
||||
else map.remove("maxExclusive")
|
||||
return this
|
||||
}
|
||||
|
||||
fun bound(range: Range<*>?): Builder {
|
||||
if (range != null) {
|
||||
if (range.hasLowerBound()) {
|
||||
when (range.lowerBoundType()) {
|
||||
BoundType.CLOSED -> minInclusive(range.lowerEndpoint())
|
||||
BoundType.OPEN -> minExclusive(range.lowerEndpoint())
|
||||
else -> {}
|
||||
}
|
||||
} else {
|
||||
minInclusive(null)
|
||||
minExclusive(null)
|
||||
}
|
||||
if (range.hasUpperBound()) {
|
||||
when (range.upperBoundType()) {
|
||||
BoundType.CLOSED -> maxInclusive(range.upperEndpoint())
|
||||
BoundType.OPEN -> maxExclusive(range.upperEndpoint())
|
||||
else -> {}
|
||||
}
|
||||
} else {
|
||||
maxInclusive(null)
|
||||
maxExclusive(null)
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun step(step: Number?): Builder {
|
||||
if (step != null) map["step"] = step
|
||||
else map.remove("step")
|
||||
return this
|
||||
}
|
||||
|
||||
fun enumeration(vararg vals: Any): Builder {
|
||||
if (vals != null && vals.size > 0) map["enumeration"] =
|
||||
ImmutableList.copyOf(vals)
|
||||
else map.remove("enumeration")
|
||||
return this
|
||||
}
|
||||
|
||||
fun maxLength(length: Int): Builder {
|
||||
if (length > -1) map["maxLength"] = length
|
||||
else map.remove("maxLength")
|
||||
return this
|
||||
}
|
||||
|
||||
fun minLength(length: Int): Builder {
|
||||
if (length > -1) map["minLength"] = length
|
||||
else map.remove("minLength")
|
||||
return this
|
||||
}
|
||||
|
||||
fun totalDigits(num: Int): Builder {
|
||||
if (num > -1) map["totalDigits"] = num
|
||||
else map.remove("totalDigits")
|
||||
return this
|
||||
}
|
||||
|
||||
fun fractionDigits(num: Int): Builder {
|
||||
if (num > -1) map["fractionDigits"] = num
|
||||
else map.remove("fractionDigits")
|
||||
return this
|
||||
}
|
||||
|
||||
override fun get(): Parameter {
|
||||
return Parameter(this)
|
||||
}
|
||||
}
|
||||
|
||||
private val map: ImmutableMap<String, Any> =
|
||||
ImmutableMap.copyOf(builder.map)
|
||||
|
||||
/**
|
||||
* Method required.
|
||||
* @return boolean
|
||||
*/
|
||||
fun required(): Boolean {
|
||||
return if (!has("required")) true else (map["required"] as Boolean?)!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method repeated.
|
||||
* @return boolean
|
||||
*/
|
||||
fun repeated(): Boolean {
|
||||
return if (!has("repeated")) false else (map["repeated"] as Boolean?)!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method value.
|
||||
* @return O
|
||||
*/
|
||||
fun <O> value(): O? {
|
||||
return map["value"] as O?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method value.
|
||||
* @param defaultValue O
|
||||
* @return O
|
||||
*/
|
||||
fun <O> value(defaultValue: O): O {
|
||||
val `val` = value<O>()
|
||||
return `val` ?: defaultValue
|
||||
}
|
||||
|
||||
/**
|
||||
* Method defaultValue.
|
||||
* @return O
|
||||
*/
|
||||
fun <O> defaultValue(): O? {
|
||||
return map["default"] as O?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method defaultValue.
|
||||
* @param defaultValue O
|
||||
* @return O
|
||||
*/
|
||||
fun <O> defaultValue(defaultValue: O): O {
|
||||
val `val` = defaultValue<O>()
|
||||
return `val` ?: defaultValue
|
||||
}
|
||||
|
||||
fun type(): String? {
|
||||
return map["type"] as String?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method pattern.
|
||||
* @return String
|
||||
*/
|
||||
fun pattern(): Iterable<String>? {
|
||||
return map["pattern"] as Iterable<String>?
|
||||
}
|
||||
|
||||
fun <O> maxInclusive(): O? {
|
||||
return map["maxInclusive"] as O?
|
||||
}
|
||||
|
||||
fun <O> maxExclusive(): O? {
|
||||
return map["maxExclusive"] as O?
|
||||
}
|
||||
|
||||
fun <O> minInclusive(): O? {
|
||||
return map["minInclusive"] as O?
|
||||
}
|
||||
|
||||
fun <O> minExclusive(): O? {
|
||||
return map["minExclusive"] as O?
|
||||
}
|
||||
|
||||
fun has(key: String?): Boolean {
|
||||
return map.containsKey(key)
|
||||
}
|
||||
|
||||
fun hasUpperBound(): Boolean {
|
||||
return has("maxInclusive") || has("maxExclusive")
|
||||
}
|
||||
|
||||
fun <O : Comparable<O>?> bounds(): Range<O?> {
|
||||
val mini: O? = minInclusive()
|
||||
val mine: O? = minExclusive()
|
||||
val maxi: O? = maxInclusive()
|
||||
val maxe: O? = maxExclusive()
|
||||
val ordering = Ordering.natural<O>()
|
||||
val min = ordering.nullsLast().min<O>(mini, mine)
|
||||
val max = ordering.nullsFirst().max<O>(maxi, maxe)
|
||||
val lower =
|
||||
if (min == null) null else if (min === mini) BoundType.CLOSED else BoundType.OPEN
|
||||
val upper =
|
||||
if (max == null) null else if (max === maxi) BoundType.CLOSED else BoundType.OPEN
|
||||
return if (lower == null && upper == null) Range.all()
|
||||
else if (lower != null && upper == null) if (lower === BoundType.CLOSED) Range.atLeast(
|
||||
min
|
||||
) else Range.greaterThan(min)
|
||||
else if (lower == null && upper != null) if (upper === BoundType.CLOSED) Range.atMost(
|
||||
max
|
||||
) else Range.lessThan(max)
|
||||
else {
|
||||
Range.range(min, lower, max, upper)
|
||||
}
|
||||
}
|
||||
|
||||
fun <N : Number?> step(): N? {
|
||||
return map["step"] as N?
|
||||
}
|
||||
|
||||
fun <N : Number?> step(defaultValue: N): N {
|
||||
val n = map["step"] as N?
|
||||
return n ?: defaultValue
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepInt.
|
||||
* @return int
|
||||
*/
|
||||
fun stepInt(): Int {
|
||||
return step<Int>()!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepInt.
|
||||
* @param defaultValue int
|
||||
* @return int
|
||||
*/
|
||||
fun stepInt(defaultValue: Int): Int {
|
||||
return step(defaultValue)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepLong.
|
||||
* @return long
|
||||
*/
|
||||
fun stepLong(): Long {
|
||||
return step<Long>()!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getLong.
|
||||
* @param defaultValue long
|
||||
* @return long
|
||||
*/
|
||||
fun stepLong(defaultValue: Long): Long {
|
||||
return step(defaultValue)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepShort.
|
||||
* @return short
|
||||
*/
|
||||
fun stepShort(): Short {
|
||||
return step<Short>()!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepShort.
|
||||
* @param defaultValue short
|
||||
* @return short
|
||||
*/
|
||||
fun stepShort(defaultValue: Short): Short {
|
||||
return step(defaultValue)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepDouble.
|
||||
* @return double
|
||||
*/
|
||||
fun stepDouble(): Double {
|
||||
return step<Double>()!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepDouble.
|
||||
* @param defaultValue double
|
||||
* @return double
|
||||
*/
|
||||
fun stepDouble(defaultValue: Double): Double {
|
||||
return step(defaultValue)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepFloat.
|
||||
* @return float
|
||||
*/
|
||||
fun stepFloat(): Float {
|
||||
return step<Float>()!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method stepFloat.
|
||||
* @param defaultValue float
|
||||
* @return float
|
||||
*/
|
||||
fun stepFloat(defaultValue: Float): Float {
|
||||
return step(defaultValue)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method enumVals.
|
||||
* @return Iterable<Object>
|
||||
</Object> */
|
||||
fun enumeration(): Iterable<Any>? {
|
||||
return if (has("enumeration")) map["enumeration"] as Iterable<Any>?
|
||||
else ImmutableSet.of()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholder.
|
||||
* @return NLV
|
||||
*/
|
||||
fun placeholder(): NLV? {
|
||||
return map["placeholder"] as NLV?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholderString.
|
||||
* @return String
|
||||
*/
|
||||
fun placeholderString(): String? {
|
||||
return _nlv("placeholder")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholderString.
|
||||
* @param lang String
|
||||
* @return String
|
||||
*/
|
||||
fun placeholderString(lang: String?): String? {
|
||||
return _nlv("placeholder", lang)
|
||||
}
|
||||
|
||||
fun maxLength(): Int {
|
||||
val i = map["maxLength"] as Int?
|
||||
return i ?: -1
|
||||
}
|
||||
|
||||
fun minLength(): Int {
|
||||
val i = map["minLength"] as Int?
|
||||
return i ?: -1
|
||||
}
|
||||
|
||||
fun totalDigits(): Int {
|
||||
val i = map["totalDigits"] as Int?
|
||||
return i ?: -1
|
||||
}
|
||||
|
||||
fun fractionDigits(): Int {
|
||||
val i = map["fractionDigits"] as Int?
|
||||
return i ?: -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholder.
|
||||
* @return NLV
|
||||
*/
|
||||
fun displayName(): NLV? {
|
||||
return map["displayName"] as NLV?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholderString.
|
||||
* @return String
|
||||
*/
|
||||
fun displayNameString(): String? {
|
||||
return _nlv("displayName")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method placeholderString.
|
||||
* @param lang String
|
||||
* @return String
|
||||
*/
|
||||
fun displayNameString(lang: String?): String? {
|
||||
return _nlv("displayName", lang)
|
||||
}
|
||||
|
||||
fun language(): String? {
|
||||
return map["language"] as String?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method _nlv.
|
||||
* @param key String
|
||||
* @return String
|
||||
*/
|
||||
protected fun _nlv(key: String?): String? {
|
||||
val lang = language()
|
||||
return _nlv(key, lang ?: DEFAULT_LOCALE)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method _nlv.
|
||||
* @param key String
|
||||
* @param lang String
|
||||
* @return String
|
||||
*/
|
||||
protected fun _nlv(key: String?, lang: String?): String? {
|
||||
val nlv =
|
||||
(map[key] as NLV?)!!
|
||||
when (nlv.valueType()) {
|
||||
ValueType.SIMPLE -> {
|
||||
val sim =
|
||||
nlv as SimpleNLV
|
||||
val l = language()
|
||||
return if (l == null || Objects.equal(l, lang)) sim.value() else null
|
||||
}
|
||||
|
||||
ValueType.OBJECT -> {
|
||||
val map =
|
||||
nlv as MapNLV
|
||||
return map.value(lang)
|
||||
}
|
||||
|
||||
else -> return null
|
||||
}
|
||||
}
|
||||
|
||||
override fun valueType(): ValueType {
|
||||
return ValueType.OBJECT
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: Parameter) : Serializable {
|
||||
private val map = obj.map
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
val builder = Builder()
|
||||
builder.map.putAll(map)
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method makeParameter.
|
||||
* @return Builder
|
||||
*/
|
||||
fun makeParameter(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,231 @@
|
|||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.common.collect.Iterables
|
||||
import com.google.gson.*
|
||||
import com.ibm.common.activitystreams.*
|
||||
import com.ibm.common.activitystreams.actions.ParameterValue.SimpleParameterValue
|
||||
import com.ibm.common.activitystreams.i.Adapter
|
||||
import java.lang.reflect.Type
|
||||
|
||||
class ParameterAdapter
|
||||
|
||||
: Adapter<ParameterValue>() {
|
||||
override fun serialize(
|
||||
src: ParameterValue?,
|
||||
typeOfSrc: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement? {
|
||||
if (src == null) return null
|
||||
when (src.valueType()) {
|
||||
ValueType.OBJECT -> if (src is Parameter) {
|
||||
val parameter = src
|
||||
val obj = JsonObject()
|
||||
if (parameter.has("type")) {
|
||||
val type = parameter.type()
|
||||
if (type != null) obj.addProperty("type", type)
|
||||
}
|
||||
if (parameter.has("displayName")) {
|
||||
val nlv = parameter.displayName()
|
||||
if (nlv != null) obj.add("displayName", context.serialize(nlv, NLV::class.java))
|
||||
}
|
||||
if (parameter.has("placeholder")) {
|
||||
val nlv = parameter.placeholder()
|
||||
if (nlv != null) obj.add("placeholder", context.serialize(nlv, NLV::class.java))
|
||||
}
|
||||
if (parameter.has("enumeration")) {
|
||||
val e = parameter.enumeration()
|
||||
if (e != null) obj.add("enumeration", context.serialize(e, e.javaClass))
|
||||
}
|
||||
if (parameter.has("language")) {
|
||||
val lang = parameter.language()
|
||||
if (lang != null) obj.addProperty("language", lang)
|
||||
}
|
||||
if (parameter.has("maxInclusive")) {
|
||||
val max = parameter.maxInclusive<Any>()
|
||||
if (max != null) obj.add("maxInclusive", context.serialize(max, max.javaClass))
|
||||
}
|
||||
if (parameter.has("maxExclusive")) {
|
||||
val max = parameter.maxExclusive<Any>()
|
||||
if (max != null) obj.add("maxExclusive", context.serialize(max, max.javaClass))
|
||||
}
|
||||
if (parameter.has("minInclusive")) {
|
||||
val max = parameter.minInclusive<Any>()
|
||||
if (max != null) obj.add("minInclusive", context.serialize(max, max.javaClass))
|
||||
}
|
||||
if (parameter.has("minExclusive")) {
|
||||
val max = parameter.minExclusive<Any>()
|
||||
if (max != null) obj.add("minExclusive", context.serialize(max, max.javaClass))
|
||||
}
|
||||
if (parameter.has("pattern")) {
|
||||
val pattern = parameter.pattern()
|
||||
if (pattern != null) {
|
||||
if (Iterables.size(pattern) == 0) {
|
||||
val first = Iterables.getFirst(pattern, null)
|
||||
if (first != null) obj.addProperty("pattern", first)
|
||||
} else obj.add("pattern", context.serialize(pattern, pattern.javaClass))
|
||||
}
|
||||
}
|
||||
if (parameter.has("repeated")) {
|
||||
val repeated = parameter.repeated()
|
||||
if (repeated) obj.addProperty("repeated", repeated)
|
||||
}
|
||||
if (parameter.has("required")) {
|
||||
val required = parameter.required()
|
||||
if (!required) obj.addProperty("required", required)
|
||||
}
|
||||
if (parameter.has("step")) {
|
||||
val n = parameter.step<Number>()
|
||||
if (n != null) obj.add("step", context.serialize(n, n.javaClass))
|
||||
}
|
||||
if (parameter.has("default")) {
|
||||
val def = parameter.defaultValue<Any>()
|
||||
if (def != null) obj.add("default", context.serialize(def, def.javaClass))
|
||||
}
|
||||
if (parameter.has("minLength")) {
|
||||
val len = parameter.minLength()
|
||||
if (len > -1) obj.addProperty("minLength", len)
|
||||
}
|
||||
if (parameter.has("maxLength")) {
|
||||
val len = parameter.maxLength()
|
||||
if (len > -1) obj.addProperty("maxLength", len)
|
||||
}
|
||||
if (parameter.has("totalDigits")) {
|
||||
val len = parameter.totalDigits()
|
||||
if (len > -1) obj.addProperty("totalDigits", len)
|
||||
}
|
||||
if (parameter.has("fractionDigits")) {
|
||||
val len = parameter.fractionDigits()
|
||||
if (len > -1) obj.addProperty("fractionDigits", len)
|
||||
}
|
||||
if (parameter.has("value")) {
|
||||
val `val` = parameter.value<Any>()
|
||||
if (`val` != null) obj.add("value", context.serialize(`val`, `val`.javaClass))
|
||||
}
|
||||
return obj
|
||||
} else if (src is UrlTemplate) {
|
||||
return context.serialize(src, UrlTemplate::class.java)
|
||||
} else if (src is TypedPayload) {
|
||||
return context.serialize(src, TypedPayload::class.java)
|
||||
} else if (src is ASObject) {
|
||||
return context.serialize(src, ASObject::class.java)
|
||||
} else throw IllegalArgumentException()
|
||||
|
||||
ValueType.SIMPLE -> {
|
||||
val spv = src as SimpleParameterValue
|
||||
return context.serialize(spv.type(), String::class.java)
|
||||
}
|
||||
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
private fun deserialize(
|
||||
context: JsonDeserializationContext,
|
||||
el: JsonElement
|
||||
): Any? {
|
||||
if (el.isJsonArray) {
|
||||
return context.deserialize(el, Iterable::class.java)
|
||||
} else if (el.isJsonObject) {
|
||||
return context.deserialize(el, ASObject::class.java)
|
||||
} else if (el.isJsonPrimitive) {
|
||||
val p = el.asJsonPrimitive
|
||||
return if (p.isBoolean) p.asBoolean
|
||||
else if (p.isNumber) p.asNumber
|
||||
else p.asString
|
||||
} else return null
|
||||
}
|
||||
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
typeOfT: Type,
|
||||
context: JsonDeserializationContext
|
||||
): ParameterValue {
|
||||
Preconditions.checkArgument(json.isJsonPrimitive || json.isJsonObject)
|
||||
|
||||
if (json.isJsonPrimitive) {
|
||||
val jp = json.asJsonPrimitive
|
||||
Preconditions.checkArgument(jp.isString)
|
||||
return ActionMakers.parameter(jp.asString)
|
||||
} else {
|
||||
val obj = json.asJsonObject
|
||||
if (obj.has("objectType")) {
|
||||
val `as` = context.deserialize<ASObject>(obj, ASObject::class.java)
|
||||
Preconditions.checkArgument(`as` is ParameterValue)
|
||||
return `as` as ParameterValue
|
||||
} else {
|
||||
val builder =
|
||||
ActionMakers.parameter()
|
||||
if (obj.has("default")) builder.defaultValue(
|
||||
deserialize(context, obj["default"])
|
||||
)
|
||||
if (obj.has("displayName")) builder.displayName(
|
||||
context.deserialize<NLV>(
|
||||
obj["displayName"],
|
||||
NLV::class.java
|
||||
)
|
||||
)
|
||||
if (obj.has("enumeration")) builder.enumeration(
|
||||
context.deserialize<Iterable<*>>(
|
||||
obj["enumeration"],
|
||||
Iterable::class.java
|
||||
)
|
||||
)
|
||||
if (obj.has("fractionDigits")) builder.fractionDigits(
|
||||
obj["fractionDigits"].asInt
|
||||
)
|
||||
if (obj.has("language")) builder.language(
|
||||
obj["language"].asString
|
||||
)
|
||||
if (obj.has("maxExclusive")) builder.maxExclusive(
|
||||
deserialize(context, obj["maxExclusive"])
|
||||
)
|
||||
if (obj.has("maxInclusive")) builder.maxInclusive(
|
||||
deserialize(context, obj["maxInclusive"])
|
||||
)
|
||||
if (obj.has("minExclusive")) builder.minExclusive(
|
||||
deserialize(context, obj["minExclusive"])
|
||||
)
|
||||
if (obj.has("minInclusive")) builder.minInclusive(
|
||||
deserialize(context, obj["minInclusive"])
|
||||
)
|
||||
if (obj.has("maxLength")) builder.maxLength(
|
||||
obj["maxLength"].asInt
|
||||
)
|
||||
if (obj.has("minLength")) builder.minLength(
|
||||
obj["minLength"].asInt
|
||||
)
|
||||
if (obj.has("pattern")) builder.pattern(
|
||||
context.deserialize<Iterable<String>>(
|
||||
obj["pattern"], Iterable::class.java
|
||||
)
|
||||
)
|
||||
if (obj.has("placeholder")) builder.placeholder(
|
||||
context.deserialize<NLV>(
|
||||
obj["placeholder"], NLV::class.java
|
||||
)
|
||||
)
|
||||
if (obj.has("repeated") && obj["repeated"].asBoolean) builder.repeated()
|
||||
if (obj.has("required") && !obj["required"].asBoolean) builder.optional()
|
||||
if (obj.has("step")) builder.step(
|
||||
obj["step"].asNumber
|
||||
)
|
||||
if (obj.has("totalDigits")) builder.totalDigits(
|
||||
obj["totalDigits"].asInt
|
||||
)
|
||||
if (obj.has("type")) builder.type(
|
||||
obj["type"].asString
|
||||
)
|
||||
if (obj.has("value")) builder.value(
|
||||
deserialize(context, obj["value"])
|
||||
)
|
||||
return builder.get()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val instance: ParameterAdapter = ParameterAdapter()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Objects
|
||||
import com.ibm.common.activitystreams.*
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
|
||||
interface ParameterValue
|
||||
|
||||
: Writable, Serializable {
|
||||
fun valueType(): ValueType
|
||||
|
||||
fun toString(io: IO): String
|
||||
|
||||
class SimpleParameterValue
|
||||
private constructor(b: Builder) :
|
||||
AbstractWritable(b), ParameterValue {
|
||||
internal class Builder
|
||||
|
||||
: AbstractWritableBuilder<SimpleParameterValue?, Builder?>() {
|
||||
var type: String? = null
|
||||
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
}
|
||||
|
||||
fun type(type: String?): Builder {
|
||||
this.type = type
|
||||
return this
|
||||
}
|
||||
|
||||
override fun get(): SimpleParameterValue {
|
||||
return SimpleParameterValue(this)
|
||||
}
|
||||
}
|
||||
|
||||
private val type: String?
|
||||
|
||||
init {
|
||||
this.type = b.type
|
||||
}
|
||||
|
||||
override fun valueType(): ValueType {
|
||||
return ValueType.SIMPLE
|
||||
}
|
||||
|
||||
fun type(): String? {
|
||||
return type
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Objects.hashCode(type)
|
||||
}
|
||||
|
||||
override fun equals(obj: Any?): Boolean {
|
||||
if (this === obj) return true
|
||||
if (obj == null) return false
|
||||
if (javaClass != obj.javaClass) return false
|
||||
val other = obj as SimpleParameterValue
|
||||
return Objects.equal(type, other.type)
|
||||
}
|
||||
|
||||
override fun toString(io: IO): String {
|
||||
return super.toString(io)
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: SimpleParameterValue) : Serializable {
|
||||
private val type = obj.type
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
return Builder().type(type).get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.gson.*
|
||||
import com.ibm.common.activitystreams.i.Adapter
|
||||
import java.lang.reflect.Type
|
||||
|
||||
class ParametersAdapter
|
||||
|
||||
: Adapter<ParametersValue>() {
|
||||
override fun serialize(
|
||||
params: ParametersValue?,
|
||||
type: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement? {
|
||||
if (params == null) return null
|
||||
val obj = JsonObject()
|
||||
for (param in params) {
|
||||
obj.add(
|
||||
param,
|
||||
context.serialize(
|
||||
params.get(param),
|
||||
ParameterValue::class.java
|
||||
)
|
||||
)
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
type: Type,
|
||||
context: JsonDeserializationContext
|
||||
): ParametersValue {
|
||||
Preconditions.checkArgument(json.isJsonObject)
|
||||
val obj = json.asJsonObject
|
||||
val builder: ParametersValue.Builder =
|
||||
ParametersValue.Companion.make()
|
||||
for ((key, value) in obj.entrySet()) builder.param(
|
||||
key,
|
||||
context.deserialize<ParameterValue>(
|
||||
value,
|
||||
ParameterValue::class.java
|
||||
)
|
||||
)
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
val instance: ParametersAdapter = ParametersAdapter()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.google.common.collect.Maps
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
* The value of the "parameters" property...
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class ParametersValue
|
||||
internal constructor(builder: Builder) : AbstractWritable(builder),
|
||||
Serializable, Iterable<String?> {
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractWritableBuilder<ParametersValue?, Builder?>() {
|
||||
val params: MutableMap<String, Any?> = Maps.newHashMap()
|
||||
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
}
|
||||
|
||||
fun notEmpty(): Boolean {
|
||||
return !params.isEmpty()
|
||||
}
|
||||
|
||||
fun param(param: String, iri: String?): Builder {
|
||||
params[param] = ActionMakers.parameter(iri)
|
||||
return this
|
||||
}
|
||||
|
||||
fun param(
|
||||
param: String,
|
||||
parameter: ParameterValue?
|
||||
): Builder {
|
||||
params[param] = parameter
|
||||
return this
|
||||
}
|
||||
|
||||
fun param(
|
||||
param: String,
|
||||
parameter: Supplier<out ParameterValue?>?
|
||||
): Builder {
|
||||
params[param] = parameter
|
||||
return this
|
||||
}
|
||||
|
||||
override fun get(): ParametersValue {
|
||||
return ParametersValue(this)
|
||||
}
|
||||
}
|
||||
|
||||
private val params: ImmutableMap<String, Any?> =
|
||||
ImmutableMap.copyOf(builder.params)
|
||||
|
||||
override fun iterator(): MutableIterator<String> {
|
||||
return params.keys.iterator()
|
||||
}
|
||||
|
||||
fun <O : ParameterValue?> get(param: String?): O? {
|
||||
return params[param] as O?
|
||||
}
|
||||
|
||||
fun has(param: String?): Boolean {
|
||||
return params.containsKey(param)
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: ParametersValue) : Serializable {
|
||||
private val map = obj.params
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
val builder =
|
||||
make()
|
||||
builder.params.putAll(map)
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,155 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObject
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
* The value of the "parameters" property...
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class StylesValue
|
||||
|
||||
/**
|
||||
* Constructor for ParametersValue.
|
||||
* @param builder Builder
|
||||
*/
|
||||
internal constructor(builder: Builder) :
|
||||
AbstractDictionaryObject<String>(builder), Serializable {
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<String, StylesValue, Builder>() {
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method media.
|
||||
* @param query String
|
||||
* @return Builder
|
||||
*/
|
||||
fun media(query: String?): Builder {
|
||||
return super.set("media", query)!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method set.
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param name String
|
||||
* @param value String
|
||||
* @return Builder
|
||||
*/
|
||||
override fun set(name: String, value: String?): Builder {
|
||||
Preconditions.checkArgument(!name.equals("query", ignoreCase = true))
|
||||
return super.set(name, value)!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
*
|
||||
*
|
||||
* @return ParametersValue * @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
override fun get(): StylesValue {
|
||||
return StylesValue(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method media.
|
||||
* @return String
|
||||
*/
|
||||
fun media(): String? {
|
||||
return getSingle("media")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @param key String
|
||||
* @return String
|
||||
*/
|
||||
fun get(key: String?): String? {
|
||||
return super.getSingle(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @param key String
|
||||
* @param defaultValue String
|
||||
* @return String
|
||||
*/
|
||||
fun get(key: String?, defaultValue: String): String {
|
||||
return super.getSingle(key, defaultValue)
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: StylesValue) : Serializable {
|
||||
private val map: ImmutableMap<String, String?>
|
||||
|
||||
init {
|
||||
val builder =
|
||||
ImmutableMap.builder<String, String?>()
|
||||
for (key in obj) builder.put(key, obj.get(key))
|
||||
this.map = builder.build()
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
val builder =
|
||||
make()
|
||||
for ((key, value) in map) builder[key] =
|
||||
value
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method make.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,180 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Predicate
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.net.MediaType
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.LinkValue
|
||||
import com.ibm.common.activitystreams.TypeValue
|
||||
import java.io.ObjectStreamException
|
||||
|
||||
class TypedPayload
|
||||
|
||||
/**
|
||||
* Constructor for TypedPayload.
|
||||
* @param builder Builder
|
||||
*/
|
||||
private constructor(builder: Builder) : ASObject(builder),
|
||||
ParameterValue {
|
||||
/**
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<TypedPayload?, Builder?>() {
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
objectType("TypedPayload")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
*/
|
||||
fun schema(iri: String?): Builder? {
|
||||
return schema(LinkValue.SimpleLinkValue.make(iri))
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param lv LinkValue
|
||||
* @return Builder
|
||||
*/
|
||||
fun schema(lv: LinkValue?): Builder? {
|
||||
return link("schema", lv)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param lv Supplier
|
||||
* @return Builder
|
||||
*/
|
||||
fun schema(lv: Supplier<out LinkValue?>): Builder? {
|
||||
return schema(lv.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
*/
|
||||
fun type(iri: String?): Builder? {
|
||||
return type(TypeValue.SimpleTypeValue.make(iri))
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @param tv TypeValue
|
||||
* @return Builder
|
||||
*/
|
||||
fun type(tv: TypeValue?): Builder? {
|
||||
return set("type", tv)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @param tv Supplier
|
||||
* @return Builder
|
||||
*/
|
||||
fun type(tv: Supplier<out TypeValue?>): Builder? {
|
||||
return type(tv.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return TypedPayload
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): TypedPayload {
|
||||
return TypedPayload(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @return L
|
||||
*/
|
||||
fun schema(): Iterable<LinkValue> {
|
||||
return this.links("schema")
|
||||
}
|
||||
|
||||
fun schema(filter: Predicate<in LinkValue?>): Iterable<LinkValue?> {
|
||||
return this.links("schema", filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @return TypeValue
|
||||
*/
|
||||
fun type(): TypeValue? {
|
||||
return this.get<TypeValue>("type")
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
override fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: TypedPayload) : AbstractSerializedForm<TypedPayload?>(obj) {
|
||||
private val mediaType = obj.mediaType().toString()
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any? {
|
||||
return super.doReadResolve()
|
||||
}
|
||||
|
||||
override fun builder(): Builder {
|
||||
return ActionMakers.typedPayload(mediaType)!!
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -2060301713159936285L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method makeTypedPayload.
|
||||
* @param mediaType MediaType
|
||||
* @return Builder
|
||||
*/
|
||||
fun makeTypedPayload(mediaType: MediaType?): Builder? {
|
||||
return Builder().mediaType(mediaType)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method makeTypedPayload.
|
||||
* @param mediaType String
|
||||
* @return Builder
|
||||
*/
|
||||
fun makeTypedPayload(mediaType: String): Builder? {
|
||||
return Builder().mediaType(mediaType)
|
||||
}
|
||||
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,132 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.actions
|
||||
|
||||
import com.google.common.base.Supplier
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import java.io.ObjectStreamException
|
||||
|
||||
/**
|
||||
*/
|
||||
class UrlTemplate
|
||||
|
||||
/**
|
||||
* Constructor for UrlTemplate.
|
||||
* @param builder Builder
|
||||
*/
|
||||
private constructor(builder: Builder) : ASObject(builder),
|
||||
ParameterValue {
|
||||
/**
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<UrlTemplate?, Builder?>() {
|
||||
private val params: ParametersValue.Builder = ParametersValue.Companion.make()
|
||||
|
||||
init {
|
||||
writeUsing(ActionMakers.io)
|
||||
objectType("UrlTemplate")
|
||||
}
|
||||
|
||||
fun template(template: String?): Builder {
|
||||
set("template", template)
|
||||
return this
|
||||
}
|
||||
|
||||
fun parameter(name: String, iri: String?): Builder {
|
||||
params.param(name, iri)
|
||||
return this
|
||||
}
|
||||
|
||||
fun parameter(
|
||||
name: String,
|
||||
parameter: ParameterValue?
|
||||
): Builder {
|
||||
params.param(name, parameter)
|
||||
return this
|
||||
}
|
||||
|
||||
fun parameter(
|
||||
name: String,
|
||||
parameter: Supplier<out ParameterValue?>
|
||||
): Builder {
|
||||
return parameter(name, parameter.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return UrlTemplate
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): UrlTemplate {
|
||||
if (params.notEmpty()) set("parameters", params.get())
|
||||
return UrlTemplate(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parameters.
|
||||
* @return ParametersValue
|
||||
*/
|
||||
fun parameters(): ParametersValue? {
|
||||
return this.get<ParametersValue>("parameters")
|
||||
}
|
||||
|
||||
/**
|
||||
* Method template.
|
||||
* @return String
|
||||
*/
|
||||
fun template(): String? {
|
||||
return this.getString("template")
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
override fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: UrlTemplate?) : AbstractSerializedForm<UrlTemplate?>(obj) {
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any? {
|
||||
return super.doReadResolve()
|
||||
}
|
||||
|
||||
override fun builder(): Builder {
|
||||
return ActionMakers.urlTemplate()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -2060301713159936285L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method makeUrlTemplate.
|
||||
* @return Builder
|
||||
*/
|
||||
fun makeUrlTemplate(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
<assembly>
|
||||
<id>bin</id>
|
||||
<formats>
|
||||
<format>tar.gz</format>
|
||||
<format>zip</format>
|
||||
</formats>
|
||||
<includeBaseDirectory>false</includeBaseDirectory>
|
||||
<dependencySets>
|
||||
<dependencySet>
|
||||
<outputDirectory></outputDirectory>
|
||||
<includes>
|
||||
<include>*</include>
|
||||
</includes>
|
||||
<unpack>false</unpack>
|
||||
</dependencySet>
|
||||
</dependencySets>
|
||||
</assembly>
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.ibm.common</groupId>
|
||||
<artifactId>activitystreams</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>assembly</artifactId>
|
||||
<name>Activity Streams 2.0 - Assembly</name>
|
||||
<packaging>pom</packaging>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ibm.common</groupId>
|
||||
<artifactId>activitystreams-core</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ibm.common</groupId>
|
||||
<artifactId>activitystreams-actions</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ibm.common</groupId>
|
||||
<artifactId>activitystreams-geo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ibm.common</groupId>
|
||||
<artifactId>activitystreams-legacy</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.2.2</version>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>assembly.xml</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
kotlin("jvm") version "2.0.21"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(libs.com.google.code.gson.gson)
|
||||
api(libs.com.google.guava.guava)
|
||||
api(libs.joda.time.joda.time)
|
||||
testImplementation(libs.junit.junit)
|
||||
implementation(kotlin("stdlib-jdk8"))
|
||||
}
|
||||
|
||||
description = "Activity Streams 2.0 - Core"
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
kotlin {
|
||||
jvmToolchain(11)
|
||||
}
|
||||
tasks.test {
|
||||
useJUnit()
|
||||
}
|
||||
131
core/pom.xml
131
core/pom.xml
|
|
@ -1,131 +0,0 @@
|
|||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>com.ibm.common</groupId>
|
||||
<artifactId>activitystreams</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>activitystreams-core</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Activity Streams 2.0 - Core</name>
|
||||
<url>http://github.org/opensocial/activitystreams</url>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
</dependency>
|
||||
<!-- Test Dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.9.1</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<docencoding>UTF-8</docencoding>
|
||||
<charset>UTF-8</charset>
|
||||
<additionalparam>-XDignore.symbol.file</additionalparam>
|
||||
<show>public</show>
|
||||
<links>
|
||||
<link>http://www.joda.org/joda-time/apidocs</link>
|
||||
<link>http://docs.guava-libraries.googlecode.com/git-history/v16.0.1/javadoc/</link>
|
||||
</links>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.3.1</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>2.3.7</version>
|
||||
<extensions>true</extensions>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>bundle-manifest</id>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>manifest</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Export-Package>com.ibm.common.activitystreams.*</Export-Package>
|
||||
<Import-Package>
|
||||
com.google.gson.*,
|
||||
com.google.common.*,
|
||||
org.joda.time.*
|
||||
</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.2.2</version>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>assembly.xml</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
</project>
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -1,234 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObject;
|
||||
|
||||
/**
|
||||
* <p>The value of the "actions" property... used to map potential verbs
|
||||
* with objects capable of "handling" those verbs. Each of the keys
|
||||
* must be a valid verb (either a token or an absolute IRI). </p>
|
||||
*
|
||||
* <p>Specifying Actions while creating AS Objects:</p>
|
||||
*
|
||||
* <pre>
|
||||
* import static com.ibm.common.activitystreams.Makers.object;
|
||||
* import static com.ibm.common.activitystreams.Makers.actions;
|
||||
* import static com.ibm.common.activitystreams.actions.ActionMakers.intentAction;
|
||||
* ...
|
||||
*
|
||||
* ASObject obj = object()
|
||||
* .action("like", "http://example.org/like-action")
|
||||
* .action("follow", intentAction())
|
||||
* .get();
|
||||
* </pre>
|
||||
*
|
||||
* <p>Accessing Actions when consuming AS Objects:</p>
|
||||
*
|
||||
* <pre>
|
||||
* ActionsValue actions = obj.actions();
|
||||
* for (LinkValue lv : actions.get("like")) {
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>The Actions object itself is a JSON Dictonary that maps
|
||||
* verb identifiers to a Link Value that contains one or more
|
||||
* Action Handlers. A call to the get(...) method returns an
|
||||
* Iterable of Simple or Object LinkValue instances representing
|
||||
* each of the associated Action Handlers.</p>
|
||||
*
|
||||
* <p>A Serialized Activity Stream object with Actions:
|
||||
* <pre>
|
||||
* {
|
||||
* "objectType": "note",
|
||||
* "displayName": "My Note",
|
||||
* "actions": {
|
||||
* "like": "http://example.org/like-action",
|
||||
* "follow": {
|
||||
* "objectType": "IntentActionHandler",
|
||||
* "displayName": "Follow updates to this note!"
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public final class ActionsValue
|
||||
extends AbstractDictionaryObject<LinkValue>
|
||||
implements Serializable {
|
||||
|
||||
/**
|
||||
* Builder for ActionsValue objects.
|
||||
* <pre>
|
||||
* Makers.actions()
|
||||
* .set("like", "http://example.org/like-action")
|
||||
* .set("follow", ActionMakers.intentAction())
|
||||
* .get();
|
||||
* </pre>
|
||||
*/
|
||||
public static final class Builder
|
||||
extends AbstractDictionaryObject.AbstractBuilder
|
||||
<LinkValue, ActionsValue, Builder> {
|
||||
|
||||
protected Builder() {}
|
||||
|
||||
/**
|
||||
* Add an action handler for the given verb.
|
||||
* Calling this multiple times results in
|
||||
* multiple action handlers.
|
||||
* @param verb String
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder set(String verb, String iri) {
|
||||
return set(
|
||||
verb,
|
||||
LinkValue.SimpleLinkValue.make(iri));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder set(String key, LinkValue x) {
|
||||
return super.link(key, x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder set(String key, Supplier<? extends LinkValue> x) {
|
||||
return super.link(key, x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the built ActionsValue instance
|
||||
* @return ParametersValue
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
**/
|
||||
public ActionsValue get() {
|
||||
return new ActionsValue(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private ActionsValue(Builder builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Action Handlers associated with the given verb.
|
||||
* @param verb String
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> get(String verb) {
|
||||
return super.getIterable(verb);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Action Handlers associated with the given verb
|
||||
* that satisfy the given filter
|
||||
* @param verb String
|
||||
* @param filter Predicate<LinkValue>
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> get(
|
||||
String verb,
|
||||
Predicate<LinkValue> filter) {
|
||||
return super.getIterable(verb, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Action Handlers associated with the given verb,
|
||||
* transformed using the given Transform function
|
||||
* @param verb String
|
||||
* @param transform Function<LinkValue,Y>
|
||||
* @return java.util.Iterable<Y>
|
||||
*/
|
||||
public <Y> Iterable<Y> get(
|
||||
String verb,
|
||||
Function<LinkValue, Y> transform) {
|
||||
return super.getIterable(verb, transform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Action Handlers associated with the given verb,
|
||||
* filtered using the given Predicate and transformed
|
||||
* using the given Transform function.
|
||||
* @param verb String
|
||||
* @param filter Predicate<LinkValue>
|
||||
* @param transform Function<LinkValue,Y>
|
||||
* @return java.util.Iterable<Y>
|
||||
*/
|
||||
public <Y> Iterable<Y> get(
|
||||
String verb,
|
||||
Predicate<LinkValue> filter,
|
||||
Function<LinkValue, Y> transform) {
|
||||
return super.getIterable(verb, filter, transform);
|
||||
}
|
||||
|
||||
/**
|
||||
* True if there is at least one Action Handler associated
|
||||
* with the given verb
|
||||
* @param verb String
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean hasAtLeastOne(String verb) {
|
||||
return super.hasAtLeastOne(verb);
|
||||
}
|
||||
|
||||
// Java serialization support
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private ImmutableMap<String,Object> map;
|
||||
SerializedForm(ActionsValue obj) {
|
||||
ImmutableMap.Builder<String,Object> builder =
|
||||
ImmutableMap.builder();
|
||||
for (String key : obj)
|
||||
builder.put(key, obj.get(key));
|
||||
this.map = builder.build();
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
ActionsValue.Builder builder =
|
||||
Makers.actions();
|
||||
for (Map.Entry<String,Object> entry : map.entrySet()) {
|
||||
Iterable<LinkValue> list = (Iterable<LinkValue>) entry.getValue();
|
||||
for (LinkValue link : list)
|
||||
builder.set(entry.getKey(), link);
|
||||
}
|
||||
return builder.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,853 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static java.lang.Math.floor;
|
||||
import static java.lang.Math.ceil;
|
||||
import static com.ibm.common.activitystreams.Makers.linkValue;
|
||||
import static com.ibm.common.activitystreams.Makers.type;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Supplier;
|
||||
|
||||
|
||||
/**
|
||||
* An Activity Streams 2.0 Activity
|
||||
*
|
||||
* <p>Creating an Activity:</p>
|
||||
* <pre>
|
||||
* import static com.ibm.common.activitystreams.Makers.activity;
|
||||
* import static com.ibm.common.activitystreams.Makers.object;
|
||||
* ...
|
||||
*
|
||||
* Activity activity = activity()
|
||||
* .actor("acct:joe@example.org")
|
||||
* .verb("post")
|
||||
* .object(object().content("This is a note"))
|
||||
* .get();
|
||||
* </pre>
|
||||
*
|
||||
* <p>Consuming an Activity:</p>
|
||||
* <pre>
|
||||
* Activity activity = IO.makeDefault().readAsActivity(...);
|
||||
*
|
||||
* System.out.println(activity.verbAsString());
|
||||
* ASObject actor = activity.firstActor();
|
||||
* ASObject object = activity.firstObject();
|
||||
* </pre>
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public class Activity
|
||||
extends ASObject
|
||||
implements Serializable {
|
||||
|
||||
public static enum Audience {
|
||||
/**
|
||||
* Primary public audience
|
||||
**/
|
||||
TO,
|
||||
/**
|
||||
* Primary private audience
|
||||
**/
|
||||
BTO,
|
||||
/**
|
||||
* Secondary public audience
|
||||
**/
|
||||
CC,
|
||||
/**
|
||||
* Secondary private audience
|
||||
**/
|
||||
BCC,
|
||||
/**
|
||||
* Public origin
|
||||
**/
|
||||
FROM,
|
||||
/**
|
||||
* Private origin
|
||||
**/
|
||||
BFROM;
|
||||
|
||||
private final String label;
|
||||
|
||||
private Audience() {
|
||||
this.label = name().toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the "from" target(s)
|
||||
* @return java.util.Iterable<LinkValue>*/
|
||||
private Iterable<LinkValue> get(
|
||||
Activity activity) {
|
||||
return activity.links(label);
|
||||
}
|
||||
|
||||
private Iterable<LinkValue> get(
|
||||
Activity activity,
|
||||
Predicate<? super LinkValue> filter) {
|
||||
return activity.links(label, filter);
|
||||
}
|
||||
|
||||
private LinkValue first(Activity activity) {
|
||||
return activity.firstLink(label);
|
||||
}
|
||||
|
||||
private LinkValue firstMatching(
|
||||
Activity activity,
|
||||
Predicate<? super LinkValue> test) {
|
||||
return activity.firstMatchingLink(label, test);
|
||||
}
|
||||
}
|
||||
|
||||
public static enum Status {
|
||||
/** The Activity is tentatively scheduled to begin **/
|
||||
TENTATIVE,
|
||||
/** The Activity is scheduled to begin **/
|
||||
PENDING,
|
||||
/** The Activity has been completed **/
|
||||
COMPLETED,
|
||||
/** The Activity has been canceled or aborted **/
|
||||
CANCELED,
|
||||
/** The Activity is currently active **/
|
||||
ACTIVE,
|
||||
/** The Activity has been voided. **/
|
||||
VOIDED,
|
||||
OTHER
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Builder for concrete Activity object instances.
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public static final class Builder
|
||||
extends Activity.AbstractBuilder<Activity,Activity.Builder> {
|
||||
|
||||
/**
|
||||
* Get the built Activity object
|
||||
* @return Activity
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
public Activity get() {
|
||||
return new Activity(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the priority value is within the acceptable range (0.0-1.0)
|
||||
* @param d double
|
||||
* @return double
|
||||
*/
|
||||
private static double checkPriority(double d) {
|
||||
checkArgument(floor(d) >= 0 && ceil(d) >= 1);
|
||||
return d;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract builder for Activity classes. This is defined this way
|
||||
* to make it easier to build extensions of the Activity class.
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static abstract class AbstractBuilder
|
||||
<A extends Activity, B extends AbstractBuilder<A,B>>
|
||||
extends ASObject.AbstractBuilder<A, B> {
|
||||
|
||||
/**
|
||||
* Set the Activity status property.
|
||||
* @param status Status
|
||||
* @return B
|
||||
*/
|
||||
public B status(Status status) {
|
||||
if (status != null)
|
||||
set("status", status);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "voided"
|
||||
* @return B
|
||||
*/
|
||||
public B voided() {
|
||||
return status(Status.VOIDED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "tentative"
|
||||
* @return B
|
||||
*/
|
||||
public B tentative() {
|
||||
return status(Status.TENTATIVE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "pending"
|
||||
* @return B
|
||||
*/
|
||||
public B pending() {
|
||||
return status(Status.PENDING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "active"
|
||||
* @return B
|
||||
*/
|
||||
public B active() {
|
||||
return status(Status.ACTIVE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "canceled"
|
||||
* @return B
|
||||
*/
|
||||
public B canceled() {
|
||||
return status(Status.CANCELED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "completed"
|
||||
* @return B
|
||||
*/
|
||||
public B completed() {
|
||||
return status(Status.COMPLETED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the verb for this activity
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
public B verb(String iri) {
|
||||
set("verb", type(iri));
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the verb for this activity
|
||||
* @param tv TypeValue
|
||||
* @return B
|
||||
*/
|
||||
public B verb(TypeValue tv) {
|
||||
set("verb", tv);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the verb for this activity
|
||||
* @param tv Supplier<? extends TypeValue>
|
||||
* @return B
|
||||
*/
|
||||
public B verb(Supplier<? extends TypeValue> tv) {
|
||||
return verb(tv.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a participant that indirectly contributed to the activity.
|
||||
* @param url String
|
||||
* @return B
|
||||
**/
|
||||
public B participant(String url, String... urls) {
|
||||
if (url != null)
|
||||
link("participant", linkValue(url));
|
||||
if (urls != null)
|
||||
for (String u : urls)
|
||||
link("participant", linkValue(u));
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a participant that indirectly contributed to the activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B participant(LinkValue link, LinkValue... links) {
|
||||
if (link != null)
|
||||
link("participant", link);
|
||||
if (links != null)
|
||||
for (LinkValue l : links)
|
||||
link("participant", l);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a participant that indirectly contributed to the activity
|
||||
* @param link Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B participant(Supplier<? extends LinkValue> link) {
|
||||
return link("participant", link.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instrument used to complete the activity
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
public B instrument(String url, String... urls) {
|
||||
if (url != null)
|
||||
link("instrument", linkValue(url));
|
||||
if (urls != null)
|
||||
for (String u : urls)
|
||||
link("instument", linkValue(u));
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instrument used to complete the activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B instrument(LinkValue link, LinkValue... links) {
|
||||
if (link != null)
|
||||
link("instrument", link);
|
||||
if (links != null)
|
||||
for (LinkValue l : links)
|
||||
link("instrument", l);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instrument used to complete the activity
|
||||
* @param link Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B instrument(Supplier<? extends LinkValue> link) {
|
||||
return link("instrument", link.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the primary actor/agent for this activity
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
public B actor(String url, String... urls) {
|
||||
if (url != null)
|
||||
link("actor", linkValue(url));
|
||||
if (urls != null)
|
||||
for (String u : urls)
|
||||
link("actor", linkValue(u));
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the primary actor/agent for this activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B actor(LinkValue link, LinkValue... links) {
|
||||
if (link != null)
|
||||
link("actor", link);
|
||||
if (links != null)
|
||||
for (LinkValue l : links)
|
||||
link("actor", l);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the actor
|
||||
* @param link Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B actor(Supplier<? extends LinkValue> link) {
|
||||
return link("actor", link.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the direct object of this activity (the object that has been
|
||||
* or will be acted upon)
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
public B object(String url, String... urls) {
|
||||
if (url != null)
|
||||
link("object", linkValue(url));
|
||||
if (urls != null)
|
||||
for (String u : urls)
|
||||
link("object", linkValue(u));
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the direct object of this activity (the object that has been
|
||||
* or will be acted upon)
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B object(LinkValue link, LinkValue... links) {
|
||||
if (link != null)
|
||||
link("object", link);
|
||||
if (links != null)
|
||||
for (LinkValue l : links)
|
||||
link("object", l);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the direct object of this activity (the object that has been
|
||||
* or will be acted upon)
|
||||
* @param link Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B object(Supplier<? extends LinkValue> link) {
|
||||
return link("object", link.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that is indirectly affected by the activity
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
public B target(String url, String... urls) {
|
||||
if (url != null)
|
||||
link("target", linkValue(url));
|
||||
if (urls != null)
|
||||
for (String u : urls)
|
||||
link("target", linkValue(u));
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that is indirectly affected by the activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B target(LinkValue link, LinkValue... links) {
|
||||
if (link != null)
|
||||
link("target", link);
|
||||
if (links != null)
|
||||
for (LinkValue l : links)
|
||||
link("target", l);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that is indirectly affected by the activity
|
||||
* @param link Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B target(Supplier<? extends LinkValue> link) {
|
||||
return link("target", link.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a URI that describes the result of this activity
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
public B result(String url, String... urls) {
|
||||
if (url != null)
|
||||
link("result", linkValue(url));
|
||||
if (urls != null)
|
||||
for (String u : urls)
|
||||
link("result", linkValue(u));
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that describes the result of this activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
public B result(LinkValue link, LinkValue... links) {
|
||||
if (link != null)
|
||||
link("result", link);
|
||||
if (links != null)
|
||||
for (LinkValue l : links)
|
||||
link("result", l);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that describes the result of this activity
|
||||
* @param link Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
*/
|
||||
public B result(Supplier<? extends LinkValue> link) {
|
||||
return link("result", link.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the audience for this activity
|
||||
* @param audience
|
||||
* @param url
|
||||
* @param urls
|
||||
* @return B
|
||||
*/
|
||||
public B audience(Audience audience, String url, String... urls) {
|
||||
link(audience.label, linkValue(url));
|
||||
for (String u : urls)
|
||||
link(audience.label, u);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the audience for this activity
|
||||
* @param audience
|
||||
* @param link
|
||||
* @param links
|
||||
* @return B
|
||||
*/
|
||||
public B audience(Audience audience, LinkValue link, LinkValue... links) {
|
||||
link(audience.label, link);
|
||||
for (LinkValue lv : links)
|
||||
link(audience.label, lv);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the audience for this activity
|
||||
* @param audience
|
||||
* @param link
|
||||
* @return B
|
||||
*/
|
||||
public B audience(Audience audience, Supplier<? extends LinkValue> link) {
|
||||
return link(audience.label, link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the priority as value in the range 0.00 to 1.00.
|
||||
* Value will be checked to ensure it is within the valid range.
|
||||
* The value SHOULD be limited to two decimal places but the
|
||||
* number of decimals will not be checked.
|
||||
* @param d double
|
||||
* @return B
|
||||
* @throws IllegalArgumentException if the value is not within 0.00-1.00
|
||||
*/
|
||||
public B priority(double d) {
|
||||
return set("priority", checkPriority(d));
|
||||
}
|
||||
}
|
||||
|
||||
Activity(Activity.AbstractBuilder<?, ?> builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the verb as TypeValue object
|
||||
* @return T
|
||||
**/
|
||||
public <T extends TypeValue>T verb() {
|
||||
return this.<T>get("verb");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the verb as a string
|
||||
* @return String
|
||||
**/
|
||||
public String verbString() {
|
||||
return typeValueAsString("verb");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the participant(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
**/
|
||||
public Iterable<LinkValue> participant() {
|
||||
return links("participant");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching participant(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> participant(Predicate<? super LinkValue> filter) {
|
||||
return links("participant", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first paticipant
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstParticipant() {
|
||||
return firstLink("participant");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching participant
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstMatchingParticipant(Predicate<? super LinkValue> test) {
|
||||
return firstMatchingLink("participan", test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the instrument(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
**/
|
||||
public Iterable<LinkValue> instrument() {
|
||||
return links("instrument");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching instrument(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> instrument(Predicate<? super LinkValue> filter) {
|
||||
return links("instrument", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first instrument
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstInstrument() {
|
||||
return firstLink("instrument");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching instrument
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstMatchingInstrument(Predicate<? super LinkValue> test) {
|
||||
return firstMatchingLink("instrument", test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the actor(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
**/
|
||||
public Iterable<LinkValue> actor() {
|
||||
return links("actor");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching actor(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> actor(Predicate<? super LinkValue> filter) {
|
||||
return links("actor", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first actor
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstActor() {
|
||||
return firstLink("actor");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching actor
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstMatchingActor(Predicate<? super LinkValue> test) {
|
||||
return firstMatchingLink("actor", test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
**/
|
||||
public Iterable<LinkValue> object() {
|
||||
return links("object");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching object(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> object(Predicate<? super LinkValue> filter) {
|
||||
return links("object", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first object
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstObject() {
|
||||
return firstLink("object");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching object
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstMatchingObject(Predicate<? super LinkValue> test) {
|
||||
return firstMatchingLink("object", test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the target(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
**/
|
||||
public Iterable<LinkValue> target() {
|
||||
return links("target");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching target(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> target(Predicate<? super LinkValue> filter) {
|
||||
return links("target", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first target
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstTarget() {
|
||||
return firstLink("target");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching target
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstMatchingTarget(Predicate<? super LinkValue> test) {
|
||||
return firstMatchingLink("target", test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
**/
|
||||
public Iterable<LinkValue> result() {
|
||||
return links("result");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the matching result(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> result(Predicate<? super LinkValue> filter) {
|
||||
return links("result", filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first result
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstResult() {
|
||||
return firstLink("result");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching result
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstMatchingResult(Predicate<? super LinkValue> test) {
|
||||
return firstMatchingLink("result", test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the priority.
|
||||
* @return double
|
||||
* @throws IllegalArgumentException if the priority is not within
|
||||
* the range 0.00-1.00
|
||||
**/
|
||||
public double priority() {
|
||||
return checkPriority(getDouble("priority"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the audience for this activity
|
||||
* @param audience
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> audience(
|
||||
Audience audience) {
|
||||
return audience.get(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the audience for this activity
|
||||
* @param audience
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> audience(
|
||||
Audience audience,
|
||||
Predicate<? super LinkValue> filter) {
|
||||
return audience.get(this,filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first audience for this activity
|
||||
* @param audience
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstAudience(Audience audience) {
|
||||
return audience.first(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching audience for this activity
|
||||
* @param audience
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstMatchingAudience(
|
||||
Audience audience,
|
||||
Predicate<? super LinkValue> test) {
|
||||
return audience.firstMatching(this, test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the status of this activity
|
||||
* @return Status
|
||||
*/
|
||||
public Status status() {
|
||||
return get("status");
|
||||
}
|
||||
|
||||
// Java Serialization Support
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
extends AbstractSerializedForm<Activity> {
|
||||
protected SerializedForm(Activity obj) {
|
||||
super(obj);
|
||||
}
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
protected Activity.Builder builder() {
|
||||
return Makers.activity();
|
||||
}
|
||||
Object readResolve() throws ObjectStreamException {
|
||||
return super.doReadResolve();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,540 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.ibm.common.activitystreams.Makers.linkValue;
|
||||
import static com.google.common.collect.Iterables.filter;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.ReadableDuration;
|
||||
import org.joda.time.ReadablePeriod;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
/**
|
||||
*
|
||||
* An Activity Streams collection object
|
||||
*
|
||||
* <pre>
|
||||
* import static com.ibm.common.activitystreams.Makers.collection;
|
||||
* import static com.ibm.common.activitystreams.Makers.object;
|
||||
* ...
|
||||
*
|
||||
* Collection collection = collection()
|
||||
* .items(object()...)
|
||||
* .items(object()...)
|
||||
* .get();
|
||||
* </pre>
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class Collection
|
||||
extends ASObject
|
||||
implements Serializable {
|
||||
|
||||
/** Standardized Paging Links **/
|
||||
public static enum Page {
|
||||
/**
|
||||
* Reference to the first page in the logical set
|
||||
**/
|
||||
FIRST,
|
||||
/**
|
||||
* Reference to the next page in the logical set
|
||||
**/
|
||||
NEXT,
|
||||
/**
|
||||
* Reference to the final page in the logical set
|
||||
* **/
|
||||
LAST,
|
||||
/**
|
||||
* Reference to the previous page in the logical set
|
||||
**/
|
||||
PREVIOUS,
|
||||
/**
|
||||
* Reference to the previous page in the logical set (treated as an alias for "previous")
|
||||
* @deprecated Use Page.PREVIOUS instead
|
||||
**/
|
||||
PREV,
|
||||
/**
|
||||
* Reference to the page containing the most recently published/updated
|
||||
* items in the logical set
|
||||
**/
|
||||
CURRENT,
|
||||
/**
|
||||
* Reference to this page
|
||||
**/
|
||||
SELF;
|
||||
|
||||
private final String label;
|
||||
private Page() {
|
||||
this.label = name().toLowerCase();
|
||||
}
|
||||
|
||||
private String checkPrev(Collection col, String or) {
|
||||
if (this == PREVIOUS) {
|
||||
if (!col.has(label) && col.has(PREV.label))
|
||||
return PREV.label;
|
||||
} else if (this == PREV) {
|
||||
if (!col.has(label) && col.has(PREVIOUS.label))
|
||||
return PREVIOUS.label;
|
||||
}
|
||||
return or;
|
||||
}
|
||||
|
||||
private Iterable<LinkValue> links(Collection col) {
|
||||
return col.links(checkPrev(col,label));
|
||||
}
|
||||
|
||||
private Iterable<LinkValue> links(
|
||||
Collection col,
|
||||
Predicate<? super LinkValue> filter) {
|
||||
return col.links(checkPrev(col,label), filter);
|
||||
}
|
||||
|
||||
private LinkValue first(Collection col) {
|
||||
return col.firstLink(checkPrev(col,label));
|
||||
}
|
||||
|
||||
private LinkValue firstMatching(
|
||||
Collection col,
|
||||
Predicate<? super LinkValue> filter) {
|
||||
return col.firstMatchingLink(checkPrev(col,label), filter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public static final class Builder
|
||||
extends Collection.AbstractBuilder<Collection,Collection.Builder> {
|
||||
|
||||
/**
|
||||
* Method create.
|
||||
* @return Collection */
|
||||
protected Collection create() {
|
||||
return new Collection(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public static abstract class AbstractBuilder
|
||||
<A extends Collection, B extends Collection.AbstractBuilder<A,B>>
|
||||
extends ASObject.AbstractBuilder<A, B> {
|
||||
|
||||
protected final ImmutableList.Builder<ASObject> list =
|
||||
ImmutableList.builder();
|
||||
|
||||
/**
|
||||
* Method create.
|
||||
* @return A
|
||||
**/
|
||||
protected abstract A create();
|
||||
|
||||
@Override
|
||||
public B set(String key, Object value) {
|
||||
if (key.equals("items")) {
|
||||
if (value instanceof ArrayLinkValue) {
|
||||
ArrayLinkValue alv = (ArrayLinkValue) value;
|
||||
for (LinkValue lv : alv) {
|
||||
list.add((ASObject)lv);
|
||||
}
|
||||
} else if (value instanceof ASObject)
|
||||
list.add((ASObject) value);
|
||||
return (B)this;
|
||||
} else return super.set(key,value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return A
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
**/
|
||||
public A get() {
|
||||
super.set("items", list.build());
|
||||
return create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add items to this collection
|
||||
* @param objs java.util.Iterable<? extends ASObject>
|
||||
* @return B
|
||||
*/
|
||||
public B items(Iterable<? extends ASObject> objs) {
|
||||
if (objs == null)
|
||||
return (B)this;
|
||||
for (ASObject obj : objs)
|
||||
items(obj);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add items to this collection
|
||||
* @param obj ASObject The object to add
|
||||
* @param objs ASObject[] Additional objects to add (vararg)
|
||||
* @return B
|
||||
**/
|
||||
public B items(ASObject obj, ASObject... objs) {
|
||||
if (obj == null)
|
||||
return (B)this;
|
||||
list.add(obj);
|
||||
if (objs != null)
|
||||
for (ASObject o : objs)
|
||||
list.add(o);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to this collection
|
||||
* @param obj com.google.common.base.Supplier<? extends ASObject>
|
||||
* @return B
|
||||
**/
|
||||
public B items(Supplier<? extends ASObject> obj) {
|
||||
if (obj == null)
|
||||
return (B)this;
|
||||
items(obj.get());
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the total number of items (must be non-negative)
|
||||
* @param i int
|
||||
* @return B
|
||||
**/
|
||||
public B totalItems(int i) {
|
||||
checkArgument(i >= 0);
|
||||
set("totalItems", i);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the number of items per page (must be non-negative)
|
||||
* @param i int
|
||||
* @return B
|
||||
**/
|
||||
public B itemsPerPage(int i) {
|
||||
checkArgument(i >= 0);
|
||||
set("itemsPerPage", i);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the starting index (must be non-negative)
|
||||
* @param i int
|
||||
* @return B
|
||||
**/
|
||||
public B startIndex(int i) {
|
||||
checkArgument(i >= 0);
|
||||
set("startIndex", i);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated after the specified time
|
||||
* @param dt DateTime
|
||||
* @return B
|
||||
**/
|
||||
public B itemsAfter(DateTime dt) {
|
||||
return _dt("itemsAfter", dt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated after right now
|
||||
* @return B
|
||||
*/
|
||||
public B itemsAfterNow() {
|
||||
return _dtNow("itemsAfter");
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific duration after now
|
||||
* @param duration Duration
|
||||
* @return B
|
||||
*/
|
||||
public B itemsAfterFromNow(ReadableDuration duration) {
|
||||
return _dtFromNow("itemsAfter", duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific period after now
|
||||
* @param period
|
||||
* @return B
|
||||
*/
|
||||
public B itemsAfterFromNow(ReadablePeriod period) {
|
||||
return _dtFromNow("itemsAfter", period);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific duration after now
|
||||
* @param v long
|
||||
* @param unit TimeUnit
|
||||
* @return B
|
||||
**/
|
||||
public B itemsAfterFromNow(long v, TimeUnit unit) {
|
||||
return _dtFromNow("itemsAfter", v, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated before a specific time
|
||||
* @param dt DateTime
|
||||
* @return B
|
||||
**/
|
||||
public B itemsBefore(DateTime dt) {
|
||||
return _dt("itemsBefore", dt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated before now
|
||||
* @return B
|
||||
*/
|
||||
public B itemsBeforeNow() {
|
||||
return _dtNow("itemsBefore");
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific duration
|
||||
* before now
|
||||
* @param duration Duration
|
||||
* @return B
|
||||
*/
|
||||
public B itemsBeforeFromNow(ReadableDuration duration) {
|
||||
return _dtFromNow("itemsBefore", duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific period
|
||||
* before now
|
||||
* @param period
|
||||
* @return B
|
||||
*/
|
||||
public B itemsBeforeFromNow(ReadablePeriod period) {
|
||||
return _dtFromNow("itemsBefore", period);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method itemsBeforeFromNow.
|
||||
* @param v long
|
||||
* @param unit TimeUnit
|
||||
* @return B
|
||||
**/
|
||||
public B itemsBeforeFromNow(long v, TimeUnit unit) {
|
||||
return _dtFromNow("itemsBefore", v, unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a paging link
|
||||
* <pre>
|
||||
* Collection collection = Makers.collection()
|
||||
* .pageLink(Page.NEXT, "http://example.org")
|
||||
* .get();
|
||||
* </pre>
|
||||
* @param page
|
||||
* @param url
|
||||
* @return B
|
||||
*/
|
||||
public B pageLink(Page page, String url) {
|
||||
return link(page.label, linkValue(url));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a paging link
|
||||
* @param page
|
||||
* @param link
|
||||
* @return B
|
||||
*/
|
||||
public B pageLink(Page page, LinkValue link) {
|
||||
return link(page.label, link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a paging link
|
||||
* @param page
|
||||
* @param link
|
||||
* @return B
|
||||
*/
|
||||
public B pageLink(Page page, Supplier<? extends LinkValue> link) {
|
||||
return link(page.label, link);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for Collection.
|
||||
* @param builder Collection.AbstractBuilder<?,?>
|
||||
*/
|
||||
Collection(Collection.AbstractBuilder<?, ?> builder) {
|
||||
super(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total number of items
|
||||
* @return int
|
||||
**/
|
||||
public int totalItems() {
|
||||
return getInt("totalItems");
|
||||
}
|
||||
|
||||
/**
|
||||
* If not null, indicates that the collection only contains items
|
||||
* updated after the given instant
|
||||
* @return DateTime
|
||||
**/
|
||||
public DateTime itemsAfter() {
|
||||
return this.getDateTime("itemsAfter");
|
||||
}
|
||||
|
||||
/**
|
||||
* If not null, indicates that the collection only contains items
|
||||
* updated before the given instant
|
||||
* @return DateTime
|
||||
* */
|
||||
public DateTime itemsBefore() {
|
||||
return this.getDateTime("itemsBefore");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of items per page
|
||||
* @return int
|
||||
**/
|
||||
public int itemsPerPage() {
|
||||
return this.getInt("itemsPerPage");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the start index for this page
|
||||
* @return int
|
||||
**/
|
||||
public int startIndex() {
|
||||
return this.getInt("startIndex");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a listing of Paging links that
|
||||
* exist on this collection
|
||||
* @return Iterable<Page>
|
||||
*/
|
||||
public Iterable<Page> pages() {
|
||||
ImmutableSet.Builder<Page> pages =
|
||||
ImmutableSet.builder();
|
||||
for (Page page : Page.values())
|
||||
if (has(page.label))
|
||||
pages.add(page);
|
||||
return pages.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a listing of Paging LinkValues
|
||||
* @param page Page The type of paging link to return
|
||||
* @return Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> pageLink(
|
||||
Page page) {
|
||||
return page.links(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a listing of Paging LinkValues
|
||||
* @param page Page The type of paging link to return
|
||||
* @param filter Predicate<? super LinkValue> A filter
|
||||
* @return Iterable<LinkValue>
|
||||
*/
|
||||
public Iterable<LinkValue> pageLink(
|
||||
Page page,
|
||||
Predicate<? super LinkValue> filter) {
|
||||
return page.links(this,filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first matching paging LinkValue
|
||||
* @param page Page The type of paging link to return
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstPageLink(
|
||||
Page page) {
|
||||
return page.first(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first matching paging LinkValue
|
||||
* @param page Page the type of paging link to return
|
||||
* @param test com.google.common.base.Predicate<? super LinkValue> a filter
|
||||
* @return LinkValue
|
||||
*/
|
||||
public LinkValue firstMatchingPageLink(
|
||||
Page page,
|
||||
Predicate<? super LinkValue> test) {
|
||||
return page.firstMatching(this,test);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the collection of items
|
||||
* @return java.util.Iterable<A>
|
||||
**/
|
||||
public <A extends ASObject>Iterable<A> items() {
|
||||
return this.<Iterable<A>>get("items");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a filtered collection of items
|
||||
* @param filter com.google.common.base.Predicate<? super A> filter
|
||||
* @return java.util.Iterable<A>
|
||||
*/
|
||||
public <A extends ASObject>Iterable<A> items(
|
||||
Predicate<? super A> filter) {
|
||||
return filter(this.<A>items(), filter);
|
||||
}
|
||||
|
||||
// Java Serialization Support
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
extends AbstractSerializedForm<Collection> {
|
||||
protected SerializedForm(Collection obj) {
|
||||
super(obj);
|
||||
}
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
protected Collection.Builder builder() {
|
||||
return Makers.collection();
|
||||
}
|
||||
Object readResolve() throws ObjectStreamException {
|
||||
return super.doReadResolve();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,629 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.ibm.common.activitystreams.internal.Adapter;
|
||||
import com.ibm.common.activitystreams.internal.GsonWrapper;
|
||||
import com.ibm.common.activitystreams.internal.Schema;
|
||||
import com.ibm.common.activitystreams.util.Module;
|
||||
|
||||
/**
|
||||
* The IO object is responsible for serializing and deserializing
|
||||
* Activity Stream objects. Instances of IO should be created and
|
||||
* defined statically. IO instances are threadsafe and immutable
|
||||
* once created.
|
||||
*
|
||||
* <p>You can choose to use one of the default IO instances:</p>
|
||||
*
|
||||
* <pre>
|
||||
* public static final IO io = IO.makeDefault();
|
||||
*
|
||||
* public static final IO prettyIo = IO.makeDefaultPrettyPrint();
|
||||
* </pre>
|
||||
*
|
||||
* <p>Or you can use the IO.Builder to construct and configure your
|
||||
* own IO instance with custom adapters, object properties and
|
||||
* type mappings:</p>
|
||||
*
|
||||
* <pre>
|
||||
* import static
|
||||
*
|
||||
* public static final IO io =
|
||||
* IO.make()
|
||||
* .schema(
|
||||
* Makers.makeSchema().map(
|
||||
* Schema.object.template()
|
||||
* .as("foo", Foo.class)))
|
||||
* .adapter(Foo.class, new MyFooAdapter())
|
||||
* .get();
|
||||
* </pre>
|
||||
*
|
||||
* <p>Once created, you can use IO instances to parse Activity Streams
|
||||
* documents:</p>
|
||||
*
|
||||
* <pre>
|
||||
* InputStream in = ...
|
||||
* Activity activity = io.readAsActivity(in);
|
||||
* </pre>
|
||||
*
|
||||
* <p>Or can use the IO instance to serialize:</p>
|
||||
*
|
||||
* <pre>
|
||||
* OutputStream out = ...
|
||||
* Activity activity = ...
|
||||
* activity.writeTo(out, io);
|
||||
* </pre>
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public final class IO {
|
||||
|
||||
/**
|
||||
* Create a new IO.Builder
|
||||
* @return Builder */
|
||||
public static Builder make() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new IO.Builder that uses the given schema
|
||||
* @param schema Schema
|
||||
* @return IO
|
||||
*/
|
||||
public static IO makeWithSchema(Schema schema) {
|
||||
return make().schema(schema).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new IO.Builder that uses the given schema
|
||||
* @param schema Supplier<? extends Schema>
|
||||
* @return IO
|
||||
*/
|
||||
public static IO makeWithSchema(Supplier<? extends Schema> schema) {
|
||||
return makeWithSchema(schema.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Make or return the default IO instance
|
||||
* @return IO
|
||||
**/
|
||||
public static IO makeDefault(Module... modules) {
|
||||
IO.Builder builder = make();
|
||||
if (modules != null)
|
||||
for (Module mod : modules)
|
||||
builder.using(mod);
|
||||
return builder.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make or return a default IO instance with Pretty Print enabled
|
||||
* @return IO
|
||||
*/
|
||||
public static IO makeDefaultPrettyPrint(Module... modules) {
|
||||
IO.Builder builder = make().prettyPrint();
|
||||
if (modules != null)
|
||||
for (Module mod : modules)
|
||||
builder.using(mod);
|
||||
return builder.get();
|
||||
}
|
||||
|
||||
public static class Builder
|
||||
implements Supplier<IO> {
|
||||
|
||||
private final GsonWrapper.Builder inner =
|
||||
GsonWrapper.make();
|
||||
private Schema schema;
|
||||
private final ImmutableSet.Builder<Module> modules =
|
||||
ImmutableSet.builder();
|
||||
|
||||
public Builder using(Module module) {
|
||||
modules.add(module);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn pretty print on or off
|
||||
* @param on boolean
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder prettyPrint(boolean on) {
|
||||
inner.prettyPrint(on);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn pretty print on
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder prettyPrint() {
|
||||
return prettyPrint(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an adapter
|
||||
* @param type Class<? extends T>
|
||||
* @param adapter Adapter<T>
|
||||
* @return Builder
|
||||
**/
|
||||
public <T>Builder adapter(
|
||||
Class<? extends T> type,
|
||||
Adapter<T> adapter) {
|
||||
inner.adapter(type, adapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an adapter
|
||||
* @param type Class<? extends T>
|
||||
* @return Builder
|
||||
*/
|
||||
public <T>Builder adapter(
|
||||
Class<? extends T> type) {
|
||||
return adapter(type,null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an adapter
|
||||
* @param type Class<? extends T>
|
||||
* @return Builder
|
||||
*/
|
||||
public <T>Builder hierarchicalAdapter(
|
||||
Class<? extends T> type) {
|
||||
return hierarchicalAdapter(type,null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an adapter.
|
||||
* @param type Class<? extends T>
|
||||
* @param adapter Adapter<T>
|
||||
* @param hier boolean
|
||||
* @return Builder
|
||||
**/
|
||||
public <T>Builder hierarchicalAdapter(
|
||||
Class<? extends T> type,
|
||||
Adapter<T> adapter) {
|
||||
inner.adapter(type, adapter, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the schema
|
||||
* @param schema Schema
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder schema(Schema schema) {
|
||||
//inner.schema(schema);
|
||||
this.schema = schema;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the schema.
|
||||
* @param schema Supplier<Schema>
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder schema(Supplier<Schema> schema) {
|
||||
return schema(schema.get());
|
||||
}
|
||||
|
||||
public IO get() {
|
||||
Iterable<Module> mods = modules.build();
|
||||
Schema schema = this.schema;
|
||||
if (schema == null) {
|
||||
Schema.Builder builder = Schema.make();
|
||||
for (Module mod : mods)
|
||||
mod.apply(builder);
|
||||
schema = builder.get();
|
||||
}
|
||||
inner.schema(schema);
|
||||
for (Module module : modules.build())
|
||||
module.apply(this, schema);
|
||||
return new IO(this);
|
||||
}
|
||||
}
|
||||
|
||||
private final GsonWrapper gson;
|
||||
|
||||
protected IO(Builder builder) {
|
||||
this.gson =
|
||||
builder.inner.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the given object
|
||||
* @param w Writable
|
||||
* @return String
|
||||
*/
|
||||
public String write(Writable w) {
|
||||
StringWriter sw = new StringWriter();
|
||||
w.writeTo(sw,this);
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously write the given object
|
||||
* @param w
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<String>
|
||||
*/
|
||||
public Future<String> write(
|
||||
final Writable w,
|
||||
ExecutorService executor) {
|
||||
return executor.submit(
|
||||
new Callable<String>() {
|
||||
public String call() throws Exception {
|
||||
return write(w);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the object to the given outputstream
|
||||
* @param w Writable
|
||||
* @param out OutputStream
|
||||
*/
|
||||
public void write(Writable w, OutputStream out) {
|
||||
gson.write(w,out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asychronously write the object to the given output stream
|
||||
* @param w
|
||||
* @param out
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<?>
|
||||
*/
|
||||
public Future<?> write(
|
||||
final Writable w,
|
||||
final OutputStream out,
|
||||
ExecutorService executor) {
|
||||
return executor.submit(
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
write(w, out);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asychronously write the object to the given writer
|
||||
* @param w
|
||||
* @param out
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<?>
|
||||
*/
|
||||
public Future<?> write(
|
||||
final Writable w,
|
||||
final Writer out,
|
||||
ExecutorService executor) {
|
||||
return executor.submit(
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
write(w, out);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the object to the given writer
|
||||
* @param w Writable
|
||||
* @param out Writer
|
||||
*/
|
||||
public void write(Writable w, Writer out) {
|
||||
gson.write(w,out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given input stream and
|
||||
* return a parsed object of the given type
|
||||
* @param in
|
||||
* @param type
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<A extends ASObject>
|
||||
*/
|
||||
public <A extends ASObject>Future<A> readAs(
|
||||
final InputStream in,
|
||||
final Class<? extends A> type,
|
||||
ExecutorService executor) {
|
||||
return executor.submit(
|
||||
new Callable<A>() {
|
||||
public A call() throws Exception {
|
||||
return readAs(in, type);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given input stream and return a parsed object
|
||||
* of the given type
|
||||
* @param in InputStream
|
||||
* @param type Class<? extends A>
|
||||
* @return A */
|
||||
public <A extends ASObject>A readAs(
|
||||
InputStream in,
|
||||
Class<? extends A> type) {
|
||||
return gson.<A>readAs(in, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given reader and return a parsed
|
||||
* object of the given type
|
||||
* @param in
|
||||
* @param type
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<A extends ASObject>
|
||||
*/
|
||||
public <A extends ASObject>Future<A> readAs(
|
||||
final Reader in,
|
||||
final Class<? extends A> type,
|
||||
ExecutorService executor) {
|
||||
return executor.submit(
|
||||
new Callable<A>() {
|
||||
public A call() throws Exception {
|
||||
return readAs(in, type);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given reader and return a parsed object of the given type
|
||||
* @param in Reader
|
||||
* @param type Class<? extends A>
|
||||
* @return A */
|
||||
public <A extends ASObject>A readAs(
|
||||
Reader in,
|
||||
Class<? extends A> type) {
|
||||
return gson.<A>readAs(in, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given string and return a parsed object of
|
||||
* the given type
|
||||
* @param in
|
||||
* @param type
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<A extends ASObject>
|
||||
*/
|
||||
public <A extends ASObject>Future<A> readAs(
|
||||
final String in,
|
||||
final Class<? extends A> type,
|
||||
ExecutorService executor) {
|
||||
return executor.submit(
|
||||
new Callable<A>() {
|
||||
public A call() throws Exception {
|
||||
return readAs(in, type);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given string and return a parsed object of the given type
|
||||
* @param in String
|
||||
* @param type Class<? extends A>
|
||||
* @return A
|
||||
*/
|
||||
public <A extends ASObject>A readAs(
|
||||
String in,
|
||||
Class<? extends A> type) {
|
||||
return readAs(new StringReader(in),type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given string
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<ASObject>
|
||||
*/
|
||||
public Future<ASObject> read(String in, ExecutorService executor) {
|
||||
return read(new StringReader(in), executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given string
|
||||
* @param in String
|
||||
* @return ASObject
|
||||
*/
|
||||
public ASObject read(String in) {
|
||||
return read(new StringReader(in));
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given inputstream
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<ASObject>
|
||||
*/
|
||||
public Future<ASObject> read(InputStream in, ExecutorService executor) {
|
||||
return readAs(in, ASObject.class, executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given reader
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<ASObject>
|
||||
*/
|
||||
public Future<ASObject> read(Reader in, ExecutorService executor) {
|
||||
return readAs(in, ASObject.class, executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given input stream.
|
||||
* @param in InputStream
|
||||
* @return ASObject
|
||||
**/
|
||||
public ASObject read(InputStream in) {
|
||||
return readAs(in, ASObject.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the given input stream
|
||||
* @param in InputStream
|
||||
* @return A
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <A extends ASObject>A readAs(InputStream in) {
|
||||
return (A)read(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given string as an Activity object
|
||||
* @param in String
|
||||
* @return Activity
|
||||
*/
|
||||
public Activity readAsActivity(String in) {
|
||||
return readAsActivity(new StringReader(in));
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given string as an Activity object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Activity>
|
||||
*/
|
||||
public Future<Activity> readAsActivity(String in, ExecutorService executor) {
|
||||
return readAsActivity(new StringReader(in), executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given inputstream as an Activity object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Activity>
|
||||
*/
|
||||
public Future<Activity> readAsActivity(InputStream in, ExecutorService executor) {
|
||||
return readAs(in, Activity.class, executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given reader as an Activity object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Activity>
|
||||
*/
|
||||
public Future<Activity> readAsActivity(Reader in, ExecutorService executor) {
|
||||
return readAs(in, Activity.class, executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given string as a Collection object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Collection>
|
||||
*/
|
||||
public Future<Collection> readAsCollection(String in, ExecutorService executor) {
|
||||
return readAsCollection(new StringReader(in), executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given input stream as a Collection object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Collection>
|
||||
*/
|
||||
public Future<Collection> readAsCollection(InputStream in, ExecutorService executor) {
|
||||
return readAs(in, Collection.class, executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given reader as a Collection object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Collection>
|
||||
*/
|
||||
public Future<Collection> readAsCollection(Reader in, ExecutorService executor) {
|
||||
return readAs(in, Collection.class, executor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given inputstream as an Activity.
|
||||
* @param in InputStream
|
||||
* @return Activity
|
||||
**/
|
||||
public Activity readAsActivity(InputStream in) {
|
||||
return readAs(in, Activity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given string as a Collection.
|
||||
* @param in InputStream
|
||||
* @return Collection
|
||||
**/
|
||||
public Collection readAsCollection(String in) {
|
||||
return readAsCollection(new StringReader(in));
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given inputstream as a Collection.
|
||||
* @param in InputStream
|
||||
* @return Collection
|
||||
**/
|
||||
public Collection readAsCollection(InputStream in) {
|
||||
return readAs(in, Collection.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given reader
|
||||
* @param in
|
||||
* @return ASObject
|
||||
*/
|
||||
public ASObject read(Reader in) {
|
||||
return readAs(in, ASObject.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given reader as an Activity
|
||||
* @param in Reader
|
||||
* @return Activity
|
||||
**/
|
||||
public Activity readAsActivity(Reader in) {
|
||||
return readAs(in, Activity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given reader as a Collection
|
||||
* @param in Reader
|
||||
* @return Collection
|
||||
**/
|
||||
public Collection readAsCollection(Reader in) {
|
||||
return readAs(in, Collection.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,330 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable;
|
||||
|
||||
/**
|
||||
* An Activity Streams 2.0 Link Value.
|
||||
*
|
||||
* <p>In Actvity Streams 2.0, Link Values can take one of three possible
|
||||
* forms:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>A String containing an absolute IRI</li>
|
||||
* <li>An Activity String object</li>
|
||||
* <li>An Array containing a mix of Strings or Objects</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>For instance, the following are all valid examples of Link Values:</p>
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "actor": "acct:joe@example.org"
|
||||
* }
|
||||
*
|
||||
* {
|
||||
* "actor": {
|
||||
* "objectType": "person",
|
||||
* "id": "acct:sally@example.org"
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* {
|
||||
* "actor": [
|
||||
* "acct:joe@example.org",
|
||||
* {
|
||||
* "objectType": "person",
|
||||
* "id": "acct:sally@example.org"
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>The LinkValue interface provides a minimal abstraction over these
|
||||
* value options. Developers will have to check the ValueType of the LinkValue
|
||||
* (or do an instanceof check) in order to determine which kind of value
|
||||
* they are working with.</p>
|
||||
*
|
||||
* <pre>
|
||||
* Activity activity = ...;
|
||||
* Iterable<LinkValue> actors = activity.actor();
|
||||
* for (LinkValue actor : actors) {
|
||||
* switch(actor.valueType()) {
|
||||
* case SIMPLE:
|
||||
* SimpleLinkValue s = (SimpleLinkValue)actor;
|
||||
* //...
|
||||
* break;
|
||||
* case OBJECT:
|
||||
* ASObject obj = (ASObject)actor;
|
||||
* //...
|
||||
* break;
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>Methods that return Iterable<LinkValue> will never include an instance
|
||||
* of ArrayLinkValue as one of the Iterable values</p>
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public interface LinkValue
|
||||
extends Writable, Serializable {
|
||||
|
||||
/**
|
||||
* Returns the LinkValue type
|
||||
* @return ValueType
|
||||
*/
|
||||
ValueType valueType();
|
||||
|
||||
/**
|
||||
* A "Simple Link Value" is a string with a relative or absolute
|
||||
* URI or IRI value.
|
||||
*/
|
||||
public static final class SimpleLinkValue
|
||||
extends AbstractWritable
|
||||
implements LinkValue, Serializable {
|
||||
|
||||
/**
|
||||
* Creates a new builder
|
||||
* @return Builder
|
||||
**/
|
||||
public static Builder make() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
* @param url String
|
||||
* @return LinkValue
|
||||
**/
|
||||
public static LinkValue make(String url) {
|
||||
return new SimpleLinkValue.Builder().url(url).get();
|
||||
}
|
||||
|
||||
public static final class Builder
|
||||
extends AbstractWritable.AbstractWritableBuilder<SimpleLinkValue,Builder> {
|
||||
|
||||
private String iri;
|
||||
|
||||
/**
|
||||
* Set the url
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder url(String iri) {
|
||||
this.iri = iri;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return SimpleLinkValue
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
**/
|
||||
public SimpleLinkValue get() {
|
||||
return new SimpleLinkValue(this);
|
||||
}
|
||||
}
|
||||
|
||||
private final String iri;
|
||||
|
||||
SimpleLinkValue(SimpleLinkValue.Builder builder) {
|
||||
super(builder);
|
||||
this.iri = builder.iri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the url
|
||||
* @return String
|
||||
*/
|
||||
public String url() {
|
||||
return iri;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return iri;
|
||||
}
|
||||
|
||||
public ValueType valueType() {
|
||||
return ValueType.SIMPLE;
|
||||
}
|
||||
|
||||
// Java Serialization Support
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private String iri;
|
||||
SerializedForm(SimpleLinkValue obj) {
|
||||
this.iri = obj.iri;
|
||||
}
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
return Makers.linkValue(iri);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* An Array Link value is a JSON Array of one or more Simple or Object
|
||||
* Link Values. Array Link Values MUST NOT contain nested arrays.
|
||||
*/
|
||||
public static final class ArrayLinkValue
|
||||
extends AbstractWritable
|
||||
implements Iterable<LinkValue>, LinkValue, Serializable {
|
||||
|
||||
/**
|
||||
* Create a new builder
|
||||
* @return ArrayLinkValue.Builder
|
||||
**/
|
||||
public static ArrayLinkValue.Builder make() {
|
||||
return new ArrayLinkValue.Builder();
|
||||
}
|
||||
|
||||
public static class Builder
|
||||
extends AbstractWritable.AbstractWritableBuilder<ArrayLinkValue, Builder> {
|
||||
|
||||
private final ImmutableList.Builder<LinkValue> links =
|
||||
ImmutableList.builder();
|
||||
|
||||
/**
|
||||
* Add one or more items
|
||||
* @param value String
|
||||
* @param values String[]
|
||||
* @return Builder */
|
||||
public Builder add(String value, String... values) {
|
||||
if (value != null)
|
||||
add(SimpleLinkValue.make(value));
|
||||
if (values != null)
|
||||
for (String v : values)
|
||||
add(SimpleLinkValue.make(v));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add one or more link values
|
||||
* @param links Iterable<LinkValue>
|
||||
* @return Builder */
|
||||
public Builder add(Iterable<LinkValue> links) {
|
||||
for (LinkValue l : links)
|
||||
add(l);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add one or more link values
|
||||
* @param values LinkValue[]
|
||||
* @return Builder */
|
||||
public Builder add(LinkValue value, LinkValue... values) {
|
||||
if (value != null) {
|
||||
checkArgument(value.valueType() != ValueType.ARRAY);
|
||||
links.add(value);
|
||||
}
|
||||
if (values != null)
|
||||
for (LinkValue v : values) {
|
||||
checkArgument(v.valueType() != ValueType.ARRAY);
|
||||
links.add(v);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a link value
|
||||
* @param value Supplier<? extends LinkValue>
|
||||
* @return Builder */
|
||||
public Builder add(Supplier<? extends LinkValue> value) {
|
||||
LinkValue val = value.get();
|
||||
checkArgument(val.valueType() != ValueType.ARRAY);
|
||||
links.add(val);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return ArrayLinkValue
|
||||
* @see com.google.common.base.Supplier#get()
|
||||
**/
|
||||
public ArrayLinkValue get() {
|
||||
return new ArrayLinkValue(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final ImmutableList<LinkValue> links;
|
||||
|
||||
ArrayLinkValue(ArrayLinkValue.Builder builder) {
|
||||
super(builder);
|
||||
this.links = builder.links.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method iterator.
|
||||
* @return Iterator<LinkValue>
|
||||
* @see java.lang.Iterable#iterator()
|
||||
**/
|
||||
public Iterator<LinkValue> iterator() {
|
||||
return links.iterator();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return links.toString();
|
||||
}
|
||||
|
||||
public ValueType valueType() {
|
||||
return ValueType.ARRAY;
|
||||
}
|
||||
|
||||
// Java Serialization Support
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private ImmutableList<LinkValue> list;
|
||||
SerializedForm(ArrayLinkValue obj) {
|
||||
this.list = obj.links;
|
||||
}
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
return Makers.linkValues().add(list);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,174 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static com.google.common.collect.ImmutableMap.copyOf;
|
||||
import com.ibm.common.activitystreams.internal.Schema;
|
||||
|
||||
/**
|
||||
* Utility class for creating Makers for all of the various objects.
|
||||
*/
|
||||
public final class Makers {
|
||||
|
||||
private Makers() {}
|
||||
|
||||
/**
|
||||
* Make a new ActionsValue.Builder
|
||||
* @return ParametersValue.Builder
|
||||
**/
|
||||
public static ActionsValue.Builder actions() {
|
||||
return new ActionsValue.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new Schema.Builder
|
||||
* @return Schema.Builder
|
||||
*/
|
||||
public static Schema.Builder schema() {
|
||||
return Schema.make();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new IO.builder
|
||||
* @return IO.Builder
|
||||
**/
|
||||
public static IO.Builder io() {
|
||||
return IO.make();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new Activity.Builder
|
||||
* @return Activity.Builder
|
||||
**/
|
||||
public static Activity.Builder activity() {
|
||||
return new Activity.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new Collection.Builder
|
||||
* @return Collection.Builder
|
||||
**/
|
||||
public static Collection.Builder collection() {
|
||||
return new Collection.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new ASObject.Builder
|
||||
* @return ASObject.Builder
|
||||
**/
|
||||
public static ASObject.Builder object() {
|
||||
return new ASObject.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new ASObject.Builder with a specific objectType
|
||||
* @param type String
|
||||
* @return ASObject.Builder
|
||||
**/
|
||||
public static ASObject.Builder object(String type) {
|
||||
return object().objectType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an Object from the given Map
|
||||
* @param map
|
||||
* @return ASObject
|
||||
*/
|
||||
public static ASObject objectFrom(Map<String,?> map) {
|
||||
ASObject.Builder builder = object();
|
||||
for (Map.Entry<String,?> entry : copyOf(map).entrySet())
|
||||
builder.set(entry.getKey(), entry.getValue());
|
||||
return builder.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new ASObject.Builder
|
||||
* @param type TypeValue
|
||||
* @return ASObject.Builder
|
||||
**/
|
||||
public static ASObject.Builder object(TypeValue type) {
|
||||
return object().objectType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new TypeValue
|
||||
* @param iri String
|
||||
* @return TypeValue
|
||||
**/
|
||||
public static TypeValue type(String iri) {
|
||||
return TypeValue.SimpleTypeValue.make(iri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new LinkValue
|
||||
* @param iri String
|
||||
* @return LinkValue
|
||||
**/
|
||||
public static LinkValue linkValue(String iri) {
|
||||
return LinkValue.SimpleLinkValue.make(iri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new ArrayLinkValue.Builder
|
||||
* @return LinkValue.ArrayLinkValue.Builder
|
||||
**/
|
||||
public static LinkValue.ArrayLinkValue.Builder linkValues() {
|
||||
return LinkValue.ArrayLinkValue.make();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new MapNLV.Builder
|
||||
* @return NLV.MapNLV.Builder
|
||||
**/
|
||||
public static NLV.MapNLV.Builder nlv() {
|
||||
return NLV.MapNLV.make();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new SimpleNLV value
|
||||
* @param val String
|
||||
* @return NLV.SimpleNLV
|
||||
**/
|
||||
public static NLV.SimpleNLV nlv(String val) {
|
||||
return NLV.SimpleNLV.make(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new verb ASObject.Builder
|
||||
* @param id String
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
public static ASObject.Builder verb(String id) {
|
||||
return object("verb").id(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new objectType ASObject.Builder
|
||||
* @param id String
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
public static ASObject.Builder objectType(String id) {
|
||||
return object("objectType").id(id);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,278 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import static com.google.common.collect.Maps.difference;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable;
|
||||
|
||||
|
||||
/**
|
||||
* An Activity Streams 2.0 Natural Language Value...
|
||||
*
|
||||
* <p>The value is either a simple string or an object
|
||||
* with multiple Language-Tag keys and values...</p>
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "objectType": "note",
|
||||
* "displayName": {
|
||||
* "en": "My Title In English",
|
||||
* "fr": "Mon titre en français"
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* ASObject obj = ...
|
||||
* System.out.println(obj.displayNameString("en"));
|
||||
* System.out.println(obj.displayNameString("fr"));
|
||||
*
|
||||
* NLV nlv = obj.displayName();
|
||||
* switch(nlv.valueType()) {
|
||||
* case SIMPLE:
|
||||
* SimpleNLV s = (SimpleNLV)nlv;
|
||||
* ...
|
||||
* break;
|
||||
* case OBJECT:
|
||||
* MapNLV m = (MapNLV)nlv;
|
||||
* ...
|
||||
* break;
|
||||
* }
|
||||
* </p>
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public interface NLV
|
||||
extends Writable, Serializable {
|
||||
|
||||
/**
|
||||
* Returns the value type. Either ValueType.SIMPLE or ValueType.OBJECT
|
||||
* @return ValueType
|
||||
*/
|
||||
ValueType valueType();
|
||||
|
||||
public static final class SimpleNLV
|
||||
extends AbstractWritable
|
||||
implements NLV, Serializable {
|
||||
|
||||
/**
|
||||
* Create a new builder
|
||||
* @return Builder */
|
||||
public static Builder make() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance
|
||||
* @param value String
|
||||
* @return SimpleNLV */
|
||||
public static SimpleNLV make(String value) {
|
||||
return make().value(value).get();
|
||||
}
|
||||
|
||||
private final String val;
|
||||
|
||||
SimpleNLV(Builder builder) {
|
||||
super(builder);
|
||||
this.val = builder.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value
|
||||
* @return String
|
||||
**/
|
||||
public String value() {
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
SimpleNLV other = (SimpleNLV) obj;
|
||||
return Objects.equal(val, other.val);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return val;
|
||||
}
|
||||
|
||||
public static final class Builder
|
||||
extends AbstractWritable.AbstractWritableBuilder<SimpleNLV,Builder> {
|
||||
|
||||
private String val;
|
||||
|
||||
public Builder value(String val) {
|
||||
this.val = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleNLV get() {
|
||||
return new SimpleNLV(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ValueType valueType() {
|
||||
return ValueType.SIMPLE;
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private String value;
|
||||
SerializedForm(SimpleNLV obj) {
|
||||
this.value = obj.val;
|
||||
}
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
return Makers.nlv(value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final class MapNLV
|
||||
extends AbstractWritable
|
||||
implements NLV, Iterable<String> {
|
||||
|
||||
public static Builder make() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
private final ImmutableMap<String,String> vals;
|
||||
private transient int hash = 1;
|
||||
|
||||
MapNLV(Builder builder) {
|
||||
super(builder);
|
||||
this.vals = builder.vals.build();
|
||||
}
|
||||
|
||||
public String value(String lang) {
|
||||
return vals.get(lang);
|
||||
}
|
||||
|
||||
public boolean has(String lang) {
|
||||
return vals.containsKey(lang);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
if (hash == 1)
|
||||
hash = Objects.hashCode(vals);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
MapNLV other = (MapNLV) obj;
|
||||
return
|
||||
difference(vals, other.vals)
|
||||
.areEqual();
|
||||
|
||||
}
|
||||
|
||||
public Iterator<String> iterator() {
|
||||
return vals.keySet().iterator();
|
||||
}
|
||||
|
||||
public static final class Builder
|
||||
extends AbstractWritable.AbstractWritableBuilder<MapNLV,Builder> {
|
||||
|
||||
private final ImmutableMap.Builder<String,String> vals =
|
||||
ImmutableMap.builder();
|
||||
|
||||
public Builder from(MapNLV other, String lang) {
|
||||
for (String l : other)
|
||||
if (!l.equalsIgnoreCase(lang))
|
||||
set(l, other.value(l));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder set(String lang, String val) {
|
||||
vals.put(lang,val);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MapNLV get() {
|
||||
return new MapNLV(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ValueType valueType() {
|
||||
return ValueType.OBJECT;
|
||||
}
|
||||
|
||||
public Map<String,String> toMap() {
|
||||
return vals;
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private ImmutableMap<String,String> map;
|
||||
SerializedForm(MapNLV obj) {
|
||||
this.map = obj.vals;
|
||||
}
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
NLV.MapNLV.Builder builder =
|
||||
Makers.nlv();
|
||||
for (Map.Entry<String,String> entry : map.entrySet())
|
||||
builder.set(entry.getKey(), entry.getValue());
|
||||
return builder.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,179 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable;
|
||||
|
||||
/**
|
||||
* In Activity Streams 2.0, the "objectType" and "verb" properties,
|
||||
* as well as several other properties, are defined as "Type Values".
|
||||
* A "Type Value" can be either a simple token string, an absolute
|
||||
* IRI string, or an ASObject.
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "verb": "post"
|
||||
* }
|
||||
*
|
||||
* {
|
||||
* "verb": "urn:example:verbs:foo"
|
||||
* }
|
||||
*
|
||||
* {
|
||||
* "verb": {
|
||||
* "id": "urn:example:verbs:foo",
|
||||
* "displayName": "Foo"
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>The TypeValue interface provides a minimal abstraction
|
||||
* over these possible values. Developers should check valueType
|
||||
* to determine which type of TypeValue they are working with.</p>
|
||||
*
|
||||
* <pre>
|
||||
* Activity activity = ...
|
||||
* TypeValue tv = activity.verb();
|
||||
*
|
||||
* System.out.println(tv.id());
|
||||
*
|
||||
* switch(tv.valueType()) {
|
||||
* case SIMPLE:
|
||||
* SimpleTypeValue s = (SimpleTypeValue)tv;
|
||||
* ...
|
||||
* break;
|
||||
* case OBJECT:
|
||||
* ASObject o (ASObject)tv;
|
||||
* ...
|
||||
* break;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public interface TypeValue
|
||||
extends Writable, Serializable {
|
||||
|
||||
/**
|
||||
* Return the type value identifier
|
||||
* @return String
|
||||
*/
|
||||
String id();
|
||||
|
||||
ValueType valueType();
|
||||
|
||||
public static final class SimpleTypeValue
|
||||
extends AbstractWritable
|
||||
implements TypeValue, Serializable {
|
||||
|
||||
public static Builder make() {
|
||||
return new SimpleTypeValue.Builder();
|
||||
}
|
||||
|
||||
public static TypeValue make(String url) {
|
||||
return make().url(url).get();
|
||||
}
|
||||
|
||||
public static final class Builder
|
||||
extends AbstractWritable.AbstractWritableBuilder<SimpleTypeValue,Builder> {
|
||||
|
||||
private String iri;
|
||||
|
||||
/**
|
||||
* Set the url
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder url(String iri) {
|
||||
this.iri = iri;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleTypeValue get() {
|
||||
return new SimpleTypeValue(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final String iri;
|
||||
|
||||
SimpleTypeValue(SimpleTypeValue.Builder builder) {
|
||||
super(builder);
|
||||
this.iri = builder.iri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(iri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
SimpleTypeValue other = (SimpleTypeValue) obj;
|
||||
return Objects.equals(iri,other.iri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the type value identifier
|
||||
* @return String
|
||||
* @see com.ibm.common.activitystreams.TypeValue#id() */
|
||||
public String id() {
|
||||
return iri;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return iri;
|
||||
}
|
||||
|
||||
public ValueType valueType() {
|
||||
return ValueType.SIMPLE;
|
||||
}
|
||||
|
||||
Object writeReplace() throws java.io.ObjectStreamException {
|
||||
return new SerializedForm(this);
|
||||
}
|
||||
|
||||
private static class SerializedForm
|
||||
implements Serializable {
|
||||
private static final long serialVersionUID = -1975376657749952999L;
|
||||
private String iri;
|
||||
SerializedForm(SimpleTypeValue obj) {
|
||||
this.iri = obj.iri;
|
||||
}
|
||||
Object readResolve()
|
||||
throws java.io.ObjectStreamException {
|
||||
return Makers.type(iri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* Base interface for all objects that serialize to IO object instances
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public interface Writable {
|
||||
|
||||
/**
|
||||
* Write the object to the output stream using the default IO instance
|
||||
* @param out OutputStream
|
||||
*/
|
||||
void writeTo(OutputStream out);
|
||||
|
||||
/**
|
||||
* Asynchronously write the object to the output stream using the
|
||||
* default IO instance
|
||||
* @param out
|
||||
* @param executor
|
||||
* @return Future<?>
|
||||
*/
|
||||
Future<?> writeTo(OutputStream out, ExecutorService executor);
|
||||
|
||||
/**
|
||||
* Write the object to the Writer using the default IO instance
|
||||
* @param out Writer
|
||||
*/
|
||||
void writeTo(Writer out);
|
||||
|
||||
/**
|
||||
* Asynchronously write the object to the writer using the default IO instance
|
||||
* @param out
|
||||
* @param executor
|
||||
* @return Future<?>
|
||||
*/
|
||||
Future<?> writeTo(Writer out, ExecutorService executor);
|
||||
|
||||
/**
|
||||
* Write the object to the output stream using the given IO instance
|
||||
* @param out OutputStream
|
||||
* @param io IO
|
||||
*/
|
||||
void writeTo(OutputStream out, IO io);
|
||||
|
||||
/**
|
||||
* Asynchronously write the object to the output stream using the given
|
||||
* IO instance.
|
||||
* @param out
|
||||
* @param io
|
||||
* @param executor
|
||||
* @return Future<?>
|
||||
*/
|
||||
Future<?> writeTo(OutputStream out, IO io, ExecutorService executor);
|
||||
|
||||
/**
|
||||
* Write the object to the writer using the given IO instance
|
||||
* @param out Writer
|
||||
* @param io IO
|
||||
*/
|
||||
void writeTo(Writer out, IO io);
|
||||
|
||||
/**
|
||||
* Asynchronously write the object to the writer using the given IO instance
|
||||
* @param out
|
||||
* @param io
|
||||
* @param executor
|
||||
* @return Future<?>
|
||||
*/
|
||||
Future<?> writeTo(Writer out, IO io, ExecutorService executor);
|
||||
|
||||
}
|
||||
|
|
@ -1,323 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
|
||||
import static com.google.common.collect.ImmutableList.builder;
|
||||
import static com.ibm.common.activitystreams.Makers.activity;
|
||||
import static com.ibm.common.activitystreams.Makers.collection;
|
||||
import static com.ibm.common.activitystreams.Makers.object;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.google.common.base.Converter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.gson.JsonArray;
|
||||
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.Activity;
|
||||
import com.ibm.common.activitystreams.Collection;
|
||||
import com.ibm.common.activitystreams.LinkValue;
|
||||
import com.ibm.common.activitystreams.Makers;
|
||||
import com.ibm.common.activitystreams.TypeValue;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public class ASObjectAdapter
|
||||
extends Adapter<ASObject> {
|
||||
|
||||
private final Schema schema;
|
||||
|
||||
protected Schema schema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for ASObjectAdapter.
|
||||
* @param schema Schema
|
||||
*/
|
||||
protected ASObjectAdapter(Schema schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param obj ASObject
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
|
||||
* @return JsonElement */
|
||||
public final JsonElement serialize(
|
||||
ASObject obj,
|
||||
Type type,
|
||||
JsonSerializationContext context) {
|
||||
JsonObject el =
|
||||
new JsonObject();
|
||||
for (String key : obj) {
|
||||
Object val = obj.get(key);
|
||||
if (val != null) {
|
||||
el.add(
|
||||
key,
|
||||
context.serialize(
|
||||
val,
|
||||
val.getClass()));
|
||||
}
|
||||
}
|
||||
return el;
|
||||
|
||||
}
|
||||
|
||||
private static final ImmutableSet<? extends Type> knownTypes =
|
||||
ImmutableSet.of(
|
||||
Collection.class,
|
||||
Activity.class);
|
||||
|
||||
protected boolean knowsType(Type type) {
|
||||
return knownTypes.contains(type);
|
||||
}
|
||||
|
||||
protected ASObject.AbstractBuilder<?,?> builderFor(Type type) {
|
||||
if (type == Collection.class)
|
||||
return collection();
|
||||
else if (type == Activity.class)
|
||||
return activity();
|
||||
else return null;
|
||||
}
|
||||
|
||||
protected Model modelFor(Type type) {
|
||||
if (type == Collection.class)
|
||||
return schema.forObjectClassOrType(
|
||||
Collection.Builder.class,
|
||||
"collection");
|
||||
else if (type == Activity.class)
|
||||
return schema.forObjectClassOrType(
|
||||
Activity.Builder.class,
|
||||
"activity");
|
||||
else return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param element JsonElement
|
||||
* @param type Type
|
||||
* @param context JsonDeserializationContext
|
||||
* @return ASObject
|
||||
* @throws JsonParseException
|
||||
* @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
|
||||
**/
|
||||
public final ASObject deserialize(
|
||||
JsonElement element,
|
||||
Type type,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
|
||||
JsonObject obj = (JsonObject)element;
|
||||
ASObject.AbstractBuilder<?,?> builder = null;
|
||||
Model propMap = null;
|
||||
TypeValue tv = null;
|
||||
|
||||
if (knowsType(type)) {
|
||||
builder = builderFor(type);
|
||||
propMap = modelFor(type);
|
||||
} else {
|
||||
if (obj.has("objectType")) {
|
||||
tv = context.deserialize(
|
||||
obj.get("objectType"),
|
||||
TypeValue.class);
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class<? extends ASObject.AbstractBuilder> _class =
|
||||
schema.builderForObjectTypeOrClass(tv.id(), (Class)type);
|
||||
if (_class != null) {
|
||||
propMap = schema.forObjectClassOrType(_class, tv.id());
|
||||
if (!_class.isInterface()) {
|
||||
try {
|
||||
builder = _class.getConstructor(String.class).newInstance(tv.id());
|
||||
} catch (Throwable t) {
|
||||
try {
|
||||
builder = _class.newInstance();
|
||||
builder.set("objectType", tv);
|
||||
} catch (Throwable t2) {
|
||||
builder = Makers.object(tv);
|
||||
}
|
||||
}
|
||||
} else
|
||||
builder = Makers.object(tv);
|
||||
} else {
|
||||
builder = Makers.object(tv);
|
||||
propMap = schema.forObjectClassOrType(
|
||||
ASObject.Builder.class, tv.id());
|
||||
}
|
||||
} else {
|
||||
if (obj.has("verb") &&
|
||||
(obj.has("actor") ||
|
||||
obj.has("object") ||
|
||||
obj.has("target"))) {
|
||||
builder = activity();
|
||||
propMap = schema.forObjectClassOrType(
|
||||
Activity.Builder.class, "activity");
|
||||
} else if (obj.has("items")) {
|
||||
builder = collection();
|
||||
propMap = schema.forObjectClassOrType(
|
||||
Collection.Builder.class,
|
||||
"collection");
|
||||
} else {
|
||||
@SuppressWarnings("rawtypes")
|
||||
Class<? extends ASObject.AbstractBuilder> _class =
|
||||
schema.builderFor((Class)type);
|
||||
if (_class != null) {
|
||||
if (!_class.isInterface()) {
|
||||
try {
|
||||
builder = _class.newInstance();
|
||||
} catch (Throwable t) {
|
||||
builder = object();
|
||||
}
|
||||
} else builder = object();
|
||||
}
|
||||
if (builder == null)
|
||||
builder = object(); // anonymous
|
||||
propMap = schema.forObjectClass(builder.getClass());
|
||||
propMap = propMap != null ?
|
||||
propMap :
|
||||
schema.forObjectClass(
|
||||
ASObject.Builder.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Entry<String,JsonElement> entry : obj.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
if (name.equalsIgnoreCase("objectType")) continue;
|
||||
Class<?> _class = propMap.get(name);
|
||||
JsonElement val = entry.getValue();
|
||||
if (val.isJsonPrimitive())
|
||||
builder.set(
|
||||
name,
|
||||
_class != null ?
|
||||
context.deserialize(val,_class) :
|
||||
primConverter.convert(val.getAsJsonPrimitive()));
|
||||
else if (val.isJsonArray()) {
|
||||
builder.set(
|
||||
name,
|
||||
LinkValue.class.isAssignableFrom(_class!=null?_class:Object.class) ?
|
||||
context.deserialize(val, LinkValue.class) :
|
||||
convert(
|
||||
val.getAsJsonArray(),
|
||||
_class,
|
||||
context,
|
||||
builder()));
|
||||
} else if (val.isJsonObject())
|
||||
builder.set(
|
||||
name,
|
||||
context.deserialize(
|
||||
val,
|
||||
propMap.has(name) ?
|
||||
propMap.get(name):
|
||||
ASObject.class));
|
||||
}
|
||||
return builder.get();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Method convert.
|
||||
* @param arr JsonArray
|
||||
* @param _class Class<?>
|
||||
* @param context JsonDeserializationContext
|
||||
* @param list ImmutableList.Builder<Object>
|
||||
* @return ImmutableList<Object>
|
||||
*/
|
||||
private ImmutableList<Object> convert(
|
||||
JsonArray arr,
|
||||
Class<?> _class,
|
||||
JsonDeserializationContext context,
|
||||
ImmutableList.Builder<Object> list) {
|
||||
processArray(arr, _class, context, list);
|
||||
return list.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method processArray.
|
||||
* @param arr JsonArray
|
||||
* @param _class Class<?>
|
||||
* @param context JsonDeserializationContext
|
||||
* @param list ImmutableList.Builder<Object>
|
||||
*/
|
||||
private void processArray(
|
||||
JsonArray arr,
|
||||
Class<?> _class,
|
||||
JsonDeserializationContext context,
|
||||
ImmutableList.Builder<Object> list) {
|
||||
for (JsonElement mem : arr) {
|
||||
if (mem.isJsonPrimitive())
|
||||
list.add(
|
||||
_class != null ?
|
||||
context.deserialize(mem,_class) :
|
||||
primConverter.convert(
|
||||
mem.getAsJsonPrimitive()));
|
||||
else if (mem.isJsonObject())
|
||||
list.add(
|
||||
context.deserialize(
|
||||
mem,
|
||||
_class != null ?
|
||||
_class :
|
||||
ASObject.class));
|
||||
else if (mem.isJsonArray())
|
||||
list.add(
|
||||
convert(
|
||||
mem.getAsJsonArray(),
|
||||
_class,
|
||||
context,
|
||||
builder()));
|
||||
}
|
||||
}
|
||||
|
||||
public static final Converter<JsonPrimitive,Object> primConverter =
|
||||
new Converter<JsonPrimitive,Object>() {
|
||||
@Override
|
||||
protected JsonPrimitive doBackward(Object a) {
|
||||
if (a instanceof Boolean)
|
||||
return new JsonPrimitive((Boolean)a);
|
||||
else if (a instanceof Number)
|
||||
return new JsonPrimitive((Number)a);
|
||||
else
|
||||
return new JsonPrimitive(a.toString());
|
||||
}
|
||||
@Override
|
||||
protected Object doForward(JsonPrimitive b) {
|
||||
if (b.isBoolean())
|
||||
return b.getAsBoolean();
|
||||
else if (b.isNumber())
|
||||
return b.getAsNumber();
|
||||
else
|
||||
return b.getAsString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -1,336 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.collect.Iterables.getFirst;
|
||||
import static com.google.common.collect.Iterables.size;
|
||||
|
||||
import static com.ibm.common.activitystreams.Makers.actions;
|
||||
import static com.ibm.common.activitystreams.Makers.linkValues;
|
||||
import static com.ibm.common.activitystreams.internal.ASObjectAdapter.primConverter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Duration;
|
||||
import org.joda.time.Interval;
|
||||
import org.joda.time.Period;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.BoundType;
|
||||
import com.google.common.collect.ImmutableTable;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.Table;
|
||||
import com.google.common.net.MediaType;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.internal.LazilyParsedNumber;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.ActionsValue;
|
||||
import com.ibm.common.activitystreams.LinkValue;
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObjectAdapter;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
final class Adapters {
|
||||
|
||||
private Adapters() {}
|
||||
|
||||
/**
|
||||
* Method forEnum.
|
||||
* @param _enumClass Class<E>
|
||||
* @return EnumAdapter<E>
|
||||
*/
|
||||
static <E extends Enum<E>>EnumAdapter<E> forEnum(Class<E> _enumClass) {
|
||||
return new EnumAdapter<E>(_enumClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method forEnum.
|
||||
* @param _enumClass Class<E>
|
||||
* @param or E
|
||||
* @return EnumAdapter<E>
|
||||
*/
|
||||
static <E extends Enum<E>>EnumAdapter<E> forEnum(Class<E> _enumClass, E or) {
|
||||
return new EnumAdapter<E>(_enumClass,or);
|
||||
}
|
||||
|
||||
static final Adapter<com.ibm.common.activitystreams.NLV> NLV =
|
||||
new NaturalLanguageValueAdapter();
|
||||
|
||||
static final Adapter<ActionsValue> ACTIONS =
|
||||
new AbstractDictionaryObjectAdapter
|
||||
<LinkValue,
|
||||
ActionsValue,
|
||||
ActionsValue.Builder>(LinkValue.class) {
|
||||
public JsonElement serialize(
|
||||
ActionsValue actions,
|
||||
Type type,
|
||||
JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
for (String verb : actions) {
|
||||
Iterable<LinkValue> links =
|
||||
actions.get(verb);
|
||||
obj.add(
|
||||
verb,
|
||||
context.serialize(
|
||||
size(links) == 1 ? // if there's only one, serialize just 1
|
||||
getFirst(links,null) : // otherwise, serialize the list
|
||||
linkValues().add(links).get(),
|
||||
LinkValue.class));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
@Override
|
||||
protected ActionsValue.Builder builder() {
|
||||
return actions();
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<Iterable<?>> ITERABLE =
|
||||
new Adapter<Iterable<?>>() {
|
||||
|
||||
public JsonElement serialize(
|
||||
Iterable<?> i,
|
||||
Type type,
|
||||
JsonSerializationContext context) {
|
||||
JsonArray ary = new JsonArray();
|
||||
for (Object obj : i)
|
||||
ary.add(context.serialize(obj, obj.getClass()));
|
||||
return ary;
|
||||
}
|
||||
|
||||
public Iterable<?> deserialize(JsonElement arg0, Type arg1,
|
||||
JsonDeserializationContext arg2) throws JsonParseException {
|
||||
return null; // handled elsewhere
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
static final Adapter<Date> DATE =
|
||||
new SimpleAdapter<Date>() {
|
||||
protected String serialize(Date t) {
|
||||
return ISODateTimeFormat.dateTime().print(new DateTime(t));
|
||||
}
|
||||
public Date apply(String v) {
|
||||
return DateTime.parse(v).toDate();
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<DateTime> DATETIME =
|
||||
new SimpleAdapter<DateTime>() {
|
||||
protected String serialize(DateTime t) {
|
||||
return ISODateTimeFormat.dateTime().print(t);
|
||||
}
|
||||
public DateTime apply(String v) {
|
||||
return DateTime.parse(v);
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<Duration> DURATION =
|
||||
new SimpleAdapter<Duration>() {
|
||||
public Duration apply(String v) {
|
||||
return Duration.parse(v);
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<Period> PERIOD =
|
||||
new SimpleAdapter<Period>() {
|
||||
public Period apply(String v) {
|
||||
return Period.parse(v);
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<Interval> INTERVAL =
|
||||
new SimpleAdapter<Interval>() {
|
||||
public Interval apply(String v) {
|
||||
return Interval.parse(v);
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<MediaType> MIMETYPE =
|
||||
new SimpleAdapter<MediaType>() {
|
||||
public MediaType apply(String v) {
|
||||
return MediaType.parse(v);
|
||||
}
|
||||
};
|
||||
|
||||
static final MultimapAdapter MULTIMAP =
|
||||
new MultimapAdapter();
|
||||
|
||||
|
||||
static final Adapter<Range<?>> RANGE =
|
||||
new Adapter<Range<?>>() {
|
||||
|
||||
public JsonElement serialize(
|
||||
Range<?> src,
|
||||
Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
JsonObject el = new JsonObject();
|
||||
el.add("lower", makeBound(src.lowerBoundType(),src.lowerEndpoint(),context));
|
||||
el.add("upper", makeBound(src.upperBoundType(),src.upperEndpoint(),context));
|
||||
return el;
|
||||
}
|
||||
|
||||
private JsonElement makeBound(
|
||||
BoundType type,
|
||||
Object val,
|
||||
JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.add("type", context.serialize(type.name().toLowerCase()));
|
||||
obj.add("endpoint", context.serialize(val));
|
||||
return obj;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Range<?> deserialize(
|
||||
JsonElement json,
|
||||
Type typeOfT,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
checkArgument(json.isJsonObject());
|
||||
try {
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
JsonObject upper = obj.getAsJsonObject("upper");
|
||||
JsonObject lower = obj.getAsJsonObject("lower");
|
||||
BoundType ubt = bt(upper.getAsJsonPrimitive("type"));
|
||||
BoundType lbt = bt(lower.getAsJsonPrimitive("type"));
|
||||
Object ub = des(upper.get("endpoint"),context);
|
||||
Object lb = des(lower.get("endpoint"),context);
|
||||
return Range.range((Comparable)lb, lbt, (Comparable)ub, ubt);
|
||||
} catch (Throwable t) {
|
||||
throw Throwables.propagate(t);
|
||||
}
|
||||
}
|
||||
|
||||
private Object des(JsonElement val, JsonDeserializationContext context) {
|
||||
if (val.isJsonArray())
|
||||
return MultimapAdapter.arraydes(val.getAsJsonArray(), context);
|
||||
else if (val.isJsonObject())
|
||||
return context.deserialize(val, ASObject.class);
|
||||
else if (val.isJsonPrimitive()) {
|
||||
Object v = primConverter.convert(val.getAsJsonPrimitive());
|
||||
if (v instanceof LazilyParsedNumber)
|
||||
v = new LazilyParsedNumberComparable((LazilyParsedNumber) v);
|
||||
return v;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
private BoundType bt(JsonPrimitive p) {
|
||||
try {
|
||||
return BoundType.valueOf(p.toString());
|
||||
} catch (Throwable t) {
|
||||
return BoundType.CLOSED;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<Optional<?>> OPTIONAL =
|
||||
new Adapter<Optional<?>>() {
|
||||
public JsonElement serialize(
|
||||
Optional<?> src,
|
||||
Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
return context.serialize(src.orNull());
|
||||
}
|
||||
public Optional<?> deserialize(
|
||||
JsonElement json,
|
||||
Type typeOfT,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
static final Adapter<Table<?,?,?>> TABLE =
|
||||
new Adapter<Table<?,?,?>>() {
|
||||
|
||||
public JsonElement serialize(
|
||||
Table<?, ?, ?> src,
|
||||
Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
|
||||
JsonObject obj = new JsonObject();
|
||||
for (Table.Cell<?, ?, ?> cell : src.cellSet()) {
|
||||
String r = cell.getRowKey().toString();
|
||||
String c = cell.getColumnKey().toString();
|
||||
JsonObject rowobj = null;
|
||||
if (!obj.has(r)) {
|
||||
rowobj = new JsonObject();
|
||||
obj.add(r, rowobj);
|
||||
} else {
|
||||
rowobj = obj.getAsJsonObject(r);
|
||||
}
|
||||
Object val = cell.getValue();
|
||||
if (val != null)
|
||||
rowobj.add(c, context.serialize(val,val.getClass()));
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
public Table<?, ?, ?> deserialize(
|
||||
JsonElement json,
|
||||
Type typeOfT,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
ImmutableTable.Builder<String,String,Object> table =
|
||||
ImmutableTable.builder();
|
||||
checkArgument(json.isJsonObject());
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
for (Map.Entry<String,JsonElement> rowentry : obj.entrySet()) {
|
||||
String row = rowentry.getKey();
|
||||
JsonElement cell = rowentry.getValue();
|
||||
checkArgument(cell.isJsonObject());
|
||||
for (Map.Entry<String,JsonElement> cellentry : cell.getAsJsonObject().entrySet()) {
|
||||
String ckey = cellentry.getKey();
|
||||
JsonElement val = cellentry.getValue();
|
||||
Object desval = null;
|
||||
if (val.isJsonArray())
|
||||
desval = MultimapAdapter.arraydes(val.getAsJsonArray(),context);
|
||||
else if (val.isJsonObject())
|
||||
desval = context.deserialize(val, ASObject.class);
|
||||
else if (val.isJsonPrimitive())
|
||||
desval = primConverter.convert(val.getAsJsonPrimitive());
|
||||
if (desval != null)
|
||||
table.put(row,ckey,desval);
|
||||
}
|
||||
}
|
||||
return table.build();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
|
@ -1,128 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
import static com.google.common.base.Enums.stringConverter;
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.ibm.common.activitystreams.util.Converters.stringConverter;
|
||||
import static com.ibm.common.activitystreams.util.Converters.toLowerConverter;
|
||||
import static com.ibm.common.activitystreams.util.Converters.toUpperConverter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.common.base.Converter;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public final class EnumAdapter<E extends Enum<E>>
|
||||
extends Adapter<E> {
|
||||
|
||||
protected static final Converter<String,String> toLower =
|
||||
toLowerConverter();
|
||||
protected static final Converter<String,String> toUpper =
|
||||
toUpperConverter();
|
||||
|
||||
private final Converter<String,E> des;
|
||||
private final Converter<E,String> ser;
|
||||
/**
|
||||
* Constructor for EnumAdapter.
|
||||
* @param _enumClass Class<E>
|
||||
*/
|
||||
public EnumAdapter(Class<E> _enumClass) {
|
||||
this(_enumClass, stringConverter(_enumClass));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for EnumAdapter
|
||||
|
||||
|
||||
* @param _enumClass Class<E>
|
||||
* @param or E
|
||||
*/
|
||||
public EnumAdapter(Class<E> _enumClass, E or) {
|
||||
this(_enumClass, stringConverter(_enumClass,or));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for EnumAdapter.
|
||||
* @param _enumClass Class<E>
|
||||
|
||||
* @param c Converter<String,E>
|
||||
*/
|
||||
public EnumAdapter(
|
||||
Class<E> _enumClass,
|
||||
Converter<String,E> c) {
|
||||
super();
|
||||
this.des = toUpper.andThen(c);
|
||||
this.ser = c.reverse().andThen(toLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param src E
|
||||
* @param typeOfSrc Type
|
||||
* @param context JsonSerializationContext
|
||||
* @return JsonElement
|
||||
*/
|
||||
public JsonElement serialize(
|
||||
E src,
|
||||
Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
return context.serialize(ser.convert(src));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param json JsonElement
|
||||
* @param typeOfT Type
|
||||
* @param context JsonDeserializationContext
|
||||
* @return E
|
||||
* @throws JsonParseException
|
||||
* @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
|
||||
*/
|
||||
public E deserialize(
|
||||
JsonElement json,
|
||||
Type typeOfT,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
checkArgument(json.isJsonPrimitive());
|
||||
JsonPrimitive jp = json.getAsJsonPrimitive();
|
||||
checkArgument(jp.isString());
|
||||
return des.convert(jp.getAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method convert.
|
||||
* @param s String
|
||||
* @return E
|
||||
*/
|
||||
protected E convert(String s) {
|
||||
return des.convert(s);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,341 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
import static com.google.gson.internal.bind.TypeAdapters.NUMBER;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.DATE;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.DATETIME;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.NLV;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.TABLE;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.OPTIONAL;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.ACTIONS;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.DURATION;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.INTERVAL;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.ITERABLE;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.MIMETYPE;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.MULTIMAP;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.RANGE;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.PERIOD;
|
||||
import static com.ibm.common.activitystreams.internal.Adapters.forEnum;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Date;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.ReadableDuration;
|
||||
import org.joda.time.ReadableInterval;
|
||||
import org.joda.time.ReadablePeriod;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.Range;
|
||||
import com.google.common.collect.Table;
|
||||
import com.google.common.net.MediaType;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.internal.LazilyParsedNumber;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.ActionsValue;
|
||||
import com.ibm.common.activitystreams.Activity;
|
||||
import com.ibm.common.activitystreams.Collection;
|
||||
import com.ibm.common.activitystreams.LinkValue;
|
||||
import com.ibm.common.activitystreams.NLV;
|
||||
import com.ibm.common.activitystreams.TypeValue;
|
||||
import com.ibm.common.activitystreams.Writable;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public final class GsonWrapper {
|
||||
|
||||
/**
|
||||
* Method make.
|
||||
|
||||
* @return Builder */
|
||||
public static final Builder make() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public static final class Builder
|
||||
implements Supplier<GsonWrapper> {
|
||||
|
||||
private String charset = "UTF-8";
|
||||
private boolean pretty;
|
||||
private Schema schema = null; // default
|
||||
private ImmutableList.Builder<AdapterEntry<?>> adapters =
|
||||
ImmutableList.builder();
|
||||
|
||||
/**
|
||||
* Method charset.
|
||||
* @param charset String
|
||||
|
||||
* @return Builder */
|
||||
public Builder charset(String charset) {
|
||||
this.charset = charset;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param schema Schema
|
||||
|
||||
* @return Builder */
|
||||
public Builder schema(Schema schema) {
|
||||
this.schema = schema;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param type Class<? extends T>
|
||||
* @param adapter Adapter<T>
|
||||
|
||||
* @return Builder */
|
||||
public <T>Builder adapter(
|
||||
Class<? extends T> type,
|
||||
Adapter<T> adapter) {
|
||||
return adapter(type,adapter,false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param type Class<? extends T>
|
||||
* @param adapter Adapter<T>
|
||||
* @param hier boolean
|
||||
|
||||
* @return Builder */
|
||||
public <T>Builder adapter(
|
||||
Class<? extends T> type,
|
||||
Adapter<T> adapter,
|
||||
boolean hier) {
|
||||
adapters.add(new AdapterEntry<T>(type,adapter,hier));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prettyPrint.
|
||||
* @param on boolean
|
||||
|
||||
* @return Builder */
|
||||
public Builder prettyPrint(boolean on) {
|
||||
this.pretty = on;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prettyPrint.
|
||||
|
||||
* @return Builder */
|
||||
public Builder prettyPrint() {
|
||||
return prettyPrint(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
|
||||
|
||||
* @return GsonWrapper * @see com.google.common.base.Supplier#get() */
|
||||
public GsonWrapper get() {
|
||||
return new GsonWrapper(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
private final static class AdapterEntry<T> {
|
||||
private final Class<? extends T> type;
|
||||
private final Adapter<T> adapter;
|
||||
private final boolean hier;
|
||||
/**
|
||||
* Constructor for AdapterEntry.
|
||||
* @param type Class<? extends T>
|
||||
* @param adapter Adapter<T>
|
||||
* @param hier boolean
|
||||
*/
|
||||
AdapterEntry(
|
||||
Class<? extends T> type,
|
||||
Adapter<T> adapter,
|
||||
boolean hier) {
|
||||
this.type = type;
|
||||
this.adapter = adapter;
|
||||
this.hier = hier;
|
||||
}
|
||||
}
|
||||
|
||||
private final Gson gson;
|
||||
private final String charset;
|
||||
|
||||
/**
|
||||
* Constructor for GsonWrapper.
|
||||
* @param builder Builder
|
||||
*/
|
||||
protected GsonWrapper(Builder builder) {
|
||||
Schema schema =
|
||||
builder.schema != null ?
|
||||
builder.schema :
|
||||
Schema.make().get();
|
||||
ASObjectAdapter base =
|
||||
new ASObjectAdapter(schema);
|
||||
GsonBuilder b = initGsonBuilder(
|
||||
builder,
|
||||
schema,
|
||||
base,
|
||||
builder.adapters.build());
|
||||
if (builder.pretty)
|
||||
b.setPrettyPrinting();
|
||||
this.gson = b.create();
|
||||
this.charset = builder.charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method initGsonBuilder.
|
||||
* @param builder Builder
|
||||
|
||||
* @return GsonBuilder */
|
||||
private static GsonBuilder initGsonBuilder(
|
||||
Builder builder,
|
||||
Schema schema,
|
||||
ASObjectAdapter base,
|
||||
Iterable<AdapterEntry<?>> adapters) {
|
||||
|
||||
GsonBuilder gson = new GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(TypeValue.class, new TypeValueAdapter(schema))
|
||||
.registerTypeHierarchyAdapter(LinkValue.class, new LinkValueAdapter(schema))
|
||||
.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)
|
||||
gson.registerTypeHierarchyAdapter(
|
||||
entry.type,
|
||||
entry.adapter!=null ?
|
||||
entry.adapter : base);
|
||||
else
|
||||
gson.registerTypeAdapter(
|
||||
entry.type,
|
||||
entry.adapter!=null ?
|
||||
entry.adapter:base);
|
||||
}
|
||||
|
||||
return gson;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Method write.
|
||||
* @param w Writable
|
||||
* @param out OutputStream
|
||||
*/
|
||||
public void write(Writable w, OutputStream out) {
|
||||
try {
|
||||
OutputStreamWriter wout =
|
||||
new OutputStreamWriter(out, charset);
|
||||
gson.toJson(w,wout);
|
||||
wout.flush();
|
||||
} catch (Throwable t) {
|
||||
throw Throwables.propagate(t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method write.
|
||||
* @param w Writable
|
||||
* @param out Writer
|
||||
*/
|
||||
public void write(Writable w, Writer out) {
|
||||
gson.toJson(w,out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method write.
|
||||
* @param w Writable
|
||||
|
||||
* @return String */
|
||||
public String write(Writable w) {
|
||||
StringWriter sw =
|
||||
new StringWriter();
|
||||
write(w,sw);
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method readAs.
|
||||
* @param in InputStream
|
||||
* @param type Class<? extends A>
|
||||
|
||||
* @return A */
|
||||
public <A extends ASObject>A readAs(InputStream in, Class<? extends A> type) {
|
||||
try {
|
||||
return readAs(new InputStreamReader(in, charset), type);
|
||||
} catch (Throwable t) {
|
||||
throw Throwables.propagate(t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method readAs.
|
||||
* @param in Reader
|
||||
* @param type Class<? extends A>
|
||||
|
||||
* @return A */
|
||||
public <A extends ASObject>A readAs(Reader in, Class<? extends A> type) {
|
||||
return (A)gson.fromJson(in, type);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,128 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
|
||||
import com.google.common.primitives.Longs;
|
||||
import com.google.gson.internal.LazilyParsedNumber;
|
||||
|
||||
/**
|
||||
*/
|
||||
final class LazilyParsedNumberComparable
|
||||
extends Number
|
||||
implements Comparable<Number> {
|
||||
private static final long serialVersionUID = 761729254455440231L;
|
||||
private final LazilyParsedNumber inner;
|
||||
/**
|
||||
* Constructor for LazilyParsedNumberComparable.
|
||||
* @param inner LazilyParsedNumber
|
||||
*/
|
||||
public LazilyParsedNumberComparable(LazilyParsedNumber inner) {
|
||||
this.inner = inner;
|
||||
}
|
||||
/**
|
||||
* Method compareTo.
|
||||
* @param o Number
|
||||
* @return int
|
||||
*/
|
||||
public int compareTo(Number o) {
|
||||
return Longs.compare(inner.longValue(), o.longValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doubleValue.
|
||||
* @return double
|
||||
*/
|
||||
public double doubleValue() {
|
||||
return inner.doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method floatValue.
|
||||
* @return float
|
||||
*/
|
||||
public float floatValue() {
|
||||
return inner.floatValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method intValue.
|
||||
* @return int
|
||||
*/
|
||||
public int intValue() {
|
||||
return inner.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method longValue.
|
||||
* @return long
|
||||
*/
|
||||
public long longValue() {
|
||||
return inner.longValue();
|
||||
}
|
||||
/**
|
||||
* Method byteValue.
|
||||
* @return byte
|
||||
*/
|
||||
public byte byteValue() {
|
||||
return inner.byteValue();
|
||||
}
|
||||
/**
|
||||
* Method shortValue.
|
||||
* @return short
|
||||
*/
|
||||
public short shortValue() {
|
||||
return inner.shortValue();
|
||||
}
|
||||
/**
|
||||
* Method equals.
|
||||
* @param obj Object
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
return inner.equals(obj);
|
||||
}
|
||||
/**
|
||||
* Method hashCode.
|
||||
* @return int
|
||||
*/
|
||||
public int hashCode() {
|
||||
return inner.hashCode();
|
||||
}
|
||||
/**
|
||||
* Method toString.
|
||||
* @return String
|
||||
*/
|
||||
public String toString() {
|
||||
return inner.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method writeReplace.
|
||||
* @return Object
|
||||
* @throws ObjectStreamException
|
||||
*/
|
||||
private Object writeReplace() throws ObjectStreamException {
|
||||
return inner;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,136 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.ibm.common.activitystreams.Makers.linkValue;
|
||||
import static com.ibm.common.activitystreams.Makers.linkValues;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
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.LinkValue;
|
||||
import com.ibm.common.activitystreams.LinkValue.SimpleLinkValue;
|
||||
import com.ibm.common.activitystreams.TypeValue;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
final class LinkValueAdapter
|
||||
extends Adapter<LinkValue> {
|
||||
|
||||
private final Schema schema;
|
||||
|
||||
/**
|
||||
* Constructor for LinkValueAdapter.
|
||||
* @param schema Schema
|
||||
*/
|
||||
public LinkValueAdapter(Schema schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param value LinkValue
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
|
||||
* @return JsonElement */
|
||||
public JsonElement serialize(
|
||||
LinkValue value,
|
||||
Type type,
|
||||
JsonSerializationContext context) {
|
||||
switch(value.valueType()) {
|
||||
case SIMPLE:
|
||||
LinkValue.SimpleLinkValue simple = (SimpleLinkValue) value;
|
||||
return context.serialize(simple.url(), String.class);
|
||||
case ARRAY:
|
||||
return context.serialize(value, Iterable.class);
|
||||
case OBJECT:
|
||||
return context.serialize(value, ASObject.class);
|
||||
default:
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param el JsonElement
|
||||
* @param type Type
|
||||
* @param context JsonDeserializationContext
|
||||
|
||||
|
||||
|
||||
* @return LinkValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
|
||||
public LinkValue deserialize(
|
||||
JsonElement el,
|
||||
Type type,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
checkArgument(
|
||||
el.isJsonArray() ||
|
||||
el.isJsonObject() ||
|
||||
el.isJsonPrimitive());
|
||||
if (el.isJsonArray()) {
|
||||
LinkValue.ArrayLinkValue.Builder builder =
|
||||
linkValues();
|
||||
for (JsonElement aryel : el.getAsJsonArray())
|
||||
builder.add(
|
||||
context.<LinkValue>deserialize(
|
||||
aryel,
|
||||
LinkValue.class));
|
||||
return builder.get();
|
||||
} else if (el.isJsonObject()) {
|
||||
JsonObject obj = el.getAsJsonObject();
|
||||
if (obj.has("objectType")) {
|
||||
TypeValue tv =
|
||||
context.deserialize(
|
||||
obj.get("objectType"),
|
||||
TypeValue.class);
|
||||
Model pMap =
|
||||
schema.forObjectType(tv.id());
|
||||
return context.deserialize(
|
||||
el,
|
||||
pMap != null && pMap.type() != null ?
|
||||
pMap.type() :
|
||||
ASObject.class);
|
||||
} else {
|
||||
return context.deserialize(
|
||||
el,
|
||||
ASObject.class);
|
||||
}
|
||||
} else {
|
||||
JsonPrimitive prim =
|
||||
el.getAsJsonPrimitive();
|
||||
checkArgument(prim.isString());
|
||||
return linkValue(prim.getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,529 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.ReadableDuration;
|
||||
import org.joda.time.ReadableInterval;
|
||||
import org.joda.time.ReadablePeriod;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.ASObject.AbstractBuilder;
|
||||
import com.ibm.common.activitystreams.LinkValue;
|
||||
import com.ibm.common.activitystreams.NLV;
|
||||
import com.ibm.common.activitystreams.TypeValue;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public final class Model {
|
||||
|
||||
final String parent;
|
||||
final ImmutableMap<String,Type> properties;
|
||||
final Type _default;
|
||||
final Type _type;
|
||||
final Type _builder;
|
||||
private transient Schema schema;
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param schema Schema
|
||||
*/
|
||||
protected void schema(Schema schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method make.
|
||||
|
||||
* @return Builder */
|
||||
public static Builder make() {
|
||||
return make(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method make.
|
||||
* @param parent String
|
||||
|
||||
* @return Builder */
|
||||
public static Builder make(String parent) {
|
||||
return new Builder().parent(parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public static final class Builder
|
||||
implements Supplier<Model> {
|
||||
|
||||
Type _type;
|
||||
Type _builder;
|
||||
Type _default = null;
|
||||
String parent = null;
|
||||
|
||||
final Map<String,Type> properties =
|
||||
Maps.newHashMap();
|
||||
|
||||
Builder() {}
|
||||
|
||||
/**
|
||||
* Constructor for Builder.
|
||||
* @param template PropertyMap
|
||||
*/
|
||||
Builder(Model template) {
|
||||
this.parent = template.parent;
|
||||
this._default = template._default;
|
||||
this._type = template._type;
|
||||
this._builder = template._builder;
|
||||
this.properties.putAll(template.properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method withDefault.
|
||||
* @param _default Class<?>
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder withDefault(Class<?> _default) {
|
||||
this._default = _default;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @param _type Class<?>
|
||||
* @param _builder Class<?>
|
||||
* @return Builder
|
||||
**/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Builder type(
|
||||
Class<?> _type, Class<?> _builder) {
|
||||
this._type = (Class<? extends ASObject>) _type;
|
||||
this._builder = (Class<? extends ASObject.AbstractBuilder<?, ?>>) _builder;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method parent.
|
||||
* @param parent String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder parent(String parent) {
|
||||
this.parent = parent;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method naturalLanguageValue.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder naturalLanguageValue(String name) {
|
||||
return as(name, NLV.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method naturalLanguageValue.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder naturalLanguageValue(String... names) {
|
||||
for (String name : names)
|
||||
naturalLanguageValue(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method object.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder object(String name) {
|
||||
return as(name, ASObject.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method object.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder object(String... names) {
|
||||
for (String name : names)
|
||||
object(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method integer.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder integer(String name) {
|
||||
return as(name, Integer.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method integer.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder integer(String... names) {
|
||||
for (String name : names)
|
||||
integer(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doub.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder doub(String name) {
|
||||
return as(name, Double.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doub.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder doub(String... names) {
|
||||
for (String name : names)
|
||||
doub(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doub.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder floatValue(String name) {
|
||||
return as(name, Float.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doub.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder floatValue(String... names) {
|
||||
for (String name : names)
|
||||
floatValue(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method string.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder string(String name) {
|
||||
return as(name, String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method string.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder string(String... names) {
|
||||
for (String name : names)
|
||||
string(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method linkValue.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder linkValue(String name) {
|
||||
return as(name, LinkValue.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method linkValue.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder linkValue(String... names) {
|
||||
for (String name : names)
|
||||
linkValue(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method dateTime.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder dateTime(String name) {
|
||||
return as(name, DateTime.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method dateTime.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
**/
|
||||
public Builder dateTime(String... names) {
|
||||
for (String name : names)
|
||||
dateTime(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method duration.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder duration(String name) {
|
||||
return as(name, ReadableDuration.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method duration.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder duration(String... names) {
|
||||
for (String name : names)
|
||||
duration(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method period.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder period(String name) {
|
||||
return as (name, ReadablePeriod.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method period.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder period(String... names) {
|
||||
for (String name: names)
|
||||
period(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method interval.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder interval(String name) {
|
||||
return as(name, ReadableInterval.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method interval.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
public Builder interval(String... names) {
|
||||
for (String name: names)
|
||||
interval(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method typeValue.
|
||||
* @param name String
|
||||
|
||||
* @return Builder */
|
||||
public Builder typeValue(String name) {
|
||||
return as(name, TypeValue.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method typeValue.
|
||||
* @param names String[]
|
||||
|
||||
* @return Builder */
|
||||
public Builder typeValue(String... names) {
|
||||
for (String name : names)
|
||||
typeValue(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method as.
|
||||
* @param name String
|
||||
* @param _class Class<?>
|
||||
|
||||
* @return Builder */
|
||||
public Builder as(String name, Class<?> _class) {
|
||||
this.properties.put(name,_class);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
|
||||
|
||||
* @return PropertyMap * @see com.google.common.base.Supplier#get() */
|
||||
public Model get() {
|
||||
return new Model(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for PropertyMap.
|
||||
* @param builder Builder
|
||||
*/
|
||||
Model(Builder builder) {
|
||||
this.parent = builder.parent;
|
||||
this.properties = ImmutableMap.copyOf(builder.properties);
|
||||
this._default = builder._default;
|
||||
this._type = builder._type;
|
||||
this._builder = builder._builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parentPropertyMap.
|
||||
|
||||
* @return PropertyMap */
|
||||
protected Model parentPropertyMap() {
|
||||
if (schema == null)
|
||||
return null;
|
||||
if (parent == null)
|
||||
return null;
|
||||
return schema.forObjectClassOrType(
|
||||
ASObject.Builder.class,
|
||||
parent.equals("object") ? null : parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @param name String
|
||||
|
||||
* @return Class<?> */
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Type>T get(String name) {
|
||||
Model parent = parentPropertyMap();
|
||||
return (T)(properties.containsKey(name) ?
|
||||
properties.get(name) :
|
||||
parent != null && _default == null ?
|
||||
parent.get(name) : _default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method containsKey.
|
||||
* @param name String
|
||||
|
||||
* @return boolean */
|
||||
public boolean has(String name) {
|
||||
Model parent = parentPropertyMap();
|
||||
return properties.containsKey(name) ?
|
||||
true : parent != null ?
|
||||
parent.has(name) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
|
||||
* @return Class<? extends ASObject> */
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<? extends ASObject> type() {
|
||||
return (Class<? extends ASObject>) _type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method builder.
|
||||
|
||||
* @return Class<? extends ASObject.AbstractBuilder<?,?>> */
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<? extends ASObject.AbstractBuilder<?,?>> builder() {
|
||||
return (Class<? extends AbstractBuilder<?, ?>>) _builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toString.
|
||||
|
||||
* @return String */
|
||||
public String toString() {
|
||||
return Objects.toStringHelper(Model.class)
|
||||
.omitNullValues()
|
||||
.add("Parent", parent)
|
||||
.add("Properties", properties)
|
||||
.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method template.
|
||||
|
||||
* @return Builder */
|
||||
public Builder template() {
|
||||
return new Builder(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method template.
|
||||
* @param _type Class<? extends B>
|
||||
* @param _builder Class<? extends X>
|
||||
|
||||
* @return PropertyMap */
|
||||
public <B extends ASObject, X extends ASObject.AbstractBuilder<B,X>> Model template(
|
||||
Class<? extends B> _type, Class<? extends X> _builder) {
|
||||
return template().type(_type,_builder).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method set.
|
||||
* @param name String
|
||||
* @param type Class<?>
|
||||
|
||||
* @return PropertyMap */
|
||||
public Model set(String name, Class<?> type) {
|
||||
return template()
|
||||
.as(name, type)
|
||||
.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method set.
|
||||
* @param map Map<String,Class<?>>
|
||||
|
||||
* @return PropertyMap */
|
||||
public Model set(Map<String,Class<?>> map) {
|
||||
Builder builder = template();
|
||||
for (Map.Entry<String,Class<?>> entry : map.entrySet())
|
||||
builder.as(entry.getKey(),entry.getValue());
|
||||
return builder.get();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,143 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
import static com.google.common.collect.Iterables.getFirst;
|
||||
import static com.google.common.collect.Iterables.size;
|
||||
|
||||
import static com.ibm.common.activitystreams.internal.ASObjectAdapter.primConverter;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.gson.JsonArray;
|
||||
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.ASObject;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes","unchecked"})
|
||||
final class MultimapAdapter
|
||||
extends Adapter<Multimap> {
|
||||
|
||||
/**
|
||||
* Method getAdaptedClass.
|
||||
|
||||
* @return Class<Multimap> */
|
||||
public Class<Multimap> getAdaptedClass() {
|
||||
return Multimap.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param src Multimap
|
||||
* @param typeOfSrc Type
|
||||
* @param context JsonSerializationContext
|
||||
|
||||
* @return JsonElement */
|
||||
public JsonElement serialize(
|
||||
Multimap src,
|
||||
Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
for (Object key : src.keySet()) {
|
||||
Iterable<Object> vals = src.get(key);
|
||||
if (size(vals) == 1) {
|
||||
Object f = getFirst(vals, null);
|
||||
if (f != null)
|
||||
obj.add(key.toString(), context.serialize(f, f.getClass()));
|
||||
} else {
|
||||
obj.add(key.toString(), context.serialize(vals, Iterable.class));
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method arraydes.
|
||||
* @param array JsonArray
|
||||
* @param context JsonDeserializationContext
|
||||
|
||||
* @return ImmutableList<Object> */
|
||||
protected static ImmutableList<Object> arraydes(
|
||||
JsonArray array,
|
||||
JsonDeserializationContext context) {
|
||||
ImmutableList.Builder<Object> builder =
|
||||
ImmutableList.builder();
|
||||
for (JsonElement child : array)
|
||||
if (child.isJsonArray())
|
||||
builder.add(arraydes(child.getAsJsonArray(),context));
|
||||
else if (child.isJsonObject())
|
||||
builder.add(context.deserialize(child, ASObject.class));
|
||||
else if (child.isJsonPrimitive())
|
||||
builder.add(primConverter.convert(child.getAsJsonPrimitive()));
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param json JsonElement
|
||||
* @param typeOfT Type
|
||||
* @param context JsonDeserializationContext
|
||||
|
||||
|
||||
|
||||
* @return Multimap * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
|
||||
public Multimap deserialize(
|
||||
JsonElement json,
|
||||
Type typeOfT,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
ImmutableMultimap.Builder mm =
|
||||
ImmutableMultimap.builder();
|
||||
JsonObject obj = json.getAsJsonObject();
|
||||
for (Map.Entry<String,JsonElement> entry : obj.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
JsonElement val = entry.getValue();
|
||||
if (val.isJsonArray()) {
|
||||
for (JsonElement el : val.getAsJsonArray()) {
|
||||
if (el.isJsonArray())
|
||||
mm.put(key, arraydes(el.getAsJsonArray(),context));
|
||||
else if (el.isJsonObject())
|
||||
mm.put(key, context.deserialize(el, ASObject.class));
|
||||
else if (el.isJsonPrimitive())
|
||||
mm.put(key,primConverter.convert(el.getAsJsonPrimitive()));
|
||||
}
|
||||
} else if (val.isJsonObject()) {
|
||||
mm.put(key, context.deserialize(val, ASObject.class));
|
||||
} else if (val.isJsonPrimitive()) {
|
||||
mm.put(key, primConverter.convert(val.getAsJsonPrimitive()));
|
||||
}
|
||||
}
|
||||
return mm.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,116 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
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.NLV;
|
||||
import com.ibm.common.activitystreams.NLV.MapNLV;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
final class NaturalLanguageValueAdapter
|
||||
extends Adapter<NLV> {
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param nlv NLV
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
|
||||
* @return JsonElement */
|
||||
public JsonElement serialize(
|
||||
NLV nlv,
|
||||
Type type,
|
||||
JsonSerializationContext context) {
|
||||
JsonElement el = null;
|
||||
switch (nlv.valueType()) {
|
||||
case SIMPLE:
|
||||
el = context.serialize(((NLV.SimpleNLV)nlv).value());
|
||||
break;
|
||||
case OBJECT:
|
||||
NLV.MapNLV map =
|
||||
(MapNLV) nlv;
|
||||
JsonObject obj = new JsonObject();
|
||||
for (String lang : map)
|
||||
obj.addProperty(
|
||||
lang.toString(),
|
||||
map.value(lang));
|
||||
el = obj;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
return el;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param element JsonElement
|
||||
* @param type1 Type
|
||||
* @param context JsonDeserializationContext
|
||||
|
||||
|
||||
|
||||
* @return NLV * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
|
||||
public NLV deserialize(
|
||||
JsonElement element,
|
||||
Type type1,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
checkArgument(
|
||||
element.isJsonPrimitive() ||
|
||||
element.isJsonObject());
|
||||
if (element.isJsonPrimitive()) {
|
||||
JsonPrimitive prim =
|
||||
element.getAsJsonPrimitive();
|
||||
checkArgument(prim.isString());
|
||||
return NLV.SimpleNLV.make(
|
||||
prim.getAsString());
|
||||
} else {
|
||||
try {
|
||||
JsonObject obj =
|
||||
element.getAsJsonObject();
|
||||
NLV.MapNLV.Builder builder =
|
||||
NLV.MapNLV.make();
|
||||
for (Entry<String,JsonElement> entry : obj.entrySet())
|
||||
builder.set(
|
||||
entry.getKey(),
|
||||
entry.getValue().getAsString());
|
||||
return builder.get();
|
||||
} catch (Throwable t) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,442 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
import static com.google.common.base.Objects.toStringHelper;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static com.google.common.collect.HashBiMap.create;
|
||||
import static com.google.common.collect.Iterables.addAll;
|
||||
import static com.google.common.collect.Maps.newHashMap;
|
||||
import static com.google.common.collect.Sets.newHashSet;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.joda.time.Duration;
|
||||
|
||||
import com.google.common.base.Converter;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.ImmutableBiMap;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.net.MediaType;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.ActionsValue;
|
||||
import com.ibm.common.activitystreams.Activity;
|
||||
import com.ibm.common.activitystreams.Collection;
|
||||
|
||||
/**
|
||||
* @TODO: The Schema mechanism needs to be revisited and reworked
|
||||
* to be much more efficient.
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public final class Schema {
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public static final class Builder
|
||||
implements Supplier<Schema> {
|
||||
|
||||
final Map<String,Model> objectTypeMap =
|
||||
newHashMap();
|
||||
final Map<String,Class<? extends ASObject.AbstractBuilder>> builderMap =
|
||||
newHashMap();
|
||||
final Map<Class<? extends ASObject.AbstractBuilder>,Model> objectClassMap =
|
||||
newHashMap();
|
||||
final BiMap<Class<? extends ASObject>, Class<? extends ASObject.AbstractBuilder>> classMap =
|
||||
create(100);
|
||||
final Set<Adapter<?>> adapters =
|
||||
newHashSet();
|
||||
|
||||
Builder() {}
|
||||
|
||||
/**
|
||||
* Constructor for Builder.
|
||||
* @param template Schema
|
||||
*/
|
||||
Builder(Schema template) {
|
||||
this.objectTypeMap.putAll(template.objectTypeMap);
|
||||
this.builderMap.putAll(template.builderMap);
|
||||
this.objectClassMap.putAll(template.objectClassMap);
|
||||
this.classMap.putAll(template.classMap);
|
||||
this.adapters.addAll(template.adapters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param _enumClass Class<E>
|
||||
|
||||
* @return Builder */
|
||||
public <E extends Enum<E>>Builder adapter(
|
||||
Class<E> _enumClass) {
|
||||
return adapter(new EnumAdapter<E>(_enumClass));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param _enumClass Class<E>
|
||||
|
||||
|
||||
* @param c Converter<String,E>
|
||||
* @return Builder */
|
||||
public <E extends Enum<E>>Builder adapter(
|
||||
Class<E> _enumClass,
|
||||
Converter<String,E> c) {
|
||||
return adapter(new EnumAdapter<E>(_enumClass,c));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter
|
||||
* @param _enumClass Class<E>
|
||||
* @param or E
|
||||
* @return Builder
|
||||
*/
|
||||
public <E extends Enum<E>>Builder adapter(
|
||||
Class<E> _enumClass, E or) {
|
||||
return adapter(new EnumAdapter<E>(_enumClass,or));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param adapter Adapter<?>
|
||||
|
||||
* @return Builder */
|
||||
public Builder adapter(Adapter<?> adapter) {
|
||||
this.adapters.add(adapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param adapters Adapter<?>[]
|
||||
|
||||
* @return Builder */
|
||||
public Builder adapter(Adapter<?>... adapters) {
|
||||
if (adapters == null) return this;
|
||||
for (Adapter<?> a : adapters)
|
||||
adapter(a);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param adapters Iterable<Adapter<?>>
|
||||
|
||||
* @return Builder */
|
||||
public Builder adapter(Iterable<Adapter<?>> adapters) {
|
||||
if (adapters == null) return this;
|
||||
addAll(this.adapters, adapters);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method map.
|
||||
* @param objectType String
|
||||
* @param propertyMap Supplier<PropertyMap>
|
||||
|
||||
* @return Builder */
|
||||
public Builder map(String objectType, Supplier<Model> propertyMap) {
|
||||
return map(objectType, propertyMap.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method map.
|
||||
* @param propertyMap Supplier<PropertyMap>
|
||||
|
||||
* @return Builder */
|
||||
public Builder map(Supplier<Model> propertyMap) {
|
||||
return map(null,propertyMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method map.
|
||||
* @param propertyMap PropertyMap
|
||||
|
||||
* @return Builder */
|
||||
public Builder map(Model propertyMap) {
|
||||
return map(null, propertyMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method map.
|
||||
* @param objectType String
|
||||
* @param propertyMap PropertyMap
|
||||
|
||||
* @return Builder */
|
||||
public Builder map(String objectType, Model propertyMap) {
|
||||
checkNotNull(propertyMap);
|
||||
Class<? extends ASObject.AbstractBuilder<?,?>> _builder = propertyMap.builder();
|
||||
Class<? extends ASObject> _type = propertyMap.type();
|
||||
if (objectType != null) objectTypeMap.put(objectType, propertyMap);
|
||||
if (objectType != null && _builder != null)
|
||||
builderMap.put(objectType,_builder);
|
||||
if (_builder != null) objectClassMap.put(_builder, propertyMap);
|
||||
if (_builder != null && _type != null)
|
||||
classMap.put(_type,_builder);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Model model(String objectType) {
|
||||
return objectTypeMap.get(objectType);
|
||||
}
|
||||
|
||||
public Model model() {
|
||||
return objectClassMap.get(ASObject.Builder.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return Schema * @see com.google.common.base.Supplier#get() */
|
||||
public Schema get() {
|
||||
return new Schema(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final ImmutableMap<String,Model> objectTypeMap;
|
||||
final ImmutableMap<Class<? extends ASObject.AbstractBuilder>, Model> objectClassMap;
|
||||
final ImmutableMap<String,Class<? extends ASObject.AbstractBuilder>> builderMap;
|
||||
final ImmutableBiMap<Class<? extends ASObject>, Class<? extends ASObject.AbstractBuilder>> classMap;
|
||||
final ImmutableSet<Adapter<?>> adapters;
|
||||
|
||||
/**
|
||||
* Constructor for Schema.
|
||||
* @param builder Builder
|
||||
*/
|
||||
Schema(Builder builder) {
|
||||
this.objectClassMap = ImmutableMap.copyOf(builder.objectClassMap);
|
||||
this.objectTypeMap = ImmutableMap.copyOf(builder.objectTypeMap);
|
||||
this.builderMap = ImmutableMap.copyOf(builder.builderMap);
|
||||
this.adapters = ImmutableSet.copyOf(builder.adapters);
|
||||
this.classMap = ImmutableBiMap.copyOf(builder.classMap);
|
||||
|
||||
for (Model pmap : objectTypeMap.values())
|
||||
pmap.schema(this);
|
||||
for (Model pmap : objectClassMap.values())
|
||||
pmap.schema(this);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapters.
|
||||
|
||||
* @return Iterable<Adapter<?>> */
|
||||
public Iterable<Adapter<?>> adapters() {
|
||||
return adapters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method builderForObjectTypeOrClass.
|
||||
* @param ots String
|
||||
* @param _class Class<?>
|
||||
|
||||
* @return Class<? extends ASObject.AbstractBuilder> */
|
||||
public Class<? extends ASObject.AbstractBuilder> builderForObjectTypeOrClass(String ots, Class<?> _class) {
|
||||
Class<? extends ASObject.AbstractBuilder> _builder = builderFor(ots);
|
||||
return _builder != null ?
|
||||
_builder : builderFor(_builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method builderFor.
|
||||
* @param _class Class<?>
|
||||
|
||||
* @return Class<? extends ASObject.AbstractBuilder> */
|
||||
public Class<? extends ASObject.AbstractBuilder> builderFor(Class<?> _class) {
|
||||
if (_class == null) return null;
|
||||
return classMap.get(_class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method classFor.
|
||||
* @param _builder Class<? extends ASObject.AbstractBuilder>
|
||||
|
||||
* @return Class<? extends ASObject> */
|
||||
public Class<? extends ASObject> classFor(Class<? extends ASObject.AbstractBuilder> _builder) {
|
||||
if (_builder == null) return null;
|
||||
return classMap.inverse().get(_builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method builderFor.
|
||||
* @param ots String
|
||||
|
||||
* @return Class<? extends ASObject.AbstractBuilder> */
|
||||
public Class<? extends ASObject.AbstractBuilder> builderFor(String ots) {
|
||||
if (ots == null) return null;
|
||||
return builderMap.get(ots);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method forObjectType.
|
||||
* @param objectType String
|
||||
|
||||
* @return PropertyMap */
|
||||
public Model forObjectType(String objectType) {
|
||||
return objectTypeMap.get(objectType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method forObjectClass.
|
||||
* @param _class Class<? extends ASObject.AbstractBuilder>
|
||||
|
||||
* @return PropertyMap */
|
||||
public Model forObjectClass(Class<? extends ASObject.AbstractBuilder> _class) {
|
||||
return objectClassMap.get(_class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method forObjectClassOrType.
|
||||
* @param _class Class<? extends ASObject.AbstractBuilder>
|
||||
* @param objectType String
|
||||
|
||||
* @return PropertyMap */
|
||||
public Model forObjectClassOrType(Class<? extends ASObject.AbstractBuilder> _class, String objectType) {
|
||||
Model pm = forObjectClass(_class);
|
||||
return pm != null ? pm : forObjectType(objectType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method template.
|
||||
|
||||
* @return Builder */
|
||||
public Builder template() {
|
||||
return new Builder(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toString.
|
||||
|
||||
* @return String */
|
||||
public String toString() {
|
||||
return toStringHelper(Schema.class)
|
||||
.add("Object Types", objectTypeMap)
|
||||
.add("Object Classes", objectClassMap)
|
||||
.toString();
|
||||
}
|
||||
|
||||
public final static Model object =
|
||||
Model
|
||||
.make()
|
||||
.type(
|
||||
ASObject.class,
|
||||
ASObject.Builder.class)
|
||||
.typeValue(
|
||||
"objectType")
|
||||
.linkValue(
|
||||
"attachments",
|
||||
"author",
|
||||
"duplicates",
|
||||
"icon",
|
||||
"image",
|
||||
"location",
|
||||
"inReplyTo",
|
||||
"tags",
|
||||
"url",
|
||||
"generator",
|
||||
"provider",
|
||||
"scope"
|
||||
)
|
||||
.dateTime(
|
||||
"published",
|
||||
"updated",
|
||||
"startTime",
|
||||
"endTime")
|
||||
.naturalLanguageValue(
|
||||
"summary",
|
||||
"title",
|
||||
"content",
|
||||
"displayName")
|
||||
.as("language", String.class)
|
||||
.as("actions", ActionsValue.class)
|
||||
.string("id", "rel", "alias")
|
||||
.doub("rating")
|
||||
.integer("height", "width")
|
||||
.as("mediaType", MediaType.class)
|
||||
.as("duration", Duration.class)
|
||||
.get();
|
||||
|
||||
public final static Model activity =
|
||||
Model
|
||||
.make("object")
|
||||
.type(
|
||||
Activity.class,
|
||||
Activity.Builder.class)
|
||||
.typeValue("verb")
|
||||
.linkValue(
|
||||
"actor",
|
||||
"participant",
|
||||
"instrument",
|
||||
"object",
|
||||
"target",
|
||||
"result",
|
||||
"to",
|
||||
"bto",
|
||||
"cc",
|
||||
"bcc")
|
||||
.doub("priority")
|
||||
.as("status", Activity.Status.class)
|
||||
.get();
|
||||
|
||||
public final static Model collection =
|
||||
Model
|
||||
.make("object")
|
||||
.type(
|
||||
Collection.class,
|
||||
Collection.Builder.class)
|
||||
.dateTime(
|
||||
"itemsBefore",
|
||||
"itemsAfter")
|
||||
.linkValue(
|
||||
"first",
|
||||
"last",
|
||||
"prev",
|
||||
"next",
|
||||
"current",
|
||||
"self")
|
||||
.object("items")
|
||||
.integer(
|
||||
"startIndex",
|
||||
"itemsPerPage",
|
||||
"totalItems")
|
||||
.get();
|
||||
|
||||
/**
|
||||
* Method make.
|
||||
|
||||
* @return Schema.Builder */
|
||||
public static Schema.Builder make() {
|
||||
return
|
||||
new Builder()
|
||||
.map(null, object)
|
||||
.map("activity", activity)
|
||||
.map("collection", collection);
|
||||
}
|
||||
|
||||
public static final Schema DEFAULT_SCHEMA = make().get();
|
||||
|
||||
}
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public abstract class SimpleAdapter<T>
|
||||
extends Adapter<T>
|
||||
implements Function<String,T> {
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param t T
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
|
||||
|
||||
* @return JsonElement * @see com.google.gson.JsonSerializer#serialize(T, Type, JsonSerializationContext) */
|
||||
public JsonElement serialize(
|
||||
T t,
|
||||
Type type,
|
||||
JsonSerializationContext context) {
|
||||
return context.serialize(serialize(t));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param t T
|
||||
|
||||
* @return String */
|
||||
protected String serialize(T t) {
|
||||
return t != null ? t.toString() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param v String
|
||||
|
||||
* @return T */
|
||||
protected T deserialize(String v) {
|
||||
return apply(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param json JsonElement
|
||||
* @param type Type
|
||||
* @param context JsonDeserializationContext
|
||||
|
||||
|
||||
|
||||
* @return T * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
|
||||
public T deserialize(
|
||||
JsonElement json,
|
||||
Type type,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
return deserialize(json.getAsJsonPrimitive().getAsString());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,121 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.ibm.common.activitystreams.Makers.type;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
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.TypeValue;
|
||||
import com.ibm.common.activitystreams.ValueType;
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
final class TypeValueAdapter
|
||||
extends Adapter<TypeValue> {
|
||||
|
||||
private final Schema schema;
|
||||
|
||||
/**
|
||||
* Constructor for TypeValueAdapter.
|
||||
* @param schema Schema
|
||||
*/
|
||||
public TypeValueAdapter(
|
||||
Schema schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param value TypeValue
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
|
||||
* @return JsonElement */
|
||||
public JsonElement serialize(
|
||||
TypeValue value,
|
||||
Type type,
|
||||
JsonSerializationContext context) {
|
||||
boolean simple = value.valueType() == ValueType.SIMPLE;
|
||||
return context.serialize(
|
||||
simple ? value.id() : value,
|
||||
simple ? String.class : ASObject.class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param el JsonElement
|
||||
* @param type Type
|
||||
* @param context JsonDeserializationContext
|
||||
|
||||
|
||||
|
||||
* @return TypeValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
|
||||
public TypeValue deserialize(
|
||||
JsonElement el,
|
||||
Type type,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
checkArgument(
|
||||
el.isJsonPrimitive() ||
|
||||
el.isJsonObject());
|
||||
if (el.isJsonPrimitive()) {
|
||||
JsonPrimitive prim =
|
||||
el.getAsJsonPrimitive();
|
||||
checkArgument(prim.isString());
|
||||
return type(prim.getAsString());
|
||||
} else {
|
||||
JsonObject obj = el.getAsJsonObject();
|
||||
if (obj.has("objectType")) {
|
||||
TypeValue tv =
|
||||
context.deserialize(
|
||||
obj.get("objectType"),
|
||||
TypeValue.class);
|
||||
Model pMap =
|
||||
schema.forObjectType(tv.id());
|
||||
return
|
||||
context.<ASObject>deserialize(
|
||||
el,
|
||||
pMap.type() != null ?
|
||||
pMap.type() :
|
||||
ASObject.class);
|
||||
} else {
|
||||
return
|
||||
context.<ASObject>deserialize(
|
||||
el,
|
||||
ASObject.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,305 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.util;
|
||||
|
||||
import static com.google.common.collect.ImmutableList.of;
|
||||
import static com.google.common.collect.Iterables.filter;
|
||||
import static com.google.common.collect.Iterables.size;
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
import static com.google.common.collect.Maps.difference;
|
||||
import static com.ibm.common.activitystreams.Makers.linkValue;
|
||||
import static com.ibm.common.activitystreams.Makers.linkValues;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ibm.common.activitystreams.ASObject;
|
||||
import com.ibm.common.activitystreams.LinkValue;
|
||||
import com.ibm.common.activitystreams.ValueType;
|
||||
import com.ibm.common.activitystreams.LinkValue.ArrayLinkValue;
|
||||
|
||||
/**
|
||||
* Utility abstract base used for objects that are JSON
|
||||
* dictionaries as opposed to full Activity Stream objects.
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public abstract class AbstractDictionaryObject<X>
|
||||
extends AbstractWritable
|
||||
implements Iterable<String> {
|
||||
|
||||
public static abstract class AbstractBuilder
|
||||
<X, D extends AbstractDictionaryObject<X>, B extends AbstractBuilder<X,D,B>>
|
||||
extends AbstractWritable.AbstractWritableBuilder<D,B> {
|
||||
|
||||
protected final Map<String, X> map =
|
||||
Maps.newHashMap();
|
||||
private boolean isempty = true;
|
||||
|
||||
/**
|
||||
* Returns true if items have been added
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean notEmpty() {
|
||||
return !isempty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an item in this dictionary object
|
||||
* @param key String
|
||||
* @param x X
|
||||
* @return B
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public B set(String key, X x) {
|
||||
if (x != null) {
|
||||
map.put(key,x);
|
||||
isempty = false;
|
||||
}
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an item in this dictionary object
|
||||
* @param key String
|
||||
* @param x Supplier<? extends X>
|
||||
* @return B
|
||||
*/
|
||||
public B set(
|
||||
String key,
|
||||
Supplier<? extends X> x) {
|
||||
return set(key, x.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a link
|
||||
* @param name String
|
||||
* @param url String
|
||||
* @return B
|
||||
**/
|
||||
protected B link(String name, String url) {
|
||||
return link(name, linkValue(url));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a link
|
||||
* @param name String
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
**/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected B link(String name, LinkValue link) {
|
||||
if (link == null)
|
||||
return (B)this;
|
||||
isempty = false;
|
||||
Object obj = map.get(name);
|
||||
if (link.valueType() != ValueType.ARRAY) {
|
||||
if (obj instanceof LinkValue)
|
||||
link =
|
||||
((LinkValue) obj).valueType() == ValueType.ARRAY ?
|
||||
linkValues()
|
||||
.add((LinkValue.ArrayLinkValue)obj)
|
||||
.add(link)
|
||||
.get() :
|
||||
linkValues()
|
||||
.add((LinkValue)obj, link)
|
||||
.get();
|
||||
map.put(name, (X) link);
|
||||
} else map.put(name, (X)link);
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a link
|
||||
* @param name String
|
||||
* @param link Supplier<? extends LinkValue>
|
||||
* @return B
|
||||
**/
|
||||
public B link(String name, Supplier<? extends LinkValue> link) {
|
||||
return link(name,link.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a link
|
||||
* @param name String
|
||||
* @param links Object[]
|
||||
* @return B
|
||||
**/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected B link(String name, Object... links) {
|
||||
if (links == null) return (B)this;
|
||||
ArrayLinkValue.Builder b =
|
||||
ArrayLinkValue.make();
|
||||
for (Object obj : links)
|
||||
_add(b, obj);
|
||||
return link(name,b.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method _add.
|
||||
* @param builder ArrayLinkValue.Builder
|
||||
* @param obj Object
|
||||
*/
|
||||
private void _add(ArrayLinkValue.Builder builder, Object obj) {
|
||||
if (obj == null)
|
||||
return;
|
||||
else if (obj instanceof String)
|
||||
builder.add((String)obj);
|
||||
else if (obj instanceof ASObject)
|
||||
builder.add((ASObject)obj);
|
||||
else if (obj instanceof Supplier)
|
||||
_add(builder,((Supplier<?>)obj).get());
|
||||
else throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final ImmutableMap<String,X> map;
|
||||
private transient int hash = 1;
|
||||
|
||||
/**
|
||||
* @param builder AbstractBuilder<X,?,?>
|
||||
*/
|
||||
protected AbstractDictionaryObject(AbstractBuilder<X,?,?> builder) {
|
||||
super(builder);
|
||||
this.map = ImmutableMap.copyOf(builder.map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getSingle.
|
||||
* @param key String
|
||||
* @return X
|
||||
*/
|
||||
protected X getSingle(String key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getSingle.
|
||||
* @param key String
|
||||
* @param defaultValue X
|
||||
* @return X
|
||||
*/
|
||||
protected X getSingle(String key, X defaultValue) {
|
||||
X ret = getSingle(key);
|
||||
return ret != null ? ret : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getIterable.
|
||||
* @param key String
|
||||
* @return Iterable<X>
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Iterable<X> getIterable(String key) {
|
||||
X x = map.get(key);
|
||||
return x instanceof Iterable ?
|
||||
(Iterable<X>)x :
|
||||
of(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getIterable.
|
||||
* @param key String
|
||||
* @param filter Predicate<X>
|
||||
* @return Iterable<X>
|
||||
*/
|
||||
protected Iterable<X> getIterable(
|
||||
String key,
|
||||
Predicate<X> filter) {
|
||||
return filter(getIterable(key), filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getIterable.
|
||||
* @param key String
|
||||
* @param transform Function<X,Y>
|
||||
* @return Iterable<Y>
|
||||
*/
|
||||
protected <Y>Iterable<Y> getIterable(
|
||||
String key,
|
||||
Function<X,Y> transform) {
|
||||
return transform(getIterable(key), transform);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getIterable.
|
||||
* @param key String
|
||||
* @param filter Predicate<X>
|
||||
* @param transform Function<X,Y>
|
||||
* @return Iterable<Y>
|
||||
*/
|
||||
protected <Y>Iterable<Y> getIterable(
|
||||
String key,
|
||||
Predicate<X> filter,
|
||||
Function<X,Y> transform) {
|
||||
return transform(filter(getIterable(key),filter),transform);
|
||||
}
|
||||
|
||||
public Iterator<String> iterator() {
|
||||
return map.keySet().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the verb appears in the ActionsValue object
|
||||
* @param key String
|
||||
* @return boolean */
|
||||
public boolean has(String key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the verb appears in the ActionsValue object
|
||||
* and there is at least one handler in the value.
|
||||
* @param key String
|
||||
* @return boolean
|
||||
*/
|
||||
protected boolean hasAtLeastOne(String key) {
|
||||
return has(key) && size(getIterable(key)) > 0;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
if (hash == 1)
|
||||
hash = map.hashCode();
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AbstractDictionaryObject other =
|
||||
(AbstractDictionaryObject) obj;
|
||||
return difference(map,other.map).areEqual();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.util;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Abstract base GSON Serializer for AbstractDictionaryObject
|
||||
* instances.
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public abstract class AbstractDictionaryObjectAdapter
|
||||
<Y,X extends AbstractDictionaryObject<Y>,
|
||||
B extends AbstractDictionaryObject.AbstractBuilder<Y, X, B>>
|
||||
extends Adapter<X> {
|
||||
|
||||
private final Class<Y> klass;
|
||||
|
||||
/**
|
||||
* Constructor for AbstractDictionaryObjectAdapter.
|
||||
* @param klass Class<Y>
|
||||
*/
|
||||
protected AbstractDictionaryObjectAdapter(Class<Y> klass) {
|
||||
this.klass = klass;
|
||||
}
|
||||
|
||||
public JsonElement serialize(
|
||||
X x,
|
||||
Type type,
|
||||
JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
for (String key : x)
|
||||
obj.add(
|
||||
key,
|
||||
context.serialize(
|
||||
x.getSingle(key),
|
||||
klass));
|
||||
return obj;
|
||||
}
|
||||
|
||||
public X deserialize(
|
||||
JsonElement element,
|
||||
Type type1,
|
||||
JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
checkArgument(element.isJsonObject());
|
||||
try {
|
||||
JsonObject obj =
|
||||
element.getAsJsonObject();
|
||||
B builder = builder();
|
||||
for (Entry<String,JsonElement> entry : obj.entrySet())
|
||||
builder.set(
|
||||
entry.getKey(),
|
||||
context.<Y>deserialize(
|
||||
entry.getValue(),
|
||||
klass));
|
||||
return builder.get();
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract B builder();
|
||||
}
|
||||
|
|
@ -1,153 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.util;
|
||||
|
||||
import static com.ibm.common.activitystreams.IO.makeDefault;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.ibm.common.activitystreams.IO;
|
||||
import com.ibm.common.activitystreams.Writable;
|
||||
|
||||
/**
|
||||
* Utility base class for Writable instances.
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public abstract class AbstractWritable
|
||||
implements Writable {
|
||||
|
||||
public static abstract class AbstractWritableBuilder
|
||||
<X extends Writable, B extends AbstractWritableBuilder<X,B>>
|
||||
implements Supplier<X> {
|
||||
|
||||
private IO io = null;
|
||||
|
||||
/**
|
||||
* Set the IO instance that should be used
|
||||
* by default for writing instances of this
|
||||
* object.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public B writeUsing(IO io) {
|
||||
this.io = io;
|
||||
return (B)this;
|
||||
}
|
||||
|
||||
public void writeTo(OutputStream out) {
|
||||
get().writeTo(out);
|
||||
}
|
||||
|
||||
public Future<?> writeTo(OutputStream out, ExecutorService executor) {
|
||||
return get().writeTo(out,executor);
|
||||
}
|
||||
|
||||
public void writeTo(Writer out) {
|
||||
get().writeTo(out);
|
||||
}
|
||||
|
||||
public Future<?> writeTo(Writer out, ExecutorService executor) {
|
||||
return get().writeTo(out,executor);
|
||||
}
|
||||
|
||||
|
||||
public void writeTo(OutputStream out, IO io) {
|
||||
get().writeTo(out,io);
|
||||
}
|
||||
|
||||
public Future<?> writeTo(OutputStream out, IO io, ExecutorService executor) {
|
||||
return get().writeTo(out, io, executor);
|
||||
}
|
||||
|
||||
public void writeTo(Writer out, IO io) {
|
||||
get().writeTo(out,io);
|
||||
}
|
||||
|
||||
public Future<?> writeTo(Writer out, IO io, ExecutorService executor) {
|
||||
return get().writeTo(out, io, executor);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final IO io;
|
||||
|
||||
protected AbstractWritable(AbstractWritableBuilder<?,?> b) {
|
||||
this.io = b.io != null ? b.io : makeDefault();
|
||||
}
|
||||
|
||||
public void writeTo(OutputStream out) {
|
||||
writeTo(out,io);
|
||||
}
|
||||
|
||||
public Future<?> writeTo(OutputStream out, ExecutorService executor) {
|
||||
return writeTo(out, io, executor);
|
||||
}
|
||||
|
||||
public void writeTo(OutputStream out, IO io) {
|
||||
io.write(this,out);
|
||||
}
|
||||
|
||||
public Future<?> writeTo(OutputStream out, IO io, ExecutorService executor) {
|
||||
return io.write(this,out,executor);
|
||||
}
|
||||
|
||||
public void writeTo(Writer out) {
|
||||
writeTo(out, io);
|
||||
}
|
||||
|
||||
public Future<?> writeTo(Writer out, ExecutorService executor) {
|
||||
return writeTo(out, io, executor);
|
||||
}
|
||||
|
||||
public void writeTo(Writer out, IO io) {
|
||||
io.write(this,out);
|
||||
}
|
||||
|
||||
public Future<?> writeTo(Writer out, IO io, ExecutorService executor) {
|
||||
return io.write(this, out, executor);
|
||||
}
|
||||
|
||||
public String toString(IO io) {
|
||||
StringWriter sw =
|
||||
new StringWriter();
|
||||
io.write(this, sw);
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
public Future<String> toString(ExecutorService executor) {
|
||||
return io.write(this, executor);
|
||||
}
|
||||
|
||||
public Future<String> toString(IO io, ExecutorService executor) {
|
||||
return io.write(this, executor);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return toString(io);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,362 +0,0 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.util;
|
||||
|
||||
import static com.google.common.base.Enums.getIfPresent;
|
||||
import static com.google.common.base.Optional.absent;
|
||||
import static com.google.common.base.Optional.fromNullable;
|
||||
import static com.google.common.base.Optional.of;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.joda.time.Duration.millis;
|
||||
import static org.joda.time.Duration.standardDays;
|
||||
import static org.joda.time.Duration.standardHours;
|
||||
import static org.joda.time.Duration.standardMinutes;
|
||||
import static org.joda.time.Duration.standardSeconds;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Duration;
|
||||
import org.joda.time.Interval;
|
||||
import org.joda.time.Period;
|
||||
|
||||
import com.google.common.base.Converter;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.primitives.Doubles;
|
||||
import com.google.common.primitives.Floats;
|
||||
import com.google.common.primitives.Ints;
|
||||
import com.google.common.primitives.Longs;
|
||||
|
||||
public final class Converters {
|
||||
|
||||
private Converters() {}
|
||||
|
||||
/**
|
||||
* Method toDuration.
|
||||
* @param v long
|
||||
* @param unit TimeUnit
|
||||
* @return Duration
|
||||
*/
|
||||
public static Duration toDuration(long v, TimeUnit unit) {
|
||||
switch(unit) {
|
||||
case NANOSECONDS:
|
||||
return millis(v / 1000 / 1000);
|
||||
case MICROSECONDS:
|
||||
return millis(v / 1000);
|
||||
case MILLISECONDS:
|
||||
return millis(v);
|
||||
case SECONDS:
|
||||
return standardSeconds(v);
|
||||
case MINUTES:
|
||||
return standardMinutes(v);
|
||||
case HOURS:
|
||||
return standardHours(v);
|
||||
case DAYS:
|
||||
return standardDays(v);
|
||||
default:
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method tryParseShort.
|
||||
* @param input String
|
||||
* @return Short
|
||||
*/
|
||||
private static Short tryParseShort(String input) {
|
||||
try {
|
||||
return Short.parseShort(input);
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method tryParseBoolean.
|
||||
* @param input String
|
||||
* @return Boolean
|
||||
*/
|
||||
private static Boolean tryParseBoolean(String input) {
|
||||
try {
|
||||
return Boolean.parseBoolean(input);
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method tryParseDateTime.
|
||||
* @param input String
|
||||
* @return DateTime
|
||||
*/
|
||||
private static DateTime tryParseDateTime(String input) {
|
||||
try {
|
||||
return DateTime.parse(input);
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method tryParseDuration.
|
||||
* @param input String
|
||||
* @return Duration
|
||||
*/
|
||||
private static Duration tryParseDuration(String input) {
|
||||
try {
|
||||
return Period.parse(input).toDurationFrom(DateTime.now());
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method tryParsePeriod.
|
||||
* @param input String
|
||||
* @return Period
|
||||
*/
|
||||
private static Period tryParsePeriod(String input) {
|
||||
try {
|
||||
return Period.parse(input);
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method tryParseInterval.
|
||||
* @param input String
|
||||
* @return Interval
|
||||
*/
|
||||
private static Interval tryParseInterval(String input) {
|
||||
try {
|
||||
return Interval.parse(input);
|
||||
} catch (Throwable t) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static final Function<Object,Optional<Interval>> toInterval =
|
||||
new Function<Object,Optional<Interval>>() {
|
||||
public Optional<Interval> apply(Object input) {
|
||||
Optional<Interval> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Interval ?
|
||||
of((Interval)input) :
|
||||
fromNullable(tryParseInterval(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<Duration>> toDuration =
|
||||
new Function<Object,Optional<Duration>>() {
|
||||
|
||||
public Optional<Duration> apply(Object input) {
|
||||
Optional<Duration> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Duration ?
|
||||
of((Duration)input) :
|
||||
input instanceof Number ?
|
||||
of(standardSeconds(((Number)input).longValue())) :
|
||||
fromNullable(tryParseDuration(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<Period>> toPeriod =
|
||||
new Function<Object,Optional<Period>>() {
|
||||
public Optional<Period> apply(Object input) {
|
||||
Optional<Period> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Period ?
|
||||
of((Period)input) :
|
||||
fromNullable(tryParsePeriod(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<DateTime>> toDateTime =
|
||||
new Function<Object,Optional<DateTime>>() {
|
||||
public Optional<DateTime> apply(Object input) {
|
||||
Optional<DateTime> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof DateTime ?
|
||||
of((DateTime)input) :
|
||||
input instanceof Date ?
|
||||
of(new DateTime((Date)input)) :
|
||||
input instanceof Number ?
|
||||
of(new DateTime(((Number)input).longValue())) :
|
||||
fromNullable(tryParseDateTime(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<Boolean>> toBoolean =
|
||||
new Function<Object,Optional<Boolean>>() {
|
||||
public Optional<Boolean> apply(Object input) {
|
||||
Optional<Boolean> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Boolean ?
|
||||
of((Boolean)input) :
|
||||
input instanceof Number ?
|
||||
of(((Number)input).intValue() != 0) :
|
||||
fromNullable(tryParseBoolean(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<Short>> toShort =
|
||||
new Function<Object,Optional<Short>>() {
|
||||
public Optional<Short> apply(Object input) {
|
||||
Optional<Short> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Number ?
|
||||
of(((Number) input).shortValue()) :
|
||||
fromNullable(tryParseShort(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<Integer>> toInt =
|
||||
new Function<Object,Optional<Integer>>() {
|
||||
public Optional<Integer> apply(Object input) {
|
||||
Optional<Integer> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Number ?
|
||||
of(((Number) input).intValue()) :
|
||||
fromNullable(Ints.tryParse(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<Long>> toLong =
|
||||
new Function<Object,Optional<Long>>() {
|
||||
public Optional<Long> apply(Object input) {
|
||||
Optional<Long> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Number ?
|
||||
of(((Number) input).longValue()) :
|
||||
fromNullable(Longs.tryParse(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<Double>> toDouble =
|
||||
new Function<Object,Optional<Double>>() {
|
||||
public Optional<Double> apply(Object input) {
|
||||
Optional<Double> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Number ?
|
||||
of(((Number) input).doubleValue()) :
|
||||
fromNullable(Doubles.tryParse(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
public static final Function<Object,Optional<Float>> toFloat =
|
||||
new Function<Object,Optional<Float>>() {
|
||||
public Optional<Float> apply(Object input) {
|
||||
Optional<Float> ret = absent();
|
||||
if (input != null)
|
||||
ret = input instanceof Number ?
|
||||
of(((Number) input).floatValue()) :
|
||||
fromNullable(Floats.tryParse(input.toString()));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Method stringConverter.
|
||||
* @param enumClass Class<E>
|
||||
* @param or E
|
||||
* @return Converter<String,E>
|
||||
*/
|
||||
public static <E extends Enum<E>> Converter<String,E> stringConverter(
|
||||
final Class<E> enumClass,
|
||||
final E or) {
|
||||
return new Converter<String,E>() {
|
||||
|
||||
@Override
|
||||
protected String doBackward(E e) {
|
||||
return checkNotNull(e).name();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected E doForward(String s) {
|
||||
return getIfPresent(enumClass, s).or(or);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toUpperConverter.
|
||||
* @return Converter<String,String>
|
||||
*/
|
||||
public static Converter<String,String> toUpperConverter() {
|
||||
return toLowerConverter().reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toUpperConverter.
|
||||
* @param locale Locale
|
||||
* @return Converter<String,String>
|
||||
*/
|
||||
public static Converter<String,String> toUpperConverter(Locale locale) {
|
||||
return toLowerConverter(locale).reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toLowerConverter.
|
||||
* @return Converter<String,String>
|
||||
*/
|
||||
public static Converter<String,String> toLowerConverter() {
|
||||
return toLowerConverter(Locale.getDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toLowerConverter.
|
||||
* @param locale Locale
|
||||
* @return Converter<String,String>
|
||||
*/
|
||||
public static Converter<String,String> toLowerConverter(final Locale locale) {
|
||||
return new Converter<String,String>() {
|
||||
|
||||
@Override
|
||||
protected String doForward(String a) {
|
||||
return a.toLowerCase(locale);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doBackward(String b) {
|
||||
return b.toUpperCase(locale);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
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());
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,234 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import com.google.common.base.Function
|
||||
import com.google.common.base.Predicate
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.ibm.common.activitystreams.LinkValue.SimpleLinkValue
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObject
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
*
|
||||
* The value of the "actions" property... used to map potential verbs
|
||||
* with objects capable of "handling" those verbs. Each of the keys
|
||||
* must be a valid verb (either a token or an absolute IRI).
|
||||
*
|
||||
*
|
||||
* Specifying Actions while creating AS Objects:
|
||||
*
|
||||
* <pre>
|
||||
* import static com.ibm.common.activitystreams.Makers.object;
|
||||
* import static com.ibm.common.activitystreams.Makers.actions;
|
||||
* import static com.ibm.common.activitystreams.actions.ActionMakers.intentAction;
|
||||
* ...
|
||||
*
|
||||
* ASObject obj = object()
|
||||
* .action("like", "http://example.org/like-action")
|
||||
* .action("follow", intentAction())
|
||||
* .get();
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
* Accessing Actions when consuming AS Objects:
|
||||
*
|
||||
* <pre>
|
||||
* ActionsValue actions = obj.actions();
|
||||
* for (LinkValue lv : actions.get("like")) {
|
||||
* ...
|
||||
* }
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
* The Actions object itself is a JSON Dictonary that maps
|
||||
* verb identifiers to a Link Value that contains one or more
|
||||
* Action Handlers. A call to the get(...) method returns an
|
||||
* Iterable of Simple or Object LinkValue instances representing
|
||||
* each of the associated Action Handlers.
|
||||
*
|
||||
*
|
||||
* A Serialized Activity Stream object with Actions:
|
||||
* <pre>
|
||||
* {
|
||||
* "objectType": "note",
|
||||
* "displayName": "My Note",
|
||||
* "actions": {
|
||||
* "like": "http://example.org/like-action",
|
||||
* "follow": {
|
||||
* "objectType": "IntentActionHandler",
|
||||
* "displayName": "Follow updates to this note!"
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
</pre> *
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class ActionsValue
|
||||
private constructor(builder: Builder) : AbstractDictionaryObject<LinkValue>(builder),
|
||||
Serializable {
|
||||
/**
|
||||
* Builder for ActionsValue objects.
|
||||
* <pre>
|
||||
* Makers.actions()
|
||||
* .set("like", "http://example.org/like-action")
|
||||
* .set("follow", ActionMakers.intentAction())
|
||||
* .get();
|
||||
</pre> *
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<LinkValue, ActionsValue, Builder>() {
|
||||
/**
|
||||
* Add an action handler for the given verb.
|
||||
* Calling this multiple times results in
|
||||
* multiple action handlers.
|
||||
* @param verb String
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
*/
|
||||
operator fun set(verb: String, iri: String?): Builder {
|
||||
return set(
|
||||
verb,
|
||||
SimpleLinkValue.Companion.make(iri)
|
||||
)
|
||||
}
|
||||
|
||||
override operator fun set(key: String, x: LinkValue?): Builder {
|
||||
return super.link(key, x)!!
|
||||
}
|
||||
|
||||
override fun set(key: String, x: Supplier<out LinkValue>): Builder {
|
||||
return super.link(key, x)!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the built ActionsValue instance
|
||||
* @return ParametersValue
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): ActionsValue {
|
||||
return ActionsValue(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Action Handlers associated with the given verb.
|
||||
* @param verb String
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun get(verb: String?): Iterable<LinkValue?> {
|
||||
return super.getIterable(verb)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Action Handlers associated with the given verb
|
||||
* that satisfy the given filter
|
||||
* @param verb String
|
||||
* @param filter Predicate<LinkValue>
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
</LinkValue></LinkValue> */
|
||||
fun get(
|
||||
verb: String?,
|
||||
filter: Predicate<LinkValue?>
|
||||
): Iterable<LinkValue?> {
|
||||
return super.getIterable(verb, filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Action Handlers associated with the given verb,
|
||||
* transformed using the given Transform function
|
||||
* @param verb String
|
||||
* @param transform Function<LinkValue></LinkValue>,Y>
|
||||
* @return java.util.Iterable<Y>
|
||||
</Y> */
|
||||
fun <Y> get(
|
||||
verb: String?,
|
||||
transform: Function<LinkValue?, Y>
|
||||
): Iterable<Y> {
|
||||
return super.getIterable(verb, transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Action Handlers associated with the given verb,
|
||||
* filtered using the given Predicate and transformed
|
||||
* using the given Transform function.
|
||||
* @param verb String
|
||||
* @param filter Predicate<LinkValue>
|
||||
* @param transform Function<LinkValue></LinkValue>,Y>
|
||||
* @return java.util.Iterable<Y>
|
||||
</Y></LinkValue> */
|
||||
fun <Y> get(
|
||||
verb: String?,
|
||||
filter: Predicate<LinkValue?>,
|
||||
transform: Function<LinkValue?, Y>
|
||||
): Iterable<Y> {
|
||||
return super.getIterable(verb, filter, transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* True if there is at least one Action Handler associated
|
||||
* with the given verb
|
||||
* @param verb String
|
||||
* @return boolean
|
||||
*/
|
||||
public override fun hasAtLeastOne(verb: String?): Boolean {
|
||||
return super.hasAtLeastOne(verb)
|
||||
}
|
||||
|
||||
// Java serialization support
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: ActionsValue) : Serializable {
|
||||
private val map: ImmutableMap<String, Any>
|
||||
|
||||
init {
|
||||
val builder =
|
||||
ImmutableMap.builder<String, Any>()
|
||||
for (key in obj) builder.put(key, obj.get(key))
|
||||
this.map = builder.build()
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
val builder =
|
||||
Makers.actions()
|
||||
for ((key, value) in map) {
|
||||
val list = value as Iterable<LinkValue>
|
||||
for (link in list) builder[key] = link
|
||||
}
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,820 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.common.base.Predicate
|
||||
import com.google.common.base.Supplier
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
import java.util.*
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
|
||||
/**
|
||||
* An Activity Streams 2.0 Activity
|
||||
*
|
||||
*
|
||||
* Creating an Activity:
|
||||
* <pre>
|
||||
* import static com.ibm.common.activitystreams.Makers.activity;
|
||||
* import static com.ibm.common.activitystreams.Makers.object;
|
||||
* ...
|
||||
*
|
||||
* Activity activity = activity()
|
||||
* .actor("acct:joe@example.org")
|
||||
* .verb("post")
|
||||
* .object(object().content("This is a note"))
|
||||
* .get();
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
* Consuming an Activity:
|
||||
* <pre>
|
||||
* Activity activity = IO.makeDefault().readAsActivity(...);
|
||||
*
|
||||
* System.out.println(activity.verbAsString());
|
||||
* ASObject actor = activity.firstActor();
|
||||
* ASObject object = activity.firstObject();
|
||||
</pre> *
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Activity
|
||||
internal constructor(builder: AbstractBuilder<*, *>) : ASObject(builder), Serializable {
|
||||
enum class Audience {
|
||||
/**
|
||||
* Primary public audience
|
||||
*/
|
||||
TO,
|
||||
|
||||
/**
|
||||
* Primary private audience
|
||||
*/
|
||||
BTO,
|
||||
|
||||
/**
|
||||
* Secondary public audience
|
||||
*/
|
||||
CC,
|
||||
|
||||
/**
|
||||
* Secondary private audience
|
||||
*/
|
||||
BCC,
|
||||
|
||||
/**
|
||||
* Public origin
|
||||
*/
|
||||
FROM,
|
||||
|
||||
/**
|
||||
* Private origin
|
||||
*/
|
||||
BFROM;
|
||||
|
||||
val label: String = name.lowercase(Locale.getDefault())
|
||||
|
||||
/**
|
||||
* Returns the "from" target(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun get(
|
||||
activity: Activity
|
||||
): Iterable<LinkValue?>? {
|
||||
return activity.links(label)
|
||||
}
|
||||
|
||||
fun get(
|
||||
activity: Activity,
|
||||
filter: Predicate<in LinkValue?>
|
||||
): Iterable<LinkValue?> {
|
||||
return activity.links(label, filter)
|
||||
}
|
||||
|
||||
fun first(activity: Activity): LinkValue? {
|
||||
return activity.firstLink(label)
|
||||
}
|
||||
|
||||
fun firstMatching(
|
||||
activity: Activity,
|
||||
test: Predicate<in LinkValue?>
|
||||
): LinkValue? {
|
||||
return activity.firstMatchingLink(label, test)
|
||||
}
|
||||
}
|
||||
|
||||
enum class Status {
|
||||
/** The Activity is tentatively scheduled to begin */
|
||||
TENTATIVE,
|
||||
|
||||
/** The Activity is scheduled to begin */
|
||||
PENDING,
|
||||
|
||||
/** The Activity has been completed */
|
||||
COMPLETED,
|
||||
|
||||
/** The Activity has been canceled or aborted */
|
||||
CANCELED,
|
||||
|
||||
/** The Activity is currently active */
|
||||
ACTIVE,
|
||||
|
||||
/** The Activity has been voided. */
|
||||
VOIDED,
|
||||
OTHER
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Builder for concrete Activity object instances.
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<Activity?, Builder?>() {
|
||||
/**
|
||||
* Get the built Activity object
|
||||
* @return Activity
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): Activity {
|
||||
return Activity(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract builder for Activity classes. This is defined this way
|
||||
* to make it easier to build extensions of the Activity class.
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
abstract class AbstractBuilder
|
||||
<A : Activity?, B : AbstractBuilder<A, B>?>
|
||||
: ASObject.AbstractBuilder<A, B>() {
|
||||
/**
|
||||
* Set the Activity status property.
|
||||
* @param status Status
|
||||
* @return B
|
||||
*/
|
||||
fun status(status: Status?): B {
|
||||
if (status != null) set("status", status)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "voided"
|
||||
* @return B
|
||||
*/
|
||||
fun voided(): B {
|
||||
return status(Status.VOIDED)
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "tentative"
|
||||
* @return B
|
||||
*/
|
||||
fun tentative(): B {
|
||||
return status(Status.TENTATIVE)
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "pending"
|
||||
* @return B
|
||||
*/
|
||||
fun pending(): B {
|
||||
return status(Status.PENDING)
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "active"
|
||||
* @return B
|
||||
*/
|
||||
fun active(): B {
|
||||
return status(Status.ACTIVE)
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "canceled"
|
||||
* @return B
|
||||
*/
|
||||
fun canceled(): B {
|
||||
return status(Status.CANCELED)
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the status of this activity as being "completed"
|
||||
* @return B
|
||||
*/
|
||||
fun completed(): B {
|
||||
return status(Status.COMPLETED)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the verb for this activity
|
||||
* @param iri String
|
||||
* @return B
|
||||
*/
|
||||
fun verb(iri: String?): B {
|
||||
set("verb", Makers.type(iri))
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the verb for this activity
|
||||
* @param tv TypeValue
|
||||
* @return B
|
||||
*/
|
||||
fun verb(tv: TypeValue?): B {
|
||||
set("verb", tv)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the verb for this activity
|
||||
* @param tv Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun verb(tv: Supplier<out TypeValue?>): B {
|
||||
return verb(tv.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a participant that indirectly contributed to the activity.
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
fun participant(url: String?, vararg urls: String?): B {
|
||||
if (url != null) link("participant", Makers.linkValue(url))
|
||||
if (urls != null) for (u in urls) link("participant", Makers.linkValue(u))
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a participant that indirectly contributed to the activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun participant(link: LinkValue?, vararg links: LinkValue?): B {
|
||||
if (link != null) link("participant", link)
|
||||
if (links != null) for (l in links) link("participant", l)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a participant that indirectly contributed to the activity
|
||||
* @param link Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun participant(link: Supplier<out LinkValue?>): B? {
|
||||
return link("participant", link.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instrument used to complete the activity
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
fun instrument(url: String?, vararg urls: String?): B {
|
||||
if (url != null) link("instrument", Makers.linkValue(url))
|
||||
if (urls != null) for (u in urls) link("instument", Makers.linkValue(u))
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instrument used to complete the activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun instrument(link: LinkValue?, vararg links: LinkValue?): B {
|
||||
if (link != null) link("instrument", link)
|
||||
if (links != null) for (l in links) link("instrument", l)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instrument used to complete the activity
|
||||
* @param link Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun instrument(link: Supplier<out LinkValue?>): B? {
|
||||
return link("instrument", link.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the primary actor/agent for this activity
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
fun actor(url: String?, vararg urls: String?): B {
|
||||
if (url != null) link("actor", Makers.linkValue(url))
|
||||
if (urls != null) for (u in urls) link("actor", Makers.linkValue(u))
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the primary actor/agent for this activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun actor(link: LinkValue?, vararg links: LinkValue?): B {
|
||||
if (link != null) link("actor", link)
|
||||
if (links != null) for (l in links) link("actor", l)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the actor
|
||||
* @param link Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun actor(link: Supplier<out LinkValue?>): B? {
|
||||
return link("actor", link.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the direct object of this activity (the object that has been
|
||||
* or will be acted upon)
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
fun `object`(url: String?, vararg urls: String?): B {
|
||||
if (url != null) link("object", Makers.linkValue(url))
|
||||
if (urls != null) for (u in urls) link("object", Makers.linkValue(u))
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the direct object of this activity (the object that has been
|
||||
* or will be acted upon)
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun `object`(link: LinkValue?, vararg links: LinkValue?): B {
|
||||
if (link != null) link("object", link)
|
||||
if (links != null) for (l in links) link("object", l)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the direct object of this activity (the object that has been
|
||||
* or will be acted upon)
|
||||
* @param link Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun `object`(link: Supplier<out LinkValue?>): B? {
|
||||
return link("object", link.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that is indirectly affected by the activity
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
fun target(url: String?, vararg urls: String?): B {
|
||||
if (url != null) link("target", Makers.linkValue(url))
|
||||
if (urls != null) for (u in urls) link("target", Makers.linkValue(u))
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that is indirectly affected by the activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun target(link: LinkValue?, vararg links: LinkValue?): B {
|
||||
if (link != null) link("target", link)
|
||||
if (links != null) for (l in links) link("target", l)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that is indirectly affected by the activity
|
||||
* @param link Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun target(link: Supplier<out LinkValue?>): B? {
|
||||
return link("target", link.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a URI that describes the result of this activity
|
||||
* @param url String
|
||||
* @return B
|
||||
*/
|
||||
fun result(url: String?, vararg urls: String?): B {
|
||||
if (url != null) link("result", Makers.linkValue(url))
|
||||
if (urls != null) for (u in urls) link("result", Makers.linkValue(u))
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that describes the result of this activity
|
||||
* @param link LinkValue
|
||||
* @return B
|
||||
*/
|
||||
fun result(link: LinkValue?, vararg links: LinkValue?): B {
|
||||
if (link != null) link("result", link)
|
||||
if (links != null) for (l in links) link("result", l)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an object that describes the result of this activity
|
||||
* @param link Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun result(link: Supplier<out LinkValue?>): B? {
|
||||
return link("result", link.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the audience for this activity
|
||||
* @param audience
|
||||
* @param url
|
||||
* @param urls
|
||||
* @return B
|
||||
*/
|
||||
fun audience(audience: Audience, url: String?, vararg urls: String?): B {
|
||||
link(audience.label, Makers.linkValue(url))
|
||||
for (u in urls) link(audience.label, u)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the audience for this activity
|
||||
* @param audience
|
||||
* @param link
|
||||
* @param links
|
||||
* @return B
|
||||
*/
|
||||
fun audience(audience: Audience, link: LinkValue?, vararg links: LinkValue?): B {
|
||||
link(audience.label, link)
|
||||
for (lv in links) link(audience.label, lv)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the audience for this activity
|
||||
* @param audience
|
||||
* @param link
|
||||
* @return B
|
||||
*/
|
||||
fun audience(audience: Audience, link: Supplier<out LinkValue?>): B? {
|
||||
return link(audience.label, link)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the priority as value in the range 0.00 to 1.00.
|
||||
* Value will be checked to ensure it is within the valid range.
|
||||
* The value SHOULD be limited to two decimal places but the
|
||||
* number of decimals will not be checked.
|
||||
* @param d double
|
||||
* @return B
|
||||
* @throws IllegalArgumentException if the value is not within 0.00-1.00
|
||||
*/
|
||||
fun priority(d: Double): B? {
|
||||
return set("priority", checkPriority(d))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the verb as TypeValue object
|
||||
* @return T
|
||||
*/
|
||||
fun <T : TypeValue?> verb(): T? {
|
||||
return this.get("verb")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the verb as a string
|
||||
* @return String
|
||||
*/
|
||||
fun verbString(): String? {
|
||||
return typeValueAsString("verb")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the participant(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun participant(): Iterable<LinkValue?>? {
|
||||
return links("participant")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching participant(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun participant(filter: Predicate<in LinkValue?>?): Iterable<LinkValue?> {
|
||||
return links("participant", filter!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first paticipant
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstParticipant(): LinkValue? {
|
||||
return firstLink("participant")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching participant
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstMatchingParticipant(test: Predicate<in LinkValue?>?): LinkValue? {
|
||||
return firstMatchingLink("participan", test!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the instrument(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun instrument(): Iterable<LinkValue?>? {
|
||||
return links("instrument")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching instrument(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun instrument(filter: Predicate<in LinkValue?>?): Iterable<LinkValue?> {
|
||||
return links("instrument", filter!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first instrument
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstInstrument(): LinkValue? {
|
||||
return firstLink("instrument")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching instrument
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstMatchingInstrument(test: Predicate<in LinkValue?>?): LinkValue? {
|
||||
return firstMatchingLink("instrument", test!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the actor(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun actor(): Iterable<LinkValue?>? {
|
||||
return links("actor")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching actor(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun actor(filter: Predicate<in LinkValue?>?): Iterable<LinkValue?> {
|
||||
return links("actor", filter!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first actor
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstActor(): LinkValue? {
|
||||
return firstLink("actor")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching actor
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstMatchingActor(test: Predicate<in LinkValue?>?): LinkValue? {
|
||||
return firstMatchingLink("actor", test!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun `object`(): Iterable<LinkValue?>? {
|
||||
return links("object")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching object(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun `object`(filter: Predicate<in LinkValue?>?): Iterable<LinkValue?> {
|
||||
return links("object", filter!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first object
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstObject(): LinkValue? {
|
||||
return firstLink("object")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching object
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstMatchingObject(test: Predicate<in LinkValue?>?): LinkValue? {
|
||||
return firstMatchingLink("object", test!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the target(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun target(): Iterable<LinkValue?>? {
|
||||
return links("target")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the matching target(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
</LinkValue> */
|
||||
fun target(filter: Predicate<in LinkValue?>?): Iterable<LinkValue?> {
|
||||
return links("target", filter!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first target
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstTarget(): LinkValue? {
|
||||
return firstLink("target")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching target
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstMatchingTarget(test: Predicate<in LinkValue?>?): LinkValue? {
|
||||
return firstMatchingLink("target", test!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result(s)
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun result(): Iterable<LinkValue?>? {
|
||||
return links("result")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the matching result(s)
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun result(filter: Predicate<in LinkValue?>?): Iterable<LinkValue?> {
|
||||
return links("result", filter!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first result
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstResult(): LinkValue? {
|
||||
return firstLink("result")
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching result
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstMatchingResult(test: Predicate<in LinkValue?>?): LinkValue? {
|
||||
return firstMatchingLink("result", test!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the priority.
|
||||
* @return double
|
||||
* @throws IllegalArgumentException if the priority is not within
|
||||
* the range 0.00-1.00
|
||||
*/
|
||||
fun priority(): Double {
|
||||
return checkPriority(getDouble("priority"))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the audience for this activity
|
||||
* @param audience
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun audience(
|
||||
audience: Audience
|
||||
): Iterable<LinkValue?>? {
|
||||
return audience.get(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the audience for this activity
|
||||
* @param audience
|
||||
* @param filter
|
||||
* @return java.util.Iterable<LinkValue>
|
||||
*/
|
||||
fun audience(
|
||||
audience: Audience,
|
||||
filter: Predicate<in LinkValue?>
|
||||
): Iterable<LinkValue?> {
|
||||
return audience.get(this, filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first audience for this activity
|
||||
* @param audience
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstAudience(audience: Audience): LinkValue? {
|
||||
return audience.first(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first matching audience for this activity
|
||||
* @param audience
|
||||
* @param test
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstMatchingAudience(
|
||||
audience: Audience,
|
||||
test: Predicate<in LinkValue?>
|
||||
): LinkValue? {
|
||||
return audience.firstMatching(this, test)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the status of this activity
|
||||
* @return Status
|
||||
*/
|
||||
fun status(): Status? {
|
||||
return get("status")
|
||||
}
|
||||
|
||||
// Java Serialization Support
|
||||
@Throws(ObjectStreamException::class)
|
||||
override fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: Activity) : AbstractSerializedForm<Activity?>(obj) {
|
||||
override fun builder(): Builder {
|
||||
return Makers.activity()
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any? {
|
||||
return super.doReadResolve()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Ensures that the priority value is within the acceptable range (0.0-1.0)
|
||||
* @param d double
|
||||
* @return double
|
||||
*/
|
||||
private fun checkPriority(d: Double): Double {
|
||||
Preconditions.checkArgument(floor(d) >= 0 && ceil(d) >= 1)
|
||||
return d
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,529 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.common.base.Predicate
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import com.google.common.collect.Iterables
|
||||
import com.ibm.common.activitystreams.LinkValue.ArrayLinkValue
|
||||
import org.joda.time.DateTime
|
||||
import org.joda.time.ReadableDuration
|
||||
import org.joda.time.ReadablePeriod
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
*
|
||||
* An Activity Streams collection object
|
||||
*
|
||||
* <pre>
|
||||
* import static com.ibm.common.activitystreams.Makers.collection;
|
||||
* import static com.ibm.common.activitystreams.Makers.object;
|
||||
* ...
|
||||
*
|
||||
* Collection collection = collection()
|
||||
* .items(object()...)
|
||||
* .items(object()...)
|
||||
* .get();
|
||||
</pre> *
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Collection
|
||||
|
||||
/**
|
||||
* Constructor for Collection.
|
||||
* @param builder Collection.AbstractBuilder
|
||||
*/
|
||||
internal constructor(builder: AbstractBuilder<*, *>) : ASObject(builder), Serializable {
|
||||
/** Standardized Paging Links */
|
||||
enum class Page {
|
||||
/**
|
||||
* Reference to the first page in the logical set
|
||||
*/
|
||||
FIRST,
|
||||
|
||||
/**
|
||||
* Reference to the next page in the logical set
|
||||
*/
|
||||
NEXT,
|
||||
|
||||
/**
|
||||
* Reference to the final page in the logical set
|
||||
*/
|
||||
LAST,
|
||||
|
||||
/**
|
||||
* Reference to the previous page in the logical set
|
||||
*/
|
||||
PREVIOUS,
|
||||
|
||||
/**
|
||||
* Reference to the previous page in the logical set (treated as an alias for "previous")
|
||||
*/
|
||||
@Deprecated("Use Page.PREVIOUS instead")
|
||||
PREV,
|
||||
|
||||
/**
|
||||
* Reference to the page containing the most recently published/updated
|
||||
* items in the logical set
|
||||
*/
|
||||
CURRENT,
|
||||
|
||||
/**
|
||||
* Reference to this page
|
||||
*/
|
||||
SELF;
|
||||
|
||||
val label: String = name.lowercase(Locale.getDefault())
|
||||
|
||||
private fun checkPrev(col: Collection, or: String): String {
|
||||
if (this == PREVIOUS) {
|
||||
if (!col.has(label) && col.has(label)) return label
|
||||
} else if (this == PREV) {
|
||||
if (!col.has(label) && col.has(label)) return label
|
||||
}
|
||||
return or
|
||||
}
|
||||
|
||||
fun links(col: Collection): Iterable<LinkValue?>? {
|
||||
return col.links(checkPrev(col, label))
|
||||
}
|
||||
|
||||
fun links(
|
||||
col: Collection,
|
||||
filter: Predicate<in LinkValue?>
|
||||
): Iterable<LinkValue?> {
|
||||
return col.links(checkPrev(col, label), filter)
|
||||
}
|
||||
|
||||
fun first(col: Collection): LinkValue? {
|
||||
return col.firstLink(checkPrev(col, label))
|
||||
}
|
||||
|
||||
fun firstMatching(
|
||||
col: Collection,
|
||||
filter: Predicate<in LinkValue?>
|
||||
): LinkValue? {
|
||||
return col.firstMatchingLink(checkPrev(col, label), filter)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: AbstractBuilder<Collection, Builder?>() {
|
||||
/**
|
||||
* Method create.
|
||||
* @return Collection
|
||||
*/
|
||||
override fun create(): Collection {
|
||||
return Collection(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
abstract class AbstractBuilder
|
||||
<A : Collection?, B : AbstractBuilder<A, B>?>
|
||||
: ASObject.AbstractBuilder<A, B?>() {
|
||||
protected val list: ImmutableList.Builder<ASObject> = ImmutableList.builder()
|
||||
|
||||
/**
|
||||
* Method create.
|
||||
* @return A
|
||||
*/
|
||||
protected abstract fun create(): A
|
||||
|
||||
override fun set(key: String, value: Any?): B? {
|
||||
if (key == "items") {
|
||||
if (value is ArrayLinkValue) {
|
||||
for (lv in value) {
|
||||
list.add(lv as ASObject)
|
||||
}
|
||||
} else if (value is ASObject) list.add(value)
|
||||
return this as B
|
||||
} else return super.set(key, value)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return A
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): A {
|
||||
super.set("items", list.build())
|
||||
return create()
|
||||
}
|
||||
|
||||
/**
|
||||
* Add items to this collection
|
||||
* @param objs java.util.Iterable
|
||||
* @return B
|
||||
*/
|
||||
fun items(objs: Iterable<ASObject?>?): B {
|
||||
if (objs == null) return this as B
|
||||
for (obj in objs) items(obj)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Add items to this collection
|
||||
* @param obj ASObject The object to add
|
||||
* @param objs ASObject[] Additional objects to add (vararg)
|
||||
* @return B
|
||||
*/
|
||||
fun items(obj: ASObject?, vararg objs: ASObject): B {
|
||||
if (obj == null) return this as B
|
||||
list.add(obj)
|
||||
if (objs != null) for (o in objs) list.add(o)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to this collection
|
||||
* @param obj com.google.common.base.Supplier
|
||||
* @return B
|
||||
*/
|
||||
fun items(obj: Supplier<out ASObject?>?): B {
|
||||
if (obj == null) return this as B
|
||||
items(obj.get())
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the total number of items (must be non-negative)
|
||||
* @param i int
|
||||
* @return B
|
||||
*/
|
||||
fun totalItems(i: Int): B {
|
||||
Preconditions.checkArgument(i >= 0)
|
||||
set("totalItems", i)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the number of items per page (must be non-negative)
|
||||
* @param i int
|
||||
* @return B
|
||||
*/
|
||||
fun itemsPerPage(i: Int): B {
|
||||
Preconditions.checkArgument(i >= 0)
|
||||
set("itemsPerPage", i)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the starting index (must be non-negative)
|
||||
* @param i int
|
||||
* @return B
|
||||
*/
|
||||
fun startIndex(i: Int): B {
|
||||
Preconditions.checkArgument(i >= 0)
|
||||
set("startIndex", i)
|
||||
return this as B
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated after the specified time
|
||||
* @param dt DateTime
|
||||
* @return B
|
||||
*/
|
||||
fun itemsAfter(dt: DateTime?): B? {
|
||||
return _dt("itemsAfter", dt)
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated after right now
|
||||
* @return B
|
||||
*/
|
||||
fun itemsAfterNow(): B? {
|
||||
return _dtNow("itemsAfter")
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific duration after now
|
||||
* @param duration Duration
|
||||
* @return B
|
||||
*/
|
||||
fun itemsAfterFromNow(duration: ReadableDuration?): B? {
|
||||
return _dtFromNow("itemsAfter", duration)
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific period after now
|
||||
* @param period
|
||||
* @return B
|
||||
*/
|
||||
fun itemsAfterFromNow(period: ReadablePeriod?): B? {
|
||||
return _dtFromNow("itemsAfter", period)
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific duration after now
|
||||
* @param v long
|
||||
* @param unit TimeUnit
|
||||
* @return B
|
||||
*/
|
||||
fun itemsAfterFromNow(v: Long, unit: TimeUnit?): B? {
|
||||
return _dtFromNow("itemsAfter", v, unit!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated before a specific time
|
||||
* @param dt DateTime
|
||||
* @return B
|
||||
*/
|
||||
fun itemsBefore(dt: DateTime?): B? {
|
||||
return _dt("itemsBefore", dt)
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated before now
|
||||
* @return B
|
||||
*/
|
||||
fun itemsBeforeNow(): B? {
|
||||
return _dtNow("itemsBefore")
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific duration
|
||||
* before now
|
||||
* @param duration Duration
|
||||
* @return B
|
||||
*/
|
||||
fun itemsBeforeFromNow(duration: ReadableDuration?): B? {
|
||||
return _dtFromNow("itemsBefore", duration)
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify that the collection contains items updated a specific period
|
||||
* before now
|
||||
* @param period
|
||||
* @return B
|
||||
*/
|
||||
fun itemsBeforeFromNow(period: ReadablePeriod?): B? {
|
||||
return _dtFromNow("itemsBefore", period)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method itemsBeforeFromNow.
|
||||
* @param v long
|
||||
* @param unit TimeUnit
|
||||
* @return B
|
||||
*/
|
||||
fun itemsBeforeFromNow(v: Long, unit: TimeUnit?): B? {
|
||||
return _dtFromNow("itemsBefore", v, unit!!)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a paging link
|
||||
* <pre>
|
||||
* Collection collection = Makers.collection()
|
||||
* .pageLink(Page.NEXT, "http://example.org")
|
||||
* .get();
|
||||
</pre> *
|
||||
* @param page
|
||||
* @param url
|
||||
* @return B
|
||||
*/
|
||||
fun pageLink(page: Page, url: String?): B? {
|
||||
return link(page.label, Makers.linkValue(url))
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a paging link
|
||||
* @param page
|
||||
* @param link
|
||||
* @return B
|
||||
*/
|
||||
fun pageLink(page: Page, link: LinkValue?): B? {
|
||||
return link(page.label, link)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a paging link
|
||||
* @param page
|
||||
* @param link
|
||||
* @return B
|
||||
*/
|
||||
fun pageLink(page: Page, link: Supplier<out LinkValue?>): B? {
|
||||
return link(page.label, link)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total number of items
|
||||
* @return int
|
||||
*/
|
||||
fun totalItems(): Int {
|
||||
return getInt("totalItems")
|
||||
}
|
||||
|
||||
/**
|
||||
* If not null, indicates that the collection only contains items
|
||||
* updated after the given instant
|
||||
* @return DateTime
|
||||
*/
|
||||
fun itemsAfter(): DateTime? {
|
||||
return this.getDateTime("itemsAfter")
|
||||
}
|
||||
|
||||
/**
|
||||
* If not null, indicates that the collection only contains items
|
||||
* updated before the given instant
|
||||
* @return DateTime
|
||||
*/
|
||||
fun itemsBefore(): DateTime? {
|
||||
return this.getDateTime("itemsBefore")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of items per page
|
||||
* @return int
|
||||
*/
|
||||
fun itemsPerPage(): Int {
|
||||
return this.getInt("itemsPerPage")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the start index for this page
|
||||
* @return int
|
||||
*/
|
||||
fun startIndex(): Int {
|
||||
return this.getInt("startIndex")
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a listing of Paging links that
|
||||
* exist on this collection
|
||||
* @return Iterable<Page>
|
||||
*/
|
||||
fun pages(): Iterable<Page> {
|
||||
val pages =
|
||||
ImmutableSet.builder<Page>()
|
||||
for (page in Page.entries) if (has(page.label)) pages.add(page)
|
||||
return pages.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a listing of Paging LinkValues
|
||||
* @param page Page The type of paging link to return
|
||||
* @return Iterable<LinkValue>
|
||||
*/
|
||||
fun pageLink(
|
||||
page: Page
|
||||
): Iterable<LinkValue?>? {
|
||||
return page.links(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a listing of Paging LinkValues
|
||||
* @param page Page The type of paging link to return
|
||||
* @param filter Predicate A filter
|
||||
* @return Iterable<LinkValue>
|
||||
*/
|
||||
fun pageLink(
|
||||
page: Page,
|
||||
filter: Predicate<in LinkValue?>
|
||||
): Iterable<LinkValue?> {
|
||||
return page.links(this, filter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first matching paging LinkValue
|
||||
* @param page Page The type of paging link to return
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstPageLink(
|
||||
page: Page
|
||||
): LinkValue? {
|
||||
return page.first(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first matching paging LinkValue
|
||||
* @param page Page the type of paging link to return
|
||||
* @param test com.google.common.base.Predicate a filter
|
||||
* @return LinkValue
|
||||
*/
|
||||
fun firstMatchingPageLink(
|
||||
page: Page,
|
||||
test: Predicate<in LinkValue?>
|
||||
): LinkValue? {
|
||||
return page.firstMatching(this, test)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the collection of items
|
||||
* @return java.util.Iterable<A>
|
||||
</A> */
|
||||
fun <A : ASObject?> items(): Iterable<A> {
|
||||
return get("items")!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a filtered collection of items
|
||||
* @param filter com.google.common.base.Predicate filter
|
||||
* @return java.util.Iterable<A>
|
||||
</A> */
|
||||
fun <A : ASObject?> items(
|
||||
filter: Predicate<in A>
|
||||
): Iterable<A> {
|
||||
return Iterables.filter(this.items(), filter)
|
||||
}
|
||||
|
||||
// Java Serialization Support
|
||||
@Throws(ObjectStreamException::class)
|
||||
override fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: Collection) :
|
||||
AbstractSerializedForm<Collection?>(obj) {
|
||||
override fun builder(): Builder {
|
||||
return Makers.collection()
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any? {
|
||||
return super.doReadResolve()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,608 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import com.ibm.common.activitystreams.i.Adapter
|
||||
import com.ibm.common.activitystreams.i.GsonWrapper
|
||||
import com.ibm.common.activitystreams.i.Schema
|
||||
import com.ibm.common.activitystreams.util.Module
|
||||
import java.io.*
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Future
|
||||
|
||||
/**
|
||||
* The IO object is responsible for serializing and deserializing
|
||||
* Activity Stream objects. Instances of IO should be created and
|
||||
* defined statically. IO instances are threadsafe and immutable
|
||||
* once created.
|
||||
*
|
||||
*
|
||||
* You can choose to use one of the default IO instances:
|
||||
*
|
||||
* <pre>
|
||||
* public static final IO io = IO.makeDefault();
|
||||
*
|
||||
* public static final IO prettyIo = IO.makeDefaultPrettyPrint();
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
* Or you can use the IO.Builder to construct and configure your
|
||||
* own IO instance with custom adapters, object properties and
|
||||
* type mappings:
|
||||
*
|
||||
* <pre>
|
||||
* import static
|
||||
*
|
||||
* public static final IO io =
|
||||
* IO.make()
|
||||
* .schema(
|
||||
* Makers.makeSchema().map(
|
||||
* Schema.object.template()
|
||||
* .as("foo", Foo.class)))
|
||||
* .adapter(Foo.class, new MyFooAdapter())
|
||||
* .get();
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
* Once created, you can use IO instances to parse Activity Streams
|
||||
* documents:
|
||||
*
|
||||
* <pre>
|
||||
* InputStream in = ...
|
||||
* Activity activity = io.readAsActivity(in);
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
* Or can use the IO instance to serialize:
|
||||
*
|
||||
* <pre>
|
||||
* OutputStream out = ...
|
||||
* Activity activity = ...
|
||||
* activity.writeTo(out, io);
|
||||
</pre> *
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class IO protected constructor(builder: Builder) {
|
||||
class Builder
|
||||
|
||||
: Supplier<IO> {
|
||||
val inner: GsonWrapper.Builder = GsonWrapper.make()
|
||||
private var schema: Schema? = null
|
||||
private val modules: ImmutableSet.Builder<Module> = ImmutableSet.builder()
|
||||
|
||||
fun using(module: Module): Builder {
|
||||
modules.add(module)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn pretty print on or off
|
||||
* @param on boolean
|
||||
* @return Builder
|
||||
*/
|
||||
fun prettyPrint(on: Boolean): Builder {
|
||||
inner.prettyPrint(on)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn pretty print on
|
||||
* @return Builder
|
||||
*/
|
||||
fun prettyPrint(): Builder {
|
||||
return prettyPrint(true)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an adapter
|
||||
* @param type Class
|
||||
* @param adapter Adapter<T>
|
||||
* @return Builder
|
||||
</T> */
|
||||
fun <T> adapter(
|
||||
type: Class<out T>?,
|
||||
adapter: Adapter<T>?
|
||||
): Builder {
|
||||
inner.adapter(type, adapter)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an adapter
|
||||
* @param type Class<? extends T>
|
||||
* @return Builder
|
||||
*/
|
||||
fun <T> adapter(
|
||||
type: Class<out T?>?
|
||||
): Builder {
|
||||
return adapter(type, null)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an adapter
|
||||
* @param type Class<? extends T>
|
||||
* @return Builder
|
||||
*/
|
||||
fun <T> hierarchicalAdapter(
|
||||
type: Class<out T>
|
||||
): Builder {
|
||||
return hierarchicalAdapter(type, null)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an adapter.
|
||||
* @param type Class
|
||||
* @param adapter Adapter<T>
|
||||
* @param hier boolean
|
||||
* @return Builder
|
||||
</T> */
|
||||
fun <T> hierarchicalAdapter(
|
||||
type: Class<out T>,
|
||||
adapter: Adapter<T>?
|
||||
): Builder {
|
||||
inner.adapter(type, adapter, true)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the schema
|
||||
* @param schema Schema
|
||||
* @return Builder
|
||||
*/
|
||||
fun schema(schema: Schema?): Builder {
|
||||
//inner.schema(schema);
|
||||
this.schema = schema
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the schema.
|
||||
* @param schema Supplier<Schema>
|
||||
* @return Builder
|
||||
</Schema> */
|
||||
fun schema(schema: Supplier<Schema?>): Builder {
|
||||
return schema(schema.get())
|
||||
}
|
||||
|
||||
override fun get(): IO {
|
||||
val mods: Iterable<Module> = modules.build()
|
||||
var schema = this.schema
|
||||
if (schema == null) {
|
||||
val builder = Schema.make()
|
||||
for (mod in mods) mod.apply(builder)
|
||||
schema = builder.get()
|
||||
}
|
||||
inner.schema(schema)
|
||||
for (module in modules.build()) module.apply(this, schema)
|
||||
return IO(this)
|
||||
}
|
||||
}
|
||||
|
||||
private val gson: GsonWrapper = builder.inner.get()
|
||||
|
||||
/**
|
||||
* Write the given object
|
||||
* @param w Writable
|
||||
* @return String
|
||||
*/
|
||||
fun write(w: Writable): String {
|
||||
val sw = StringWriter()
|
||||
w.writeTo(sw, this)
|
||||
return sw.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously write the given object
|
||||
* @param w
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<String>
|
||||
*/
|
||||
fun write(
|
||||
w: Writable,
|
||||
executor: ExecutorService
|
||||
): Future<String> {
|
||||
return executor.submit<String> { write(w) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the object to the given outputstream
|
||||
* @param w Writable
|
||||
* @param out OutputStream
|
||||
*/
|
||||
fun write(w: Writable?, out: OutputStream) {
|
||||
gson.write(w, out)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asychronously write the object to the given output stream
|
||||
* @param w
|
||||
* @param out
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<?>
|
||||
*/
|
||||
fun write(
|
||||
w: Writable?,
|
||||
out: OutputStream,
|
||||
executor: ExecutorService
|
||||
): Future<*> {
|
||||
return executor.submit { write(w, out) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Asychronously write the object to the given writer
|
||||
* @param w
|
||||
* @param out
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<?>
|
||||
*/
|
||||
fun write(
|
||||
w: Writable?,
|
||||
out: Writer?,
|
||||
executor: ExecutorService
|
||||
): Future<*> {
|
||||
return executor.submit { write(w, out) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the object to the given writer
|
||||
* @param w Writable
|
||||
* @param out Writer
|
||||
*/
|
||||
fun write(w: Writable?, out: Writer?) {
|
||||
gson.write(w, out)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given input stream and
|
||||
* return a parsed object of the given type
|
||||
* @param in
|
||||
* @param type
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<A extends ASObject>
|
||||
*/
|
||||
fun <A : ASObject?> readAs(
|
||||
`in`: InputStream,
|
||||
type: Class<out A>?,
|
||||
executor: ExecutorService
|
||||
): Future<A> {
|
||||
return executor.submit<A> { readAs(`in`, type) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given input stream and return a parsed object
|
||||
* of the given type
|
||||
* @param in InputStream
|
||||
* @param type Class
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> readAs(
|
||||
`in`: InputStream,
|
||||
type: Class<out A>?
|
||||
): A {
|
||||
return gson.readAs(`in`, type)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given reader and return a parsed
|
||||
* object of the given type
|
||||
* @param in
|
||||
* @param type
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<A extends ASObject>
|
||||
*/
|
||||
fun <A : ASObject?> readAs(
|
||||
`in`: Reader,
|
||||
type: Class<out A>?,
|
||||
executor: ExecutorService
|
||||
): Future<A> {
|
||||
return executor.submit<A> { readAs(`in`, type) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given reader and return a parsed object of the given type
|
||||
* @param in Reader
|
||||
* @param type Class
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> readAs(
|
||||
`in`: Reader,
|
||||
type: Class<out A>?
|
||||
): A {
|
||||
return gson.readAs(`in`, type)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given string and return a parsed object of
|
||||
* the given type
|
||||
* @param in
|
||||
* @param type
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<A extends ASObject>
|
||||
*/
|
||||
fun <A : ASObject?> readAs(
|
||||
`in`: String,
|
||||
type: Class<out A>?,
|
||||
executor: ExecutorService
|
||||
): Future<A> {
|
||||
return executor.submit<A> { readAs(`in`, type) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given string and return a parsed object of the given type
|
||||
* @param in String
|
||||
* @param type Class
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> readAs(
|
||||
`in`: String,
|
||||
type: Class<out A>?
|
||||
): A {
|
||||
return readAs(StringReader(`in`), type)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given string
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<ASObject>
|
||||
*/
|
||||
fun read(`in`: String, executor: ExecutorService): Future<ASObject> {
|
||||
return read(StringReader(`in`), executor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given string
|
||||
* @param in String
|
||||
* @return ASObject
|
||||
*/
|
||||
fun read(`in`: String): ASObject {
|
||||
return read(StringReader(`in`))
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given inputstream
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<ASObject>
|
||||
*/
|
||||
fun read(`in`: InputStream, executor: ExecutorService): Future<ASObject> {
|
||||
return readAs(`in`, ASObject::class.java, executor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given reader
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<ASObject>
|
||||
*/
|
||||
fun read(`in`: Reader, executor: ExecutorService): Future<ASObject> {
|
||||
return readAs(`in`, ASObject::class.java, executor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given input stream.
|
||||
* @param in InputStream
|
||||
* @return ASObject
|
||||
*/
|
||||
fun read(`in`: InputStream): ASObject {
|
||||
return readAs(`in`, ASObject::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the given input stream
|
||||
* @param in InputStream
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> readAs(`in`: InputStream): A {
|
||||
return read(`in`) as A
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given string as an Activity object
|
||||
* @param in String
|
||||
* @return Activity
|
||||
*/
|
||||
fun readAsActivity(`in`: String): Activity {
|
||||
return readAsActivity(StringReader(`in`))
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given string as an Activity object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Activity>
|
||||
*/
|
||||
fun readAsActivity(`in`: String, executor: ExecutorService): Future<Activity> {
|
||||
return readAsActivity(StringReader(`in`), executor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given inputstream as an Activity object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Activity>
|
||||
*/
|
||||
fun readAsActivity(`in`: InputStream, executor: ExecutorService): Future<Activity> {
|
||||
return readAs(
|
||||
`in`,
|
||||
Activity::class.java, executor
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given reader as an Activity object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Activity>
|
||||
*/
|
||||
fun readAsActivity(`in`: Reader, executor: ExecutorService): Future<Activity> {
|
||||
return readAs(
|
||||
`in`,
|
||||
Activity::class.java, executor
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given string as a Collection object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Collection>
|
||||
*/
|
||||
fun readAsCollection(`in`: String, executor: ExecutorService): Future<Collection> {
|
||||
return readAsCollection(StringReader(`in`), executor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given input stream as a Collection object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Collection>
|
||||
*/
|
||||
fun readAsCollection(`in`: InputStream, executor: ExecutorService): Future<Collection> {
|
||||
return readAs(
|
||||
`in`,
|
||||
Collection::class.java, executor
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously read the given reader as a Collection object
|
||||
* @param in
|
||||
* @param executor
|
||||
* @return java.util.concurrent.Future<Collection>
|
||||
*/
|
||||
fun readAsCollection(`in`: Reader, executor: ExecutorService): Future<Collection> {
|
||||
return readAs(
|
||||
`in`,
|
||||
Collection::class.java, executor
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given inputstream as an Activity.
|
||||
* @param in InputStream
|
||||
* @return Activity
|
||||
*/
|
||||
fun readAsActivity(`in`: InputStream): Activity {
|
||||
return readAs(`in`, Activity::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given string as a Collection.
|
||||
* @param in InputStream
|
||||
* @return Collection
|
||||
*/
|
||||
fun readAsCollection(`in`: String): Collection {
|
||||
return readAsCollection(StringReader(`in`))
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given inputstream as a Collection.
|
||||
* @param in InputStream
|
||||
* @return Collection
|
||||
*/
|
||||
fun readAsCollection(`in`: InputStream): Collection {
|
||||
return readAs(`in`, Collection::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given reader
|
||||
* @param in
|
||||
* @return ASObject
|
||||
*/
|
||||
fun read(`in`: Reader): ASObject {
|
||||
return readAs(`in`, ASObject::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given reader as an Activity
|
||||
* @param in Reader
|
||||
* @return Activity
|
||||
*/
|
||||
fun readAsActivity(`in`: Reader): Activity {
|
||||
return readAs(`in`, Activity::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the given reader as a Collection
|
||||
* @param in Reader
|
||||
* @return Collection
|
||||
*/
|
||||
fun readAsCollection(`in`: Reader): Collection {
|
||||
return readAs(`in`, Collection::class.java)
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Create a new IO.Builder
|
||||
* @return Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new IO.Builder that uses the given schema
|
||||
* @param schema Schema
|
||||
* @return IO
|
||||
*/
|
||||
fun makeWithSchema(schema: Schema?): IO {
|
||||
return make().schema(schema).get()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new IO.Builder that uses the given schema
|
||||
* @param schema Supplier
|
||||
* @return IO
|
||||
*/
|
||||
fun makeWithSchema(schema: Supplier<out Schema?>): IO {
|
||||
return makeWithSchema(schema.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Make or return the default IO instance
|
||||
* @return IO
|
||||
*/
|
||||
@JvmStatic
|
||||
fun makeDefault(vararg modules: Module): IO {
|
||||
val builder = make()
|
||||
if (modules != null) for (mod in modules) builder.using(mod)
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make or return a default IO instance with Pretty Print enabled
|
||||
* @return IO
|
||||
*/
|
||||
@JvmStatic
|
||||
fun makeDefaultPrettyPrint(vararg modules: Module): IO {
|
||||
val builder = make().prettyPrint()
|
||||
if (modules != null) for (mod in modules) builder.using(mod)
|
||||
return builder.get()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,327 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
* An Activity Streams 2.0 Link Value.
|
||||
*
|
||||
*
|
||||
* In Actvity Streams 2.0, Link Values can take one of three possible
|
||||
* forms:
|
||||
*
|
||||
*
|
||||
* * A String containing an absolute IRI
|
||||
* * An Activity String object
|
||||
* * An Array containing a mix of Strings or Objects
|
||||
*
|
||||
*
|
||||
*
|
||||
* For instance, the following are all valid examples of Link Values:
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "actor": "acct:joe@example.org"
|
||||
* }
|
||||
*
|
||||
* {
|
||||
* "actor": {
|
||||
* "objectType": "person",
|
||||
* "id": "acct:sally@example.org"
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* {
|
||||
* "actor": [
|
||||
* "acct:joe@example.org",
|
||||
* {
|
||||
* "objectType": "person",
|
||||
* "id": "acct:sally@example.org"
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
* The LinkValue interface provides a minimal abstraction over these
|
||||
* value options. Developers will have to check the ValueType of the LinkValue
|
||||
* (or do an instanceof check) in order to determine which kind of value
|
||||
* they are working with.
|
||||
*
|
||||
* <pre>
|
||||
* Activity activity = ...;
|
||||
* Iterable<LinkValue> actors = activity.actor();
|
||||
* for (LinkValue actor : actors) {
|
||||
* switch(actor.valueType()) {
|
||||
* case SIMPLE:
|
||||
* SimpleLinkValue s = (SimpleLinkValue)actor;
|
||||
* //...
|
||||
* break;
|
||||
* case OBJECT:
|
||||
* ASObject obj = (ASObject)actor;
|
||||
* //...
|
||||
* break;
|
||||
* }
|
||||
* }
|
||||
</LinkValue></pre> *
|
||||
*
|
||||
*
|
||||
* Methods that return Iterable<LinkValue> will never include an instance
|
||||
* of ArrayLinkValue as one of the Iterable values</LinkValue>
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
interface LinkValue
|
||||
|
||||
: Writable, Serializable {
|
||||
/**
|
||||
* Returns the LinkValue type
|
||||
* @return ValueType
|
||||
*/
|
||||
fun valueType(): ValueType
|
||||
|
||||
/**
|
||||
* A "Simple Link Value" is a string with a relative or absolute
|
||||
* URI or IRI value.
|
||||
*/
|
||||
class SimpleLinkValue
|
||||
internal constructor(builder: Builder) : AbstractWritable(builder),
|
||||
LinkValue, Serializable {
|
||||
class Builder
|
||||
|
||||
: AbstractWritableBuilder<SimpleLinkValue?, Builder?>() {
|
||||
var iri: String? = null
|
||||
|
||||
/**
|
||||
* Set the url
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
*/
|
||||
fun url(iri: String?): Builder {
|
||||
this.iri = iri
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return SimpleLinkValue
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): SimpleLinkValue {
|
||||
return SimpleLinkValue(this)
|
||||
}
|
||||
}
|
||||
|
||||
private val iri: String
|
||||
|
||||
init {
|
||||
this.iri = builder.iri!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the url
|
||||
* @return String
|
||||
*/
|
||||
fun url(): String {
|
||||
return iri
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return iri
|
||||
}
|
||||
|
||||
override fun valueType(): ValueType {
|
||||
return ValueType.SIMPLE
|
||||
}
|
||||
|
||||
// Java Serialization Support
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: SimpleLinkValue) : Serializable {
|
||||
private val iri = obj.iri
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
return Makers.linkValue(iri)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Creates a new builder
|
||||
* @return Builder
|
||||
*/
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
* @param url String
|
||||
* @return LinkValue
|
||||
*/
|
||||
@JvmStatic
|
||||
fun make(url: String?): LinkValue {
|
||||
return Builder().url(url).get()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An Array Link value is a JSON Array of one or more Simple or Object
|
||||
* Link Values. Array Link Values MUST NOT contain nested arrays.
|
||||
*/
|
||||
class ArrayLinkValue
|
||||
internal constructor(builder: Builder) : AbstractWritable(builder),
|
||||
Iterable<LinkValue?>, LinkValue, Serializable {
|
||||
class Builder
|
||||
|
||||
: AbstractWritableBuilder<ArrayLinkValue?, Builder?>() {
|
||||
val links: ImmutableList.Builder<LinkValue> = ImmutableList.builder()
|
||||
|
||||
/**
|
||||
* Add one or more items
|
||||
* @param value String
|
||||
* @param values String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun add(value: String?, vararg values: String?): Builder {
|
||||
if (value != null) add(SimpleLinkValue.make(value))
|
||||
if (values != null) for (v in values) add(SimpleLinkValue.make(v))
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Add one or more link values
|
||||
* @param links Iterable<LinkValue>
|
||||
* @return Builder
|
||||
</LinkValue> */
|
||||
fun add(links: Iterable<LinkValue?>): Builder {
|
||||
for (l in links) add(l)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Add one or more link values
|
||||
* @param values LinkValue[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun add(value: LinkValue?, vararg values: LinkValue): Builder {
|
||||
if (value != null) {
|
||||
Preconditions.checkArgument(value.valueType() != ValueType.ARRAY)
|
||||
links.add(value)
|
||||
}
|
||||
if (values != null) for (v in values) {
|
||||
Preconditions.checkArgument(v.valueType() != ValueType.ARRAY)
|
||||
links.add(v)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a link value
|
||||
* @param value Supplier
|
||||
* @return Builder
|
||||
*/
|
||||
fun add(value: Supplier<out LinkValue>): Builder {
|
||||
val `val` = value.get()
|
||||
Preconditions.checkArgument(`val`.valueType() != ValueType.ARRAY)
|
||||
links.add(`val`)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return ArrayLinkValue
|
||||
* @see com.google.common.base.Supplier.get
|
||||
*/
|
||||
override fun get(): ArrayLinkValue {
|
||||
return ArrayLinkValue(this)
|
||||
}
|
||||
}
|
||||
|
||||
private val links: ImmutableList<LinkValue> = builder.links.build()
|
||||
|
||||
/**
|
||||
* Method iterator.
|
||||
* @return Iterator<LinkValue>
|
||||
* @see java.lang.Iterable.iterator
|
||||
</LinkValue> */
|
||||
override fun iterator(): MutableIterator<LinkValue> {
|
||||
return links.iterator()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return links.toString()
|
||||
}
|
||||
|
||||
override fun valueType(): ValueType {
|
||||
return ValueType.ARRAY
|
||||
}
|
||||
|
||||
// Java Serialization Support
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: ArrayLinkValue) : Serializable {
|
||||
private val list = obj.links
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
return Makers.linkValues().add(list)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Create a new builder
|
||||
* @return ArrayLinkValue.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,187 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.ibm.common.activitystreams.LinkValue.ArrayLinkValue
|
||||
import com.ibm.common.activitystreams.LinkValue.SimpleLinkValue
|
||||
import com.ibm.common.activitystreams.NLV.MapNLV
|
||||
import com.ibm.common.activitystreams.NLV.SimpleNLV
|
||||
import com.ibm.common.activitystreams.TypeValue.SimpleTypeValue
|
||||
import com.ibm.common.activitystreams.i.Schema
|
||||
|
||||
/**
|
||||
* Utility class for creating Makers for all of the various objects.
|
||||
*/
|
||||
object Makers {
|
||||
/**
|
||||
* Make a new ActionsValue.Builder
|
||||
* @return ParametersValue.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun actions(): ActionsValue.Builder {
|
||||
return ActionsValue.Builder()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new Schema.Builder
|
||||
* @return Schema.Builder
|
||||
*/
|
||||
fun schema(): Schema.Builder {
|
||||
return Schema.make()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new IO.builder
|
||||
* @return IO.Builder
|
||||
*/
|
||||
fun io(): IO.Builder {
|
||||
return IO.Companion.make()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new Activity.Builder
|
||||
* @return Activity.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun activity(): Activity.Builder {
|
||||
return Activity.Builder()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new Collection.Builder
|
||||
* @return Collection.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun collection(): Collection.Builder {
|
||||
return Collection.Builder()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new ASObject.Builder
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun `object`(): ASObject.Builder {
|
||||
return ASObject.Builder()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new ASObject.Builder with a specific objectType
|
||||
* @param type String
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun `object`(type: String?): ASObject.Builder? {
|
||||
return `object`().objectType(type)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an Object from the given Map
|
||||
* @param map
|
||||
* @return ASObject
|
||||
*/
|
||||
fun objectFrom(map: Map<String?, *>): ASObject {
|
||||
val builder = `object`()
|
||||
for ((key, value) in ImmutableMap.copyOf(
|
||||
map
|
||||
)) builder[key!!] =
|
||||
value
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new ASObject.Builder
|
||||
* @param type TypeValue
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun `object`(type: TypeValue?): ASObject.Builder? {
|
||||
return `object`().objectType(type)
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new TypeValue
|
||||
* @param iri String
|
||||
* @return TypeValue
|
||||
*/
|
||||
@JvmStatic
|
||||
fun type(iri: String?): TypeValue {
|
||||
return SimpleTypeValue.Companion.make(iri)
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new LinkValue
|
||||
* @param iri String
|
||||
* @return LinkValue
|
||||
*/
|
||||
@JvmStatic
|
||||
fun linkValue(iri: String?): LinkValue {
|
||||
return SimpleLinkValue.Companion.make(iri)
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new ArrayLinkValue.Builder
|
||||
* @return LinkValue.ArrayLinkValue.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun linkValues(): ArrayLinkValue.Builder {
|
||||
return ArrayLinkValue.Companion.make()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new MapNLV.Builder
|
||||
* @return NLV.MapNLV.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun nlv(): MapNLV.Builder {
|
||||
return MapNLV.Companion.make()
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new SimpleNLV value
|
||||
* @param val String
|
||||
* @return NLV.SimpleNLV
|
||||
*/
|
||||
@JvmStatic
|
||||
fun nlv(`val`: String?): SimpleNLV {
|
||||
return SimpleNLV.Companion.make(`val`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new verb ASObject.Builder
|
||||
* @param id String
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
fun verb(id: String?): ASObject.Builder? {
|
||||
return `object`("verb")!!.id(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a new objectType ASObject.Builder
|
||||
* @param id String
|
||||
* @return ASObject.Builder
|
||||
*/
|
||||
fun objectType(id: String?): ASObject.Builder? {
|
||||
return `object`("objectType")!!.id(id)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,265 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import com.google.common.base.Objects
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.google.common.collect.Maps
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
|
||||
/**
|
||||
* An Activity Streams 2.0 Natural Language Value...
|
||||
*
|
||||
*
|
||||
* The value is either a simple string or an object
|
||||
* with multiple Language-Tag keys and values...
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "objectType": "note",
|
||||
* "displayName": {
|
||||
* "en": "My Title In English",
|
||||
* "fr": "Mon titre en français"
|
||||
* }
|
||||
* }
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
*
|
||||
* ASObject obj = ...
|
||||
* System.out.println(obj.displayNameString("en"));
|
||||
* System.out.println(obj.displayNameString("fr"));
|
||||
*
|
||||
* NLV nlv = obj.displayName();
|
||||
* switch(nlv.valueType()) {
|
||||
* case SIMPLE:
|
||||
* SimpleNLV s = (SimpleNLV)nlv;
|
||||
* ...
|
||||
* break;
|
||||
* case OBJECT:
|
||||
* MapNLV m = (MapNLV)nlv;
|
||||
* ...
|
||||
* break;
|
||||
* }
|
||||
*
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
interface NLV
|
||||
|
||||
: Writable, Serializable {
|
||||
/**
|
||||
* Returns the value type. Either ValueType.SIMPLE or ValueType.OBJECT
|
||||
* @return ValueType
|
||||
*/
|
||||
fun valueType(): ValueType
|
||||
|
||||
class SimpleNLV
|
||||
internal constructor(builder: Builder) : AbstractWritable(builder), NLV,
|
||||
Serializable {
|
||||
private val `val`: String
|
||||
|
||||
init {
|
||||
this.`val` = builder.`val`!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value
|
||||
* @return String
|
||||
*/
|
||||
fun value(): String {
|
||||
return `val`
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Objects.hashCode(`val`)
|
||||
}
|
||||
|
||||
override fun equals(obj: Any?): Boolean {
|
||||
if (this === obj) return true
|
||||
if (obj == null) return false
|
||||
if (javaClass != obj.javaClass) return false
|
||||
val other = obj as SimpleNLV
|
||||
return Objects.equal(`val`, other.`val`)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return `val`
|
||||
}
|
||||
|
||||
class Builder
|
||||
|
||||
: AbstractWritableBuilder<SimpleNLV?, Builder?>() {
|
||||
var `val`: String? = null
|
||||
|
||||
fun value(`val`: String?): Builder {
|
||||
this.`val` = `val`
|
||||
return this
|
||||
}
|
||||
|
||||
override fun get(): SimpleNLV {
|
||||
return SimpleNLV(this)
|
||||
}
|
||||
}
|
||||
|
||||
override fun valueType(): ValueType {
|
||||
return ValueType.SIMPLE
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: SimpleNLV) : Serializable {
|
||||
private val value = obj.`val`
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
return Makers.nlv(value)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Create a new builder
|
||||
* @return Builder
|
||||
*/
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance
|
||||
* @param value String
|
||||
* @return SimpleNLV
|
||||
*/
|
||||
@JvmStatic
|
||||
fun make(value: String?): SimpleNLV {
|
||||
return make().value(value).get()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MapNLV
|
||||
internal constructor(builder: Builder) : AbstractWritable(builder), NLV,
|
||||
Iterable<String?> {
|
||||
private val vals: ImmutableMap<String, String?> = builder.vals.build()
|
||||
|
||||
@Transient
|
||||
private var hash = 1
|
||||
|
||||
fun value(lang: String?): String? {
|
||||
return vals[lang]
|
||||
}
|
||||
|
||||
fun has(lang: String?): Boolean {
|
||||
return vals.containsKey(lang)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
if (hash == 1) hash = Objects.hashCode(vals)
|
||||
return hash
|
||||
}
|
||||
|
||||
override fun equals(obj: Any?): Boolean {
|
||||
if (this === obj) return true
|
||||
if (obj == null) return false
|
||||
if (javaClass != obj.javaClass) return false
|
||||
val other = obj as MapNLV
|
||||
return Maps.difference(vals, other.vals)
|
||||
.areEqual()
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<String> {
|
||||
return vals.keys.iterator()
|
||||
}
|
||||
|
||||
class Builder
|
||||
|
||||
: AbstractWritableBuilder<MapNLV?, Builder?>() {
|
||||
val vals: ImmutableMap.Builder<String, String?> = ImmutableMap.builder()
|
||||
|
||||
fun from(other: MapNLV, lang: String?): Builder {
|
||||
for (l in other) if (!l.equals(lang, ignoreCase = true)) set(l, other.value(l))
|
||||
return this
|
||||
}
|
||||
|
||||
operator fun set(lang: String?, `val`: String?): Builder {
|
||||
vals.put(lang!!, `val`!!)
|
||||
return this
|
||||
}
|
||||
|
||||
override fun get(): MapNLV {
|
||||
return MapNLV(this)
|
||||
}
|
||||
}
|
||||
|
||||
override fun valueType(): ValueType {
|
||||
return ValueType.OBJECT
|
||||
}
|
||||
|
||||
fun toMap(): Map<String, String?> {
|
||||
return vals
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: MapNLV) : Serializable {
|
||||
private val map = obj.vals
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
val builder =
|
||||
Makers.nlv()
|
||||
for ((key, value) in map) builder.set(
|
||||
key,
|
||||
value
|
||||
)
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,177 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import com.ibm.common.activitystreams.util.AbstractWritable
|
||||
import java.io.ObjectStreamException
|
||||
import java.io.Serializable
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* In Activity Streams 2.0, the "objectType" and "verb" properties,
|
||||
* as well as several other properties, are defined as "Type Values".
|
||||
* A "Type Value" can be either a simple token string, an absolute
|
||||
* IRI string, or an ASObject.
|
||||
*
|
||||
* <pre>
|
||||
* {
|
||||
* "verb": "post"
|
||||
* }
|
||||
*
|
||||
* {
|
||||
* "verb": "urn:example:verbs:foo"
|
||||
* }
|
||||
*
|
||||
* {
|
||||
* "verb": {
|
||||
* "id": "urn:example:verbs:foo",
|
||||
* "displayName": "Foo"
|
||||
* }
|
||||
* }
|
||||
</pre> *
|
||||
*
|
||||
*
|
||||
* The TypeValue interface provides a minimal abstraction
|
||||
* over these possible values. Developers should check valueType
|
||||
* to determine which type of TypeValue they are working with.
|
||||
*
|
||||
* <pre>
|
||||
* Activity activity = ...
|
||||
* TypeValue tv = activity.verb();
|
||||
*
|
||||
* System.out.println(tv.id());
|
||||
*
|
||||
* switch(tv.valueType()) {
|
||||
* case SIMPLE:
|
||||
* SimpleTypeValue s = (SimpleTypeValue)tv;
|
||||
* ...
|
||||
* break;
|
||||
* case OBJECT:
|
||||
* ASObject o (ASObject)tv;
|
||||
* ...
|
||||
* break;
|
||||
* }
|
||||
</pre> *
|
||||
*
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
interface TypeValue
|
||||
|
||||
: Writable, Serializable {
|
||||
/**
|
||||
* Return the type value identifier
|
||||
* @return String
|
||||
*/
|
||||
fun id(): String?
|
||||
|
||||
fun valueType(): ValueType
|
||||
|
||||
class SimpleTypeValue
|
||||
internal constructor(builder: Builder) : AbstractWritable(builder),
|
||||
TypeValue, Serializable {
|
||||
class Builder
|
||||
|
||||
: AbstractWritableBuilder<SimpleTypeValue?, Builder?>() {
|
||||
var iri: String? = null
|
||||
|
||||
/**
|
||||
* Set the url
|
||||
* @param iri String
|
||||
* @return Builder
|
||||
*/
|
||||
fun url(iri: String?): Builder {
|
||||
this.iri = iri
|
||||
return this
|
||||
}
|
||||
|
||||
override fun get(): SimpleTypeValue {
|
||||
return SimpleTypeValue(this)
|
||||
}
|
||||
}
|
||||
|
||||
private val iri: String
|
||||
|
||||
init {
|
||||
this.iri = builder.iri!!
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Objects.hash(iri)
|
||||
}
|
||||
|
||||
override fun equals(obj: Any?): Boolean {
|
||||
if (this === obj) return true
|
||||
if (obj == null) return false
|
||||
if (javaClass != obj.javaClass) return false
|
||||
val other = obj as SimpleTypeValue
|
||||
return iri == other.iri
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the type value identifier
|
||||
* @return String
|
||||
* @see com.ibm.common.activitystreams.TypeValue.id
|
||||
*/
|
||||
override fun id(): String? {
|
||||
return iri
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return iri
|
||||
}
|
||||
|
||||
override fun valueType(): ValueType {
|
||||
return ValueType.SIMPLE
|
||||
}
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun writeReplace(): Any {
|
||||
return SerializedForm(this)
|
||||
}
|
||||
|
||||
private class SerializedForm
|
||||
(obj: SimpleTypeValue) : Serializable {
|
||||
private val iri = obj.iri
|
||||
|
||||
@Throws(ObjectStreamException::class)
|
||||
fun readResolve(): Any {
|
||||
return Makers.type(iri)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -1975376657749952999L
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun make(url: String?): TypeValue {
|
||||
return make().url(url).get()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
core/src/main/java/com/ibm/common/activitystreams/ValueType.java → core/src/main/kotlin/com/ibm/common/activitystreams/ValueType.kt
Executable file → Normal file
12
core/src/main/java/com/ibm/common/activitystreams/ValueType.java → core/src/main/kotlin/com/ibm/common/activitystreams/ValueType.kt
Executable file → Normal file
|
|
@ -19,18 +19,18 @@
|
|||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams;
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
/**
|
||||
* Used for TypeValue, LinkValue and NLV interfaces to distinguish
|
||||
* between the possible value options.
|
||||
*
|
||||
*
|
||||
* LinkValue.valueType() can return SIMPLE, OBJECT or ARRAY
|
||||
* TypeValue.valueType() can return SIMPLE or OBJECT
|
||||
* NLV.valueType() can return SIMPLE or OBJECT
|
||||
*/
|
||||
public enum ValueType {
|
||||
SIMPLE,
|
||||
OBJECT,
|
||||
ARRAY;
|
||||
enum class ValueType {
|
||||
SIMPLE,
|
||||
OBJECT,
|
||||
ARRAY
|
||||
}
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams
|
||||
|
||||
import java.io.OutputStream
|
||||
import java.io.Writer
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Future
|
||||
|
||||
/**
|
||||
* Base interface for all objects that serialize to IO object instances
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
interface Writable {
|
||||
/**
|
||||
* Write the object to the output stream using the default IO instance
|
||||
* @param out OutputStream
|
||||
*/
|
||||
fun writeTo(out: OutputStream)
|
||||
|
||||
/**
|
||||
* Asynchronously write the object to the output stream using the
|
||||
* default IO instance
|
||||
* @param out
|
||||
* @param executor
|
||||
* @return Future<?>
|
||||
*/
|
||||
fun writeTo(out: OutputStream, executor: ExecutorService): Future<*>
|
||||
|
||||
/**
|
||||
* Write the object to the Writer using the default IO instance
|
||||
* @param out Writer
|
||||
*/
|
||||
fun writeTo(out: Writer)
|
||||
|
||||
/**
|
||||
* Asynchronously write the object to the writer using the default IO instance
|
||||
* @param out
|
||||
* @param executor
|
||||
* @return Future<?>
|
||||
*/
|
||||
fun writeTo(out: Writer, executor: ExecutorService): Future<*>
|
||||
|
||||
/**
|
||||
* Write the object to the output stream using the given IO instance
|
||||
* @param out OutputStream
|
||||
* @param io IO
|
||||
*/
|
||||
fun writeTo(out: OutputStream, io: IO)
|
||||
|
||||
/**
|
||||
* Asynchronously write the object to the output stream using the given
|
||||
* IO instance.
|
||||
* @param out
|
||||
* @param io
|
||||
* @param executor
|
||||
* @return Future<?>
|
||||
*/
|
||||
fun writeTo(out: OutputStream, io: IO, executor: ExecutorService): Future<*>
|
||||
|
||||
/**
|
||||
* Write the object to the writer using the given IO instance
|
||||
* @param out Writer
|
||||
* @param io IO
|
||||
*/
|
||||
fun writeTo(out: Writer, io: IO)
|
||||
|
||||
/**
|
||||
* Asynchronously write the object to the writer using the given IO instance
|
||||
* @param out
|
||||
* @param io
|
||||
* @param executor
|
||||
* @return Future<?>
|
||||
*/
|
||||
fun writeTo(out: Writer, io: IO, executor: ExecutorService): Future<*>
|
||||
}
|
||||
|
|
@ -0,0 +1,303 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Converter
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.collect.ImmutableSet
|
||||
import com.google.gson.*
|
||||
import com.ibm.common.activitystreams.*
|
||||
import com.ibm.common.activitystreams.Collection
|
||||
import com.ibm.common.activitystreams.Makers.activity
|
||||
import com.ibm.common.activitystreams.Makers.collection
|
||||
import com.ibm.common.activitystreams.Makers.`object`
|
||||
import java.lang.reflect.Type
|
||||
import kotlin.collections.Map
|
||||
import kotlin.collections.contains
|
||||
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
open class ASObjectAdapter
|
||||
|
||||
/**
|
||||
* Constructor for ASObjectAdapter.
|
||||
* @param schema Schema
|
||||
*/(private val schema: Schema) : Adapter<ASObject>() {
|
||||
protected fun schema(): Schema {
|
||||
return schema
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param obj ASObject
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
*
|
||||
* @return JsonElement
|
||||
*/
|
||||
override fun serialize(
|
||||
obj: ASObject,
|
||||
type: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
val el: JsonObject =
|
||||
JsonObject()
|
||||
for (key: String in obj) {
|
||||
val `val`: Any? = obj.get<Any>(key)
|
||||
if (`val` != null) {
|
||||
el.add(
|
||||
key,
|
||||
context.serialize(
|
||||
`val`,
|
||||
`val`.javaClass
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
return el
|
||||
}
|
||||
|
||||
protected open fun knowsType(type: Type?): Boolean {
|
||||
return knownTypes.contains(type)
|
||||
}
|
||||
|
||||
protected open fun builderFor(type: Type): ASObject.AbstractBuilder<*, *>? {
|
||||
if (type === Collection::class.java) return collection()
|
||||
else if (type === Activity::class.java) return activity()
|
||||
else return null
|
||||
}
|
||||
|
||||
protected open fun modelFor(type: Type): Model? {
|
||||
if (type === Collection::class.java) return schema.forObjectClassOrType(
|
||||
Collection.Builder::class.java,
|
||||
"collection"
|
||||
)
|
||||
else if (type === Activity::class.java) return schema.forObjectClassOrType(
|
||||
Activity.Builder::class.java,
|
||||
"activity"
|
||||
)
|
||||
else return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param element JsonElement
|
||||
* @param type Type
|
||||
* @param context JsonDeserializationContext
|
||||
* @return ASObject
|
||||
* @throws JsonParseException
|
||||
* @see com.google.gson.JsonDeserializer.deserialize
|
||||
*/
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
element: JsonElement,
|
||||
type: Type,
|
||||
context: JsonDeserializationContext
|
||||
): ASObject {
|
||||
val obj: JsonObject = element as JsonObject
|
||||
var builder: ASObject.AbstractBuilder<*, *>? = null
|
||||
var propMap: Model? = null
|
||||
var tv: TypeValue? = null
|
||||
|
||||
if (knowsType(type)) {
|
||||
builder = builderFor(type)
|
||||
propMap = modelFor(type)
|
||||
} else {
|
||||
if (obj.has("objectType")) {
|
||||
tv = context.deserialize(
|
||||
obj.get("objectType"),
|
||||
TypeValue::class.java
|
||||
)
|
||||
val _class: Class<out ASObject.AbstractBuilder<*, *>?>? =
|
||||
schema.builderForObjectTypeOrClass(tv?.id(), type as Class<*>?)
|
||||
if (_class != null) {
|
||||
propMap = schema.forObjectClassOrType(_class, tv?.id())
|
||||
if (!_class.isInterface()) {
|
||||
try {
|
||||
builder = _class.getConstructor(String::class.java).newInstance(tv?.id())
|
||||
} catch (t: Throwable) {
|
||||
try {
|
||||
builder = _class.newInstance()
|
||||
builder!!.set("objectType", tv)
|
||||
} catch (t2: Throwable) {
|
||||
builder = `object`(tv)
|
||||
}
|
||||
}
|
||||
} else builder = `object`(tv)
|
||||
} else {
|
||||
builder = `object`(tv)
|
||||
propMap = schema.forObjectClassOrType(
|
||||
ASObject.Builder::class.java, tv?.id()
|
||||
)
|
||||
}
|
||||
} else {
|
||||
if (obj.has("verb") &&
|
||||
(obj.has("actor") ||
|
||||
obj.has("object") ||
|
||||
obj.has("target"))
|
||||
) {
|
||||
builder = activity()
|
||||
propMap = schema.forObjectClassOrType(
|
||||
Activity.Builder::class.java, "activity"
|
||||
)
|
||||
} else if (obj.has("items")) {
|
||||
builder = collection()
|
||||
propMap = schema.forObjectClassOrType(
|
||||
Collection.Builder::class.java,
|
||||
"collection"
|
||||
)
|
||||
} else {
|
||||
val _class: Class<out ASObject.AbstractBuilder<*, *>?>? =
|
||||
schema.builderFor(type as Class<*>?)
|
||||
if (_class != null) {
|
||||
if (!_class.isInterface()) {
|
||||
try {
|
||||
builder = _class.newInstance()
|
||||
} catch (t: Throwable) {
|
||||
builder = `object`()
|
||||
}
|
||||
} else builder = `object`()
|
||||
}
|
||||
if (builder == null) builder = `object`() // anonymous
|
||||
|
||||
propMap = schema.forObjectClass(builder.javaClass)
|
||||
propMap = if (propMap != null) propMap else schema.forObjectClass(
|
||||
ASObject.Builder::class.java
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (entry: Map.Entry<String, JsonElement> in obj.entrySet()) {
|
||||
val name: String = entry.key
|
||||
if (name.equals("objectType", ignoreCase = true)) continue
|
||||
val _class: Class<*>? = propMap!!.get(name)
|
||||
val `val`: JsonElement = entry.value
|
||||
if (`val`.isJsonPrimitive()) builder!!.set(
|
||||
name,
|
||||
if (_class != null) context.deserialize(
|
||||
`val`,
|
||||
_class
|
||||
) else primConverter.convert(`val`.getAsJsonPrimitive())
|
||||
)
|
||||
else if (`val`.isJsonArray()) {
|
||||
builder!!.set(
|
||||
name,
|
||||
if (LinkValue::class.java.isAssignableFrom(if (_class != null) _class else Any::class.java)) context.deserialize<Any>(
|
||||
`val`,
|
||||
LinkValue::class.java
|
||||
) else convert(
|
||||
`val`.getAsJsonArray(),
|
||||
_class,
|
||||
context,
|
||||
ImmutableList.builder()
|
||||
)
|
||||
)
|
||||
} else if (`val`.isJsonObject()) builder!!.set(
|
||||
name,
|
||||
context.deserialize(
|
||||
`val`,
|
||||
if (propMap.has(name)) propMap.get<Type>(name) else ASObject::class.java
|
||||
) as Any?
|
||||
)
|
||||
}
|
||||
return builder!!.get()!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method convert.
|
||||
* @param arr JsonArray
|
||||
* @param _class Class
|
||||
* @param context JsonDeserializationContext
|
||||
* @param list ImmutableList.Builder<Object>
|
||||
* @return ImmutableList<Object>
|
||||
</Object></Object> */
|
||||
private fun convert(
|
||||
arr: JsonArray,
|
||||
_class: Class<*>?,
|
||||
context: JsonDeserializationContext,
|
||||
list: ImmutableList.Builder<Any>
|
||||
): ImmutableList<Any> {
|
||||
processArray(arr, _class, context, list)
|
||||
return list.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method processArray.
|
||||
* @param arr JsonArray
|
||||
* @param _class Class
|
||||
* @param context JsonDeserializationContext
|
||||
* @param list ImmutableList.Builder<Object>
|
||||
</Object> */
|
||||
private fun processArray(
|
||||
arr: JsonArray,
|
||||
_class: Class<*>?,
|
||||
context: JsonDeserializationContext,
|
||||
list: ImmutableList.Builder<Any>
|
||||
) {
|
||||
for (mem: JsonElement in arr) {
|
||||
if (mem.isJsonPrimitive()) list.add(
|
||||
if (_class != null) context.deserialize(mem, _class) else primConverter.convert(
|
||||
mem.getAsJsonPrimitive()
|
||||
)
|
||||
)
|
||||
else if (mem.isJsonObject()) list.add(
|
||||
*context.deserialize(
|
||||
mem,
|
||||
if (_class != null) _class else ASObject::class.java
|
||||
)
|
||||
)
|
||||
else if (mem.isJsonArray()) list.add(
|
||||
convert(
|
||||
mem.getAsJsonArray(),
|
||||
_class,
|
||||
context,
|
||||
ImmutableList.builder()
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val knownTypes: ImmutableSet<out Type> = ImmutableSet.of(
|
||||
Collection::class.java,
|
||||
Activity::class.java
|
||||
)
|
||||
|
||||
val primConverter: Converter<JsonPrimitive, Any> = object : Converter<JsonPrimitive, Any>() {
|
||||
override fun doBackward(a: Any): JsonPrimitive {
|
||||
if (a is Boolean) return JsonPrimitive(a)
|
||||
else if (a is Number) return JsonPrimitive(a)
|
||||
else return JsonPrimitive(a.toString())
|
||||
}
|
||||
|
||||
override fun doForward(b: JsonPrimitive): Any {
|
||||
if (b.isBoolean()) return b.getAsBoolean()
|
||||
else if (b.isNumber()) return b.getAsNumber()
|
||||
else return b.getAsString()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -19,16 +19,14 @@
|
|||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.internal;
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.JsonDeserializer
|
||||
import com.google.gson.JsonSerializer
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
public abstract class Adapter<T>
|
||||
implements JsonSerializer<T>, JsonDeserializer<T> {
|
||||
|
||||
}
|
||||
abstract class Adapter<T>
|
||||
: JsonSerializer<T>, JsonDeserializer<T>
|
||||
|
|
@ -0,0 +1,304 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Optional
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.common.base.Throwables
|
||||
import com.google.common.collect.*
|
||||
import com.google.common.net.MediaType
|
||||
import com.google.gson.*
|
||||
import com.google.gson.internal.LazilyParsedNumber
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.ActionsValue
|
||||
import com.ibm.common.activitystreams.LinkValue
|
||||
import com.ibm.common.activitystreams.Makers.actions
|
||||
import com.ibm.common.activitystreams.Makers.linkValues
|
||||
import com.ibm.common.activitystreams.NLV
|
||||
import com.ibm.common.activitystreams.util.AbstractDictionaryObjectAdapter
|
||||
import org.joda.time.DateTime
|
||||
import org.joda.time.Duration
|
||||
import org.joda.time.Interval
|
||||
import org.joda.time.Period
|
||||
import org.joda.time.format.ISODateTimeFormat
|
||||
import java.lang.reflect.Type
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
internal object Adapters {
|
||||
/**
|
||||
* Method forEnum.
|
||||
* @param _enumClass Class<E>
|
||||
* @return EnumAdapter<E>
|
||||
</E></E> */
|
||||
fun <E : Enum<E>> forEnum(_enumClass: Class<E>): EnumAdapter<E> {
|
||||
return EnumAdapter(_enumClass)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method forEnum.
|
||||
* @param _enumClass Class<E>
|
||||
* @param or E
|
||||
* @return EnumAdapter<E>
|
||||
</E></E> */
|
||||
fun <E : Enum<E>> forEnum(_enumClass: Class<E>, or: E): EnumAdapter<E> {
|
||||
return EnumAdapter(_enumClass)
|
||||
}
|
||||
|
||||
val NLV: Adapter<NLV> = NaturalLanguageValueAdapter()
|
||||
|
||||
val ACTIONS: Adapter<ActionsValue> =
|
||||
object : AbstractDictionaryObjectAdapter<LinkValue, ActionsValue, ActionsValue.Builder>(
|
||||
LinkValue::class.java
|
||||
) {
|
||||
override fun serialize(
|
||||
actions: ActionsValue,
|
||||
type: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
val obj = JsonObject()
|
||||
for (verb: String? in actions) {
|
||||
val links =
|
||||
actions.get(verb)
|
||||
obj.add(
|
||||
verb,
|
||||
context.serialize(
|
||||
if (Iterables.size(links) == 1) {
|
||||
// if there's only one, serialize just 1
|
||||
Iterables.getFirst(links, null)
|
||||
} else {
|
||||
// otherwise, serialize the list
|
||||
linkValues().add(links).get()
|
||||
},
|
||||
LinkValue::class.java
|
||||
)
|
||||
)
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
override fun builder(): ActionsValue.Builder {
|
||||
return actions()
|
||||
}
|
||||
}
|
||||
|
||||
val ITERABLE: Adapter<Iterable<*>> = object : Adapter<Iterable<*>>() {
|
||||
override fun serialize(
|
||||
i: Iterable<*>,
|
||||
type: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
val ary = JsonArray()
|
||||
for (obj in i) ary.add(context.serialize(obj, obj?.javaClass))
|
||||
return ary
|
||||
}
|
||||
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
arg0: JsonElement, arg1: Type,
|
||||
arg2: JsonDeserializationContext
|
||||
): Iterable<*> {
|
||||
return listOf<String>() // handled elsewhere
|
||||
}
|
||||
}
|
||||
|
||||
val DATE: Adapter<Date> = object : SimpleAdapter<Date>() {
|
||||
override fun serialize(t: Date?): String? {
|
||||
return ISODateTimeFormat.dateTime().print(DateTime(t))
|
||||
}
|
||||
|
||||
override fun apply(v: String?): Date? {
|
||||
return DateTime.parse(v).toDate()
|
||||
}
|
||||
}
|
||||
|
||||
val DATETIME: Adapter<DateTime> = object : SimpleAdapter<DateTime>() {
|
||||
override fun serialize(t: DateTime?): String? {
|
||||
return ISODateTimeFormat.dateTime().print(t)
|
||||
}
|
||||
|
||||
override fun apply(v: String?): DateTime? {
|
||||
return DateTime.parse(v)
|
||||
}
|
||||
}
|
||||
|
||||
val DURATION: Adapter<Duration> = object : SimpleAdapter<Duration>() {
|
||||
override fun apply(v: String?): Duration? {
|
||||
return Duration.parse(v)
|
||||
}
|
||||
}
|
||||
|
||||
val PERIOD: Adapter<Period> = object : SimpleAdapter<Period>() {
|
||||
override fun apply(v: String?): Period? {
|
||||
return Period.parse(v)
|
||||
}
|
||||
}
|
||||
|
||||
val INTERVAL: Adapter<Interval> = object : SimpleAdapter<Interval>() {
|
||||
override fun apply(v: String?): Interval? {
|
||||
return Interval.parse(v)
|
||||
}
|
||||
}
|
||||
|
||||
val MIMETYPE: Adapter<MediaType> = object : SimpleAdapter<MediaType>() {
|
||||
override fun apply(v: String?): MediaType? {
|
||||
return MediaType.parse(v)
|
||||
}
|
||||
}
|
||||
|
||||
val MULTIMAP: MultimapAdapter = MultimapAdapter()
|
||||
|
||||
|
||||
val RANGE: Adapter<Range<*>> = object : Adapter<Range<*>>() {
|
||||
override fun serialize(
|
||||
src: Range<*>,
|
||||
typeOfSrc: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
val el = JsonObject()
|
||||
el.add("lower", makeBound(src.lowerBoundType(), src.lowerEndpoint(), context))
|
||||
el.add("upper", makeBound(src.upperBoundType(), src.upperEndpoint(), context))
|
||||
return el
|
||||
}
|
||||
|
||||
private fun makeBound(
|
||||
type: BoundType,
|
||||
`val`: Any,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
val obj = JsonObject()
|
||||
obj.add("type", context.serialize(type.name.lowercase(Locale.getDefault())))
|
||||
obj.add("endpoint", context.serialize(`val`))
|
||||
return obj
|
||||
}
|
||||
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
typeOfT: Type,
|
||||
context: JsonDeserializationContext
|
||||
): Range<*> {
|
||||
Preconditions.checkArgument(json.isJsonObject)
|
||||
try {
|
||||
val obj = json.asJsonObject
|
||||
val upper = obj.getAsJsonObject("upper")
|
||||
val lower = obj.getAsJsonObject("lower")
|
||||
val ubt = bt(upper.getAsJsonPrimitive("type"))
|
||||
val lbt = bt(lower.getAsJsonPrimitive("type"))
|
||||
val ub = des(upper["endpoint"], context)
|
||||
val lb = des(lower["endpoint"], context)
|
||||
return Range.range(lb as Comparable<*>?, lbt, ub as Comparable<*>?, ubt)
|
||||
} catch (t: Throwable) {
|
||||
throw Throwables.propagate(t)
|
||||
}
|
||||
}
|
||||
|
||||
private fun des(`val`: JsonElement, context: JsonDeserializationContext): Any? {
|
||||
if (`val`.isJsonArray) return MultimapAdapter.Companion.arraydes(`val`.asJsonArray, context)
|
||||
else if (`val`.isJsonObject) return context.deserialize(`val`, ASObject::class.java)
|
||||
else if (`val`.isJsonPrimitive) {
|
||||
var v: Any? = ASObjectAdapter.Companion.primConverter.convert(`val`.asJsonPrimitive)
|
||||
if (v is LazilyParsedNumber) v = LazilyParsedNumberComparable(v)
|
||||
return v
|
||||
} else return null
|
||||
}
|
||||
|
||||
private fun bt(p: JsonPrimitive): BoundType {
|
||||
try {
|
||||
return BoundType.valueOf(p.toString())
|
||||
} catch (t: Throwable) {
|
||||
return BoundType.CLOSED
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val OPTIONAL: Adapter<Optional<*>> = object : Adapter<Optional<*>>() {
|
||||
override fun serialize(
|
||||
src: Optional<*>,
|
||||
typeOfSrc: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
return context.serialize(src.orNull())
|
||||
}
|
||||
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
typeOfT: Type,
|
||||
context: JsonDeserializationContext
|
||||
): Optional<*> {
|
||||
return Optional.absent<String>()
|
||||
}
|
||||
}
|
||||
|
||||
val TABLE: Adapter<Table<*, *, *>> = object : Adapter<Table<*, *, *>>() {
|
||||
override fun serialize(
|
||||
src: Table<*, *, *>,
|
||||
typeOfSrc: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
val obj = JsonObject()
|
||||
for (cell: Table.Cell<*, *, *> in src.cellSet()) {
|
||||
val r = cell.rowKey.toString()
|
||||
val c = cell.columnKey.toString()
|
||||
var rowobj: JsonObject? = null
|
||||
if (!obj.has(r)) {
|
||||
rowobj = JsonObject()
|
||||
obj.add(r, rowobj)
|
||||
} else {
|
||||
rowobj = obj.getAsJsonObject(r)
|
||||
}
|
||||
val `val` = cell.value
|
||||
if (`val` != null) rowobj!!.add(c, context.serialize(`val`, `val`.javaClass))
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
typeOfT: Type,
|
||||
context: JsonDeserializationContext
|
||||
): Table<*, *, *> {
|
||||
val table =
|
||||
ImmutableTable.builder<String, String, Any>()
|
||||
Preconditions.checkArgument(json.isJsonObject)
|
||||
val obj = json.asJsonObject
|
||||
for ((row, cell) in obj.entrySet()) {
|
||||
Preconditions.checkArgument(cell.isJsonObject)
|
||||
for (cellentry in cell.asJsonObject.entrySet()) {
|
||||
var desval: Any? = null
|
||||
if (`json`.isJsonArray) desval = MultimapAdapter.Companion.arraydes(`json`.asJsonArray, context)
|
||||
else if (`json`.isJsonObject) desval = context.deserialize<Any>(`json`, ASObject::class.java)
|
||||
else if (`json`.isJsonPrimitive) desval =
|
||||
ASObjectAdapter.Companion.primConverter.convert(`json`.asJsonPrimitive)
|
||||
if (desval != null) table.put(row, cellentry.key, desval)
|
||||
}
|
||||
}
|
||||
return table.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Converter
|
||||
import com.google.common.base.Enums
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.gson.*
|
||||
import com.ibm.common.activitystreams.util.Converters.toLowerConverter
|
||||
import com.ibm.common.activitystreams.util.Converters.toUpperConverter
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class EnumAdapter<E : Enum<E>>
|
||||
@JvmOverloads constructor(
|
||||
_enumClass: Class<E>?,
|
||||
c: Converter<String, E> = Enums.stringConverter(_enumClass)
|
||||
) :
|
||||
Adapter<E>() {
|
||||
private val des: Converter<String, E>
|
||||
private val ser: Converter<E, String>
|
||||
|
||||
/**
|
||||
* Constructor for EnumAdapter
|
||||
*
|
||||
*
|
||||
* @param _enumClass Class<E>
|
||||
* @param or E
|
||||
</E> */
|
||||
constructor(_enumClass: Class<E>, or: E) : this(_enumClass, Enums.stringConverter<E>(_enumClass))
|
||||
|
||||
/**
|
||||
* Constructor for EnumAdapter.
|
||||
* @param _enumClass Class<E>
|
||||
*
|
||||
* @param c Converter<String></String>,E>
|
||||
</E> */
|
||||
/**
|
||||
* Constructor for EnumAdapter.
|
||||
* @param _enumClass Class<E>
|
||||
</E> */
|
||||
init {
|
||||
this.des = toUpper.andThen(c)
|
||||
this.ser = c.reverse().andThen(toLower)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param src E
|
||||
* @param typeOfSrc Type
|
||||
* @param context JsonSerializationContext
|
||||
* @return JsonElement
|
||||
*/
|
||||
override fun serialize(
|
||||
src: E,
|
||||
typeOfSrc: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
return context.serialize(ser.convert(src))
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param json JsonElement
|
||||
* @param typeOfT Type
|
||||
* @param context JsonDeserializationContext
|
||||
* @return E
|
||||
* @throws JsonParseException
|
||||
* @see com.google.gson.JsonDeserializer.deserialize
|
||||
*/
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
typeOfT: Type,
|
||||
context: JsonDeserializationContext
|
||||
): E {
|
||||
Preconditions.checkArgument(json.isJsonPrimitive)
|
||||
val jp: JsonPrimitive = json.asJsonPrimitive
|
||||
Preconditions.checkArgument(jp.isString)
|
||||
return des.convert(jp.asString)!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method convert.
|
||||
* @param s String
|
||||
* @return E
|
||||
*/
|
||||
protected fun convert(s: String?): E? {
|
||||
return des.convert(s)
|
||||
}
|
||||
|
||||
companion object {
|
||||
protected val toLower: Converter<String, String> = toLowerConverter()
|
||||
protected val toUpper: Converter<String, String> = toUpperConverter()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,312 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Optional
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.base.Throwables
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.collect.Multimap
|
||||
import com.google.common.collect.Range
|
||||
import com.google.common.collect.Table
|
||||
import com.google.common.net.MediaType
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.internal.LazilyParsedNumber
|
||||
import com.google.gson.internal.bind.TypeAdapters
|
||||
import com.ibm.common.activitystreams.*
|
||||
import com.ibm.common.activitystreams.Collection
|
||||
import org.joda.time.DateTime
|
||||
import org.joda.time.ReadableDuration
|
||||
import org.joda.time.ReadableInterval
|
||||
import org.joda.time.ReadablePeriod
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
import kotlin.collections.Iterable
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class GsonWrapper protected constructor(builder: Builder) {
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: Supplier<GsonWrapper> {
|
||||
var charset: String = "UTF-8"
|
||||
var pretty: Boolean = false
|
||||
var schema: Schema? = null // default
|
||||
val adapters: ImmutableList.Builder<AdapterEntry<*>> = ImmutableList.builder()
|
||||
|
||||
/**
|
||||
* Method charset.
|
||||
* @param charset String
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun charset(charset: String): Builder {
|
||||
this.charset = charset
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param schema Schema
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun schema(schema: Schema?): Builder {
|
||||
this.schema = schema
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param type Class
|
||||
* @param adapter Adapter<T>
|
||||
*
|
||||
* @return Builder
|
||||
</T> */
|
||||
fun <T> adapter(
|
||||
type: Class<out T>?,
|
||||
adapter: Adapter<T>?
|
||||
): Builder {
|
||||
return adapter(type, adapter, false)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param type Class
|
||||
* @param adapter Adapter<T>
|
||||
* @param hier boolean
|
||||
*
|
||||
* @return Builder
|
||||
</T> */
|
||||
fun <T> adapter(
|
||||
type: Class<out T>?,
|
||||
adapter: Adapter<T>?,
|
||||
hier: Boolean
|
||||
): Builder {
|
||||
adapters.add(AdapterEntry(type, adapter, hier))
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method prettyPrint.
|
||||
* @param on boolean
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
/**
|
||||
* Method prettyPrint.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun prettyPrint(on: Boolean = true): Builder {
|
||||
this.pretty = on
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
*
|
||||
*
|
||||
* @return GsonWrapper * @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
override fun get(): GsonWrapper {
|
||||
return GsonWrapper(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class AdapterEntry<T>
|
||||
/**
|
||||
* Constructor for AdapterEntry.
|
||||
* @param type Class
|
||||
* @param adapter Adapter<T>
|
||||
* @param hier boolean
|
||||
</T> */(
|
||||
val type: Class<out T>?,
|
||||
val adapter: Adapter<T>?,
|
||||
val hier: Boolean
|
||||
)
|
||||
|
||||
private val gson: Gson
|
||||
private val charset: String
|
||||
|
||||
/**
|
||||
* Constructor for GsonWrapper.
|
||||
* @param builder Builder
|
||||
*/
|
||||
init {
|
||||
val schema: Schema =
|
||||
if (builder.schema != null) builder.schema!! else Schema.Companion.make().get()
|
||||
val base: ASObjectAdapter =
|
||||
ASObjectAdapter(schema)
|
||||
val b: GsonBuilder = initGsonBuilder(
|
||||
builder,
|
||||
schema,
|
||||
base,
|
||||
builder.adapters.build()
|
||||
)
|
||||
if (builder.pretty) b.setPrettyPrinting()
|
||||
this.gson = b.create()
|
||||
this.charset = builder.charset
|
||||
}
|
||||
|
||||
/**
|
||||
* Method write.
|
||||
* @param w Writable
|
||||
* @param out OutputStream
|
||||
*/
|
||||
fun write(w: Writable?, out: OutputStream) {
|
||||
try {
|
||||
val wout: OutputStreamWriter =
|
||||
OutputStreamWriter(out, charset)
|
||||
gson.toJson(w, wout)
|
||||
wout.flush()
|
||||
} catch (t: Throwable) {
|
||||
throw Throwables.propagate(t)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method write.
|
||||
* @param w Writable
|
||||
* @param out Writer
|
||||
*/
|
||||
fun write(w: Writable?, out: Writer?) {
|
||||
gson.toJson(w, out)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method write.
|
||||
* @param w Writable
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
fun write(w: Writable?): String {
|
||||
val sw: StringWriter =
|
||||
StringWriter()
|
||||
write(w, sw)
|
||||
return sw.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method readAs.
|
||||
* @param in InputStream
|
||||
* @param type Class
|
||||
*
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> readAs(`in`: InputStream, type: Class<out A>?): A {
|
||||
try {
|
||||
return readAs(InputStreamReader(`in`, charset), type)
|
||||
} catch (t: Throwable) {
|
||||
throw Throwables.propagate(t)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method readAs.
|
||||
* @param in Reader
|
||||
* @param type Class
|
||||
*
|
||||
* @return A
|
||||
*/
|
||||
fun <A : ASObject?> readAs(`in`: Reader, type: Class<out A>?): A {
|
||||
return gson.fromJson(`in`, type) as A
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method make.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method initGsonBuilder.
|
||||
* @param builder Builder
|
||||
*
|
||||
* @return GsonBuilder
|
||||
*/
|
||||
private fun initGsonBuilder(
|
||||
builder: Builder,
|
||||
schema: Schema,
|
||||
base: ASObjectAdapter,
|
||||
adapters: Iterable<AdapterEntry<*>>
|
||||
): GsonBuilder {
|
||||
val gson: GsonBuilder = GsonBuilder()
|
||||
.registerTypeHierarchyAdapter(TypeValue::class.java, TypeValueAdapter(schema))
|
||||
.registerTypeHierarchyAdapter(LinkValue::class.java, LinkValueAdapter(schema))
|
||||
.registerTypeHierarchyAdapter(Iterable::class.java, Adapters.ITERABLE)
|
||||
.registerTypeHierarchyAdapter(ASObject::class.java, base)
|
||||
.registerTypeHierarchyAdapter(Collection::class.java, base)
|
||||
.registerTypeHierarchyAdapter(Activity::class.java, base)
|
||||
.registerTypeHierarchyAdapter(NLV::class.java, Adapters.NLV)
|
||||
.registerTypeHierarchyAdapter(ActionsValue::class.java, Adapters.ACTIONS)
|
||||
.registerTypeHierarchyAdapter(Optional::class.java, Adapters.OPTIONAL)
|
||||
.registerTypeHierarchyAdapter(Range::class.java, Adapters.RANGE)
|
||||
.registerTypeHierarchyAdapter(Table::class.java, Adapters.TABLE)
|
||||
.registerTypeHierarchyAdapter(LazilyParsedNumber::class.java, TypeAdapters.NUMBER)
|
||||
.registerTypeHierarchyAdapter(LazilyParsedNumberComparable::class.java, TypeAdapters.NUMBER)
|
||||
.registerTypeHierarchyAdapter(ReadableDuration::class.java, Adapters.DURATION)
|
||||
.registerTypeHierarchyAdapter(ReadablePeriod::class.java, Adapters.PERIOD)
|
||||
.registerTypeHierarchyAdapter(ReadableInterval::class.java, Adapters.INTERVAL)
|
||||
.registerTypeAdapter(
|
||||
Activity.Status::class.java,
|
||||
Adapters.forEnum(
|
||||
Activity.Status::class.java,
|
||||
Activity.Status.OTHER
|
||||
)
|
||||
)
|
||||
.registerTypeAdapter(Date::class.java, Adapters.DATE)
|
||||
.registerTypeAdapter(DateTime::class.java, Adapters.DATETIME)
|
||||
.registerTypeAdapter(MediaType::class.java, Adapters.MIMETYPE)
|
||||
.registerTypeHierarchyAdapter(Multimap::class.java, Adapters.MULTIMAP)
|
||||
|
||||
for (entry: AdapterEntry<*> in adapters) {
|
||||
if (entry.hier) gson.registerTypeHierarchyAdapter(
|
||||
entry.type,
|
||||
if (entry.adapter != null) entry.adapter else base
|
||||
)
|
||||
else gson.registerTypeAdapter(
|
||||
entry.type,
|
||||
if (entry.adapter != null) entry.adapter else base
|
||||
)
|
||||
}
|
||||
|
||||
return gson
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,131 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.primitives.Longs
|
||||
import com.google.gson.internal.LazilyParsedNumber
|
||||
import java.io.ObjectStreamException
|
||||
|
||||
/**
|
||||
*/
|
||||
internal class LazilyParsedNumberComparable
|
||||
|
||||
/**
|
||||
* Constructor for LazilyParsedNumberComparable.
|
||||
* @param inner LazilyParsedNumber
|
||||
*/(private val inner: LazilyParsedNumber) : Number(), Comparable<Number> {
|
||||
/**
|
||||
* Method compareTo.
|
||||
* @param o Number
|
||||
* @return int
|
||||
*/
|
||||
override fun compareTo(o: Number): Int {
|
||||
return Longs.compare(inner.toLong(), o.toLong())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doubleValue.
|
||||
* @return double
|
||||
*/
|
||||
override fun toDouble(): Double {
|
||||
return inner.toDouble()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method floatValue.
|
||||
* @return float
|
||||
*/
|
||||
override fun toFloat(): Float {
|
||||
return inner.toFloat()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method intValue.
|
||||
* @return int
|
||||
*/
|
||||
override fun toInt(): Int {
|
||||
return inner.toInt()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method longValue.
|
||||
* @return long
|
||||
*/
|
||||
override fun toLong(): Long {
|
||||
return inner.toLong()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method byteValue.
|
||||
* @return byte
|
||||
*/
|
||||
override fun toByte(): Byte {
|
||||
return inner.toByte()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method shortValue.
|
||||
* @return short
|
||||
*/
|
||||
override fun toShort(): Short {
|
||||
return inner.toShort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method equals.
|
||||
* @param obj Object
|
||||
* @return boolean
|
||||
*/
|
||||
override fun equals(obj: Any?): Boolean {
|
||||
return inner == obj
|
||||
}
|
||||
|
||||
/**
|
||||
* Method hashCode.
|
||||
* @return int
|
||||
*/
|
||||
override fun hashCode(): Int {
|
||||
return inner.hashCode()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toString.
|
||||
* @return String
|
||||
*/
|
||||
override fun toString(): String {
|
||||
return inner.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method writeReplace.
|
||||
* @return Object
|
||||
* @throws ObjectStreamException
|
||||
*/
|
||||
@Throws(ObjectStreamException::class)
|
||||
private fun writeReplace(): Any {
|
||||
return inner
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 761729254455440231L
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,125 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.gson.*
|
||||
import com.ibm.common.activitystreams.*
|
||||
import com.ibm.common.activitystreams.LinkValue.SimpleLinkValue
|
||||
import com.ibm.common.activitystreams.Makers.linkValue
|
||||
import com.ibm.common.activitystreams.Makers.linkValues
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
internal class LinkValueAdapter
|
||||
|
||||
/**
|
||||
* Constructor for LinkValueAdapter.
|
||||
* @param schema Schema
|
||||
*/(private val schema: Schema) : Adapter<LinkValue>() {
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param value LinkValue
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
*
|
||||
* @return JsonElement
|
||||
*/
|
||||
override fun serialize(
|
||||
value: LinkValue,
|
||||
type: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
when (value.valueType()) {
|
||||
ValueType.SIMPLE -> {
|
||||
val simple: SimpleLinkValue = value as SimpleLinkValue
|
||||
return context.serialize(simple.url(), String::class.java)
|
||||
}
|
||||
|
||||
ValueType.ARRAY -> return context.serialize(value, Iterable::class.java)
|
||||
ValueType.OBJECT -> return context.serialize(value, ASObject::class.java)
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param el JsonElement
|
||||
* @param type Type
|
||||
* @param context JsonDeserializationContext
|
||||
*
|
||||
*
|
||||
*
|
||||
* @return LinkValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
|
||||
*/
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
el: JsonElement,
|
||||
type: Type,
|
||||
context: JsonDeserializationContext
|
||||
): LinkValue {
|
||||
Preconditions.checkArgument(
|
||||
el.isJsonArray() ||
|
||||
el.isJsonObject() ||
|
||||
el.isJsonPrimitive()
|
||||
)
|
||||
if (el.isJsonArray()) {
|
||||
val builder: LinkValue.ArrayLinkValue.Builder =
|
||||
linkValues()
|
||||
for (aryel: JsonElement? in el.getAsJsonArray()) builder.add(
|
||||
context.deserialize<LinkValue>(
|
||||
aryel,
|
||||
LinkValue::class.java
|
||||
)
|
||||
)
|
||||
return builder.get()
|
||||
} else if (el.isJsonObject()) {
|
||||
val obj: JsonObject = el.getAsJsonObject()
|
||||
if (obj.has("objectType")) {
|
||||
val tv: TypeValue =
|
||||
context.deserialize(
|
||||
obj.get("objectType"),
|
||||
TypeValue::class.java
|
||||
)
|
||||
val pMap: Model? =
|
||||
schema.forObjectType(tv.id())
|
||||
return context.deserialize(
|
||||
el,
|
||||
if (pMap != null && pMap.type() != null) pMap.type() else ASObject::class.java
|
||||
)
|
||||
} else {
|
||||
return context.deserialize(
|
||||
el,
|
||||
ASObject::class.java
|
||||
)
|
||||
}
|
||||
} else {
|
||||
val prim: JsonPrimitive =
|
||||
el.getAsJsonPrimitive()
|
||||
Preconditions.checkArgument(prim.isString())
|
||||
return linkValue(prim.getAsString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,521 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Objects
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import com.google.common.collect.Maps
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import com.ibm.common.activitystreams.LinkValue
|
||||
import com.ibm.common.activitystreams.NLV
|
||||
import com.ibm.common.activitystreams.TypeValue
|
||||
import org.joda.time.DateTime
|
||||
import org.joda.time.ReadableDuration
|
||||
import org.joda.time.ReadableInterval
|
||||
import org.joda.time.ReadablePeriod
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Model internal constructor(builder: Builder) {
|
||||
val parent: String?
|
||||
val properties: ImmutableMap<String, Type>
|
||||
val _default: Type?
|
||||
val _type: Type?
|
||||
val _builder: Type?
|
||||
|
||||
@Transient
|
||||
private var schema: Schema? = null
|
||||
|
||||
/**
|
||||
* Method schema.
|
||||
* @param schema Schema
|
||||
*/
|
||||
fun schema(schema: Schema?) {
|
||||
this.schema = schema
|
||||
}
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: Supplier<Model> {
|
||||
var _type: Type? = null
|
||||
var _builder: Type? = null
|
||||
var _default: Type? = null
|
||||
var parent: String? = null
|
||||
|
||||
val properties: MutableMap<String, Type> = Maps.newHashMap()
|
||||
|
||||
internal constructor()
|
||||
|
||||
/**
|
||||
* Constructor for Builder.
|
||||
* @param template PropertyMap
|
||||
*/
|
||||
internal constructor(template: Model) {
|
||||
this.parent = template.parent
|
||||
this._default = template._default
|
||||
this._type = template._type
|
||||
this._builder = template._builder
|
||||
properties.putAll(template.properties)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method withDefault.
|
||||
* @param _default Class
|
||||
* @return Builder
|
||||
*/
|
||||
fun withDefault(_default: Class<*>?): Builder {
|
||||
this._default = _default
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
* @param _type Class
|
||||
* @param _builder Class
|
||||
* @return Builder
|
||||
*/
|
||||
fun type(
|
||||
_type: Class<*>?, _builder: Class<*>?
|
||||
): Builder {
|
||||
this._type = _type as Class<out ASObject?>?
|
||||
this._builder = _builder as Class<out ASObject.AbstractBuilder<*, *>?>?
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method parent.
|
||||
* @param parent String
|
||||
* @return Builder
|
||||
*/
|
||||
fun parent(parent: String?): Builder {
|
||||
this.parent = parent
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method naturalLanguageValue.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun naturalLanguageValue(name: String): Builder {
|
||||
return `as`(name, NLV::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method naturalLanguageValue.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun naturalLanguageValue(vararg names: String): Builder {
|
||||
for (name: String in names) naturalLanguageValue(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method object.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun `object`(name: String): Builder {
|
||||
return `as`(name, ASObject::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method object.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun `object`(vararg names: String): Builder {
|
||||
for (name: String in names) `object`(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method integer.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun integer(name: String): Builder {
|
||||
return `as`(name, Int::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method integer.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun integer(vararg names: String): Builder {
|
||||
for (name: String in names) integer(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doub.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun doub(name: String): Builder {
|
||||
return `as`(name, Double::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doub.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun doub(vararg names: String): Builder {
|
||||
for (name: String in names) doub(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doub.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun floatValue(name: String): Builder {
|
||||
return `as`(name, Float::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method doub.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun floatValue(vararg names: String): Builder {
|
||||
for (name: String in names) floatValue(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method string.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun string(name: String): Builder {
|
||||
return `as`(name, String::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method string.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun string(vararg names: String): Builder {
|
||||
for (name: String in names) string(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method linkValue.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun linkValue(name: String): Builder {
|
||||
return `as`(name, LinkValue::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method linkValue.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun linkValue(vararg names: String): Builder {
|
||||
for (name: String in names) linkValue(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method dateTime.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun dateTime(name: String): Builder {
|
||||
return `as`(name, DateTime::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method dateTime.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun dateTime(vararg names: String): Builder {
|
||||
for (name: String in names) dateTime(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method duration.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun duration(name: String): Builder {
|
||||
return `as`(name, ReadableDuration::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method duration.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun duration(vararg names: String): Builder {
|
||||
for (name: String in names) duration(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method period.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun period(name: String): Builder {
|
||||
return `as`(name, ReadablePeriod::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method period.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun period(vararg names: String): Builder {
|
||||
for (name: String in names) period(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method interval.
|
||||
* @param name String
|
||||
* @return Builder
|
||||
*/
|
||||
fun interval(name: String): Builder {
|
||||
return `as`(name, ReadableInterval::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method interval.
|
||||
* @param names String[]
|
||||
* @return Builder
|
||||
*/
|
||||
fun interval(vararg names: String): Builder {
|
||||
for (name: String in names) interval(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method typeValue.
|
||||
* @param name String
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun typeValue(name: String): Builder {
|
||||
return `as`(name, TypeValue::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method typeValue.
|
||||
* @param names String[]
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun typeValue(vararg names: String): Builder {
|
||||
for (name: String in names) typeValue(name)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method as.
|
||||
* @param name String
|
||||
* @param _class Class
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun `as`(name: String, _class: Class<*>): Builder {
|
||||
properties.put(name, _class)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
*
|
||||
*
|
||||
* @return PropertyMap * @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
override fun get(): Model {
|
||||
return Model(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for PropertyMap.
|
||||
* @param builder Builder
|
||||
*/
|
||||
init {
|
||||
this.parent = builder.parent
|
||||
this.properties = ImmutableMap.copyOf(builder.properties)
|
||||
this._default = builder._default
|
||||
this._type = builder._type
|
||||
this._builder = builder._builder
|
||||
}
|
||||
|
||||
/**
|
||||
* Method parentPropertyMap.
|
||||
*
|
||||
* @return PropertyMap
|
||||
*/
|
||||
protected fun parentPropertyMap(): Model? {
|
||||
if (schema == null) return null
|
||||
if (parent == null) return null
|
||||
return schema!!.forObjectClassOrType(
|
||||
ASObject.Builder::class.java,
|
||||
if (parent == "object") null else parent
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @param name String
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
fun <T : Type?> get(name: String?): T? {
|
||||
val parent: Model? = parentPropertyMap()
|
||||
return (if (properties.containsKey(name)) properties.get(name) else if (parent != null && _default == null) parent.get<Type>(
|
||||
name
|
||||
) else _default) as T?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method containsKey.
|
||||
* @param name String
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
fun has(name: String?): Boolean {
|
||||
val parent: Model? = parentPropertyMap()
|
||||
return if (properties.containsKey(name)) true else if (parent != null) parent.has(name) else false
|
||||
}
|
||||
|
||||
/**
|
||||
* Method type.
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
fun type(): Class<out ASObject>? {
|
||||
return _type as Class<out ASObject>?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method builder.
|
||||
*
|
||||
* @return Class>
|
||||
*/
|
||||
fun builder(): Class<out ASObject.AbstractBuilder<*, *>?>? {
|
||||
return _builder as Class<out ASObject.AbstractBuilder<*, *>?>?
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toString.
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
override fun toString(): String {
|
||||
return Objects.toStringHelper(Model::class.java)
|
||||
.omitNullValues()
|
||||
.add("Parent", parent)
|
||||
.add("Properties", properties)
|
||||
.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method template.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun template(): Builder {
|
||||
return Builder(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method template.
|
||||
* @param _type Class
|
||||
* @param _builder Class
|
||||
*
|
||||
* @return PropertyMap
|
||||
*/
|
||||
fun <B : ASObject?, X : ASObject.AbstractBuilder<B, X>?> template(
|
||||
_type: Class<out B>?, _builder: Class<out X>?
|
||||
): Model {
|
||||
return template().type(_type, _builder).get()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method set.
|
||||
* @param name String
|
||||
* @param type Class
|
||||
*
|
||||
* @return PropertyMap
|
||||
*/
|
||||
fun set(name: String, type: Class<*>): Model {
|
||||
return template()
|
||||
.`as`(name, type)
|
||||
.get()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method set.
|
||||
* @param map Map<String></String>,Class>
|
||||
*
|
||||
* @return PropertyMap
|
||||
*/
|
||||
fun set(map: Map<String, Class<*>>): Model {
|
||||
val builder: Builder = template()
|
||||
for (entry: Map.Entry<String, Class<*>> in map.entries) builder.`as`(entry.key, entry.value)
|
||||
return builder.get()
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method make.
|
||||
* @param parent String
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
/**
|
||||
* Method make.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun make(parent: String? = null): Builder {
|
||||
return Builder().parent(parent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.collect.ImmutableList
|
||||
import com.google.common.collect.ImmutableMultimap
|
||||
import com.google.common.collect.Iterables
|
||||
import com.google.common.collect.Multimap
|
||||
import com.google.gson.*
|
||||
import com.ibm.common.activitystreams.ASObject
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
internal class MultimapAdapter
|
||||
|
||||
: Adapter<Multimap<*, *>>() {
|
||||
val adaptedClass: Class<Multimap<*, *>>
|
||||
/**
|
||||
* Method getAdaptedClass.
|
||||
*
|
||||
* @return Class<Multimap>
|
||||
</Multimap> */
|
||||
get() {
|
||||
return Multimap::class.java
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param src Multimap
|
||||
* @param typeOfSrc Type
|
||||
* @param context JsonSerializationContext
|
||||
*
|
||||
* @return JsonElement
|
||||
*/
|
||||
override fun serialize(
|
||||
src: Multimap<*, *>,
|
||||
typeOfSrc: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
val obj: JsonObject = JsonObject()
|
||||
for (key: Any in src.keySet()) {
|
||||
val vals: Iterable<Any> = (src as Multimap<Any, Any>).get(key)
|
||||
if (Iterables.size(vals) == 1) {
|
||||
val f: Any? = Iterables.getFirst(vals, null)
|
||||
if (f != null) obj.add(key.toString(), context.serialize(f, f.javaClass))
|
||||
} else {
|
||||
obj.add(key.toString(), context.serialize(vals, Iterable::class.java))
|
||||
}
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param json JsonElement
|
||||
* @param typeOfT Type
|
||||
* @param context JsonDeserializationContext
|
||||
*
|
||||
*
|
||||
*
|
||||
* @return Multimap * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
|
||||
*/
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
typeOfT: Type,
|
||||
context: JsonDeserializationContext
|
||||
): Multimap<*, *> {
|
||||
val mm =
|
||||
ImmutableMultimap.builder<Any, Any>()
|
||||
val obj: JsonObject = json.asJsonObject
|
||||
for (entry: Map.Entry<String, JsonElement> in obj.entrySet()) {
|
||||
val key: String = entry.key
|
||||
val `val`: JsonElement = entry.value
|
||||
if (`val`.isJsonArray) {
|
||||
for (el: JsonElement in `val`.asJsonArray) {
|
||||
if (el.isJsonArray) mm.put(key, arraydes(el.asJsonArray, context))
|
||||
else if (el.isJsonObject) mm.put(key, context.deserialize<Any>(el, ASObject::class.java))
|
||||
else if (el.isJsonPrimitive) mm.put(
|
||||
key,
|
||||
ASObjectAdapter.Companion.primConverter.convert(el.asJsonPrimitive)
|
||||
)
|
||||
}
|
||||
} else if (`val`.isJsonObject) {
|
||||
mm.put(key, context.deserialize<Any>(`val`, ASObject::class.java))
|
||||
} else if (`val`.isJsonPrimitive) {
|
||||
mm.put(key, ASObjectAdapter.Companion.primConverter.convert(`val`.asJsonPrimitive))
|
||||
}
|
||||
}
|
||||
return mm.build()
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Method arraydes.
|
||||
* @param array JsonArray
|
||||
* @param context JsonDeserializationContext
|
||||
*
|
||||
* @return ImmutableList<Object>
|
||||
</Object> */
|
||||
fun arraydes(
|
||||
array: JsonArray,
|
||||
context: JsonDeserializationContext
|
||||
): ImmutableList<Any> {
|
||||
val builder: ImmutableList.Builder<Any> =
|
||||
ImmutableList.builder()
|
||||
for (child: JsonElement in array) if (child.isJsonArray) builder.add(
|
||||
arraydes(
|
||||
child.asJsonArray,
|
||||
context
|
||||
)
|
||||
)
|
||||
else if (child.isJsonObject) builder.add(*context.deserialize(child, ASObject::class.java))
|
||||
else if (child.isJsonPrimitive) builder.add(ASObjectAdapter.Companion.primConverter.convert(child.asJsonPrimitive))
|
||||
return builder.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,114 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.gson.*
|
||||
import com.ibm.common.activitystreams.*
|
||||
import com.ibm.common.activitystreams.NLV.MapNLV
|
||||
import com.ibm.common.activitystreams.NLV.SimpleNLV
|
||||
import com.ibm.common.activitystreams.NLV.SimpleNLV.Companion.make
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
internal class NaturalLanguageValueAdapter
|
||||
|
||||
: Adapter<NLV>() {
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param nlv NLV
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
*
|
||||
* @return JsonElement
|
||||
*/
|
||||
override fun serialize(
|
||||
nlv: NLV,
|
||||
type: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement? {
|
||||
var el: JsonElement? = null
|
||||
when (nlv.valueType()) {
|
||||
ValueType.SIMPLE -> el = context.serialize((nlv as SimpleNLV).value())
|
||||
ValueType.OBJECT -> {
|
||||
val map: MapNLV =
|
||||
nlv as MapNLV
|
||||
val obj: JsonObject = JsonObject()
|
||||
for (lang: String in map) obj.addProperty(
|
||||
lang.toString(),
|
||||
map.value(lang)
|
||||
)
|
||||
el = obj
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
return el
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param element JsonElement
|
||||
* @param type1 Type
|
||||
* @param context JsonDeserializationContext
|
||||
*
|
||||
*
|
||||
*
|
||||
* @return NLV * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
|
||||
*/
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
element: JsonElement,
|
||||
type1: Type,
|
||||
context: JsonDeserializationContext
|
||||
): NLV {
|
||||
Preconditions.checkArgument(
|
||||
element.isJsonPrimitive() ||
|
||||
element.isJsonObject()
|
||||
)
|
||||
if (element.isJsonPrimitive()) {
|
||||
val prim: JsonPrimitive =
|
||||
element.getAsJsonPrimitive()
|
||||
Preconditions.checkArgument(prim.isString())
|
||||
return make(
|
||||
prim.getAsString()
|
||||
)
|
||||
} else {
|
||||
try {
|
||||
val obj: JsonObject =
|
||||
element.getAsJsonObject()
|
||||
val builder: MapNLV.Builder =
|
||||
MapNLV.make()
|
||||
for (entry: Map.Entry<String?, JsonElement> in obj.entrySet()) builder.set(
|
||||
entry.key,
|
||||
entry.value.getAsString()
|
||||
)
|
||||
return builder.get()
|
||||
} catch (t: Throwable) {
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,447 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Converter
|
||||
import com.google.common.base.Objects
|
||||
import com.google.common.base.Preconditions
|
||||
import com.google.common.base.Supplier
|
||||
import com.google.common.collect.*
|
||||
import com.google.common.net.MediaType
|
||||
import com.ibm.common.activitystreams.*
|
||||
import com.ibm.common.activitystreams.Collection
|
||||
import org.joda.time.Duration
|
||||
import kotlin.collections.Iterable
|
||||
import kotlin.collections.MutableMap
|
||||
import kotlin.collections.MutableSet
|
||||
import kotlin.collections.get
|
||||
|
||||
/**
|
||||
* @TODO: The Schema mechanism needs to be revisited and reworked
|
||||
* to be much more efficient.
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Schema internal constructor(builder: Builder) {
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
class Builder
|
||||
|
||||
: Supplier<Schema> {
|
||||
val objectTypeMap: MutableMap<String, Model> = Maps.newHashMap()
|
||||
val builderMap: MutableMap<String, Class<out ASObject.AbstractBuilder<*, *>?>> = Maps.newHashMap()
|
||||
val objectClassMap: MutableMap<Class<out ASObject.AbstractBuilder<*, *>?>, Model> = Maps.newHashMap()
|
||||
val classMap: BiMap<Class<out ASObject?>, Class<out ASObject.AbstractBuilder<*, *>?>> = HashBiMap.create(100)
|
||||
val adapters: MutableSet<Adapter<*>> = Sets.newHashSet()
|
||||
|
||||
internal constructor()
|
||||
|
||||
/**
|
||||
* Constructor for Builder.
|
||||
* @param template Schema
|
||||
*/
|
||||
internal constructor(template: Schema) {
|
||||
objectTypeMap.putAll(template.objectTypeMap)
|
||||
builderMap.putAll(template.builderMap)
|
||||
objectClassMap.putAll(template.objectClassMap)
|
||||
classMap.putAll(template.classMap)
|
||||
adapters.addAll(template.adapters)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param _enumClass Class<E>
|
||||
*
|
||||
* @return Builder
|
||||
</E> */
|
||||
fun <E : Enum<E>> adapter(
|
||||
_enumClass: Class<E>
|
||||
): Builder {
|
||||
return adapter(EnumAdapter(_enumClass))
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param _enumClass Class<E>
|
||||
*
|
||||
*
|
||||
* @param c Converter<String></String>,E>
|
||||
* @return Builder
|
||||
</E> */
|
||||
fun <E : Enum<E>> adapter(
|
||||
_enumClass: Class<E>,
|
||||
c: Converter<String, E>
|
||||
): Builder {
|
||||
return adapter(EnumAdapter(_enumClass, c))
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter
|
||||
* @param _enumClass Class<E>
|
||||
* @param or E
|
||||
* @return Builder
|
||||
</E> */
|
||||
fun <E : Enum<E>> adapter(
|
||||
_enumClass: Class<E>, or: E
|
||||
): Builder {
|
||||
return adapter(EnumAdapter(_enumClass, or))
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param adapter Adapter
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun adapter(adapter: Adapter<*>): Builder {
|
||||
adapters.add(adapter)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param adapters Adapter[]
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun adapter(vararg adapters: Adapter<*>): Builder {
|
||||
if (adapters == null) return this
|
||||
for (a: Adapter<*> in adapters) adapter(a)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method adapter.
|
||||
* @param adapters Iterable<Adapter></Adapter>>
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun adapter(adapters: Iterable<Adapter<*>>?): Builder {
|
||||
if (adapters == null) return this
|
||||
Iterables.addAll(this.adapters, adapters)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Method map.
|
||||
* @param objectType String
|
||||
* @param propertyMap Supplier<PropertyMap>
|
||||
*
|
||||
* @return Builder
|
||||
</PropertyMap> */
|
||||
fun map(objectType: String?, propertyMap: Supplier<Model>): Builder {
|
||||
return map(objectType, propertyMap.get())
|
||||
}
|
||||
|
||||
/**
|
||||
* Method map.
|
||||
* @param propertyMap Supplier<PropertyMap>
|
||||
*
|
||||
* @return Builder
|
||||
</PropertyMap> */
|
||||
fun map(propertyMap: Supplier<Model>): Builder {
|
||||
return map(null, propertyMap)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method map.
|
||||
* @param propertyMap PropertyMap
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun map(propertyMap: Model): Builder {
|
||||
return map(null, propertyMap)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method map.
|
||||
* @param objectType String
|
||||
* @param propertyMap PropertyMap
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun map(objectType: String?, propertyMap: Model): Builder {
|
||||
Preconditions.checkNotNull(propertyMap)
|
||||
val _builder: Class<out ASObject.AbstractBuilder<*, *>?>? = propertyMap.builder()
|
||||
val _type: Class<out ASObject?>? = propertyMap.type()
|
||||
if (objectType != null) objectTypeMap.put(objectType, propertyMap)
|
||||
if (objectType != null && _builder != null) builderMap.put(objectType, _builder)
|
||||
if (_builder != null) objectClassMap.put(_builder, propertyMap)
|
||||
if (_builder != null && _type != null) classMap.put(_type, _builder)
|
||||
return this
|
||||
}
|
||||
|
||||
fun model(objectType: String): Model? {
|
||||
return objectTypeMap.get(objectType)
|
||||
}
|
||||
|
||||
fun model(): Model? {
|
||||
return objectClassMap.get(ASObject.Builder::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get.
|
||||
* @return Schema * @see com.google.common.base.Supplier#get()
|
||||
*/
|
||||
override fun get(): Schema {
|
||||
return Schema(this)
|
||||
}
|
||||
}
|
||||
|
||||
val objectTypeMap: ImmutableMap<String, Model>
|
||||
val objectClassMap: ImmutableMap<Class<out ASObject.AbstractBuilder<*, *>?>, Model>
|
||||
val builderMap: ImmutableMap<String, Class<out ASObject.AbstractBuilder<*, *>?>>
|
||||
val classMap: ImmutableBiMap<Class<out ASObject?>, Class<out ASObject.AbstractBuilder<*, *>?>>
|
||||
val adapters: ImmutableSet<Adapter<*>>
|
||||
|
||||
/**
|
||||
* Method adapters.
|
||||
*
|
||||
* @return Iterable<Adapter></Adapter>>
|
||||
*/
|
||||
fun adapters(): Iterable<Adapter<*>> {
|
||||
return adapters
|
||||
}
|
||||
|
||||
/**
|
||||
* Method builderForObjectTypeOrClass.
|
||||
* @param ots String
|
||||
* @param _class Class
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
fun builderForObjectTypeOrClass(ots: String?, _class: Class<*>?): Class<out ASObject.AbstractBuilder<*, *>?>? {
|
||||
val _builder: Class<out ASObject.AbstractBuilder<*, *>?>? = builderFor(ots)
|
||||
return if (_builder != null) _builder else builderFor(_builder as Class<*>)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method builderFor.
|
||||
* @param _class Class
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
fun builderFor(_class: Class<*>?): Class<out ASObject.AbstractBuilder<*, *>?>? {
|
||||
if (_class == null) return null
|
||||
return classMap.get(_class)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method classFor.
|
||||
* @param _builder Class
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
fun classFor(_builder: Class<out ASObject.AbstractBuilder<*, *>?>?): Class<out ASObject?>? {
|
||||
if (_builder == null) return null
|
||||
return classMap.inverse().get(_builder)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method builderFor.
|
||||
* @param ots String
|
||||
*
|
||||
* @return Class
|
||||
*/
|
||||
fun builderFor(ots: String?): Class<out ASObject.AbstractBuilder<*, *>?>? {
|
||||
if (ots == null) return null
|
||||
return builderMap.get(ots)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method forObjectType.
|
||||
* @param objectType String
|
||||
*
|
||||
* @return PropertyMap
|
||||
*/
|
||||
fun forObjectType(objectType: String?): Model? {
|
||||
return objectTypeMap.get(objectType)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method forObjectClass.
|
||||
* @param _class Class
|
||||
*
|
||||
* @return PropertyMap
|
||||
*/
|
||||
fun forObjectClass(_class: Class<out ASObject.AbstractBuilder<*, *>?>): Model? {
|
||||
return objectClassMap.get(_class)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method forObjectClassOrType.
|
||||
* @param _class Class
|
||||
* @param objectType String
|
||||
*
|
||||
* @return PropertyMap
|
||||
*/
|
||||
fun forObjectClassOrType(_class: Class<out ASObject.AbstractBuilder<*, *>?>, objectType: String?): Model {
|
||||
val pm: Model? = forObjectClass(_class)
|
||||
return if (pm != null) pm else forObjectType(objectType)!!
|
||||
}
|
||||
|
||||
/**
|
||||
* Method template.
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
fun template(): Builder {
|
||||
return Builder(this)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method toString.
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
override fun toString(): String {
|
||||
return Objects.toStringHelper(Schema::class.java)
|
||||
.add("Object Types", objectTypeMap)
|
||||
.add("Object Classes", objectClassMap)
|
||||
.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for Schema.
|
||||
* @param builder Builder
|
||||
*/
|
||||
init {
|
||||
this.objectClassMap = ImmutableMap.copyOf(builder.objectClassMap)
|
||||
this.objectTypeMap = ImmutableMap.copyOf(builder.objectTypeMap)
|
||||
this.builderMap = ImmutableMap.copyOf(builder.builderMap)
|
||||
this.adapters = ImmutableSet.copyOf(builder.adapters)
|
||||
this.classMap = ImmutableBiMap.copyOf(builder.classMap)
|
||||
|
||||
for (pmap: Model in objectTypeMap.values) pmap.schema(this)
|
||||
for (pmap: Model in objectClassMap.values) pmap.schema(this)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val `object`: Model = Model.make()
|
||||
.type(
|
||||
ASObject::class.java,
|
||||
ASObject.Builder::class.java
|
||||
)
|
||||
.typeValue(
|
||||
"objectType"
|
||||
)
|
||||
.linkValue(
|
||||
"attachments",
|
||||
"author",
|
||||
"duplicates",
|
||||
"icon",
|
||||
"image",
|
||||
"location",
|
||||
"inReplyTo",
|
||||
"tags",
|
||||
"url",
|
||||
"generator",
|
||||
"provider",
|
||||
"scope"
|
||||
)
|
||||
.dateTime(
|
||||
"published",
|
||||
"updated",
|
||||
"startTime",
|
||||
"endTime"
|
||||
)
|
||||
.naturalLanguageValue(
|
||||
"summary",
|
||||
"title",
|
||||
"content",
|
||||
"displayName"
|
||||
)
|
||||
.`as`("language", String::class.java)
|
||||
.`as`("actions", ActionsValue::class.java)
|
||||
.string("id", "rel", "alias")
|
||||
.doub("rating")
|
||||
.integer("height", "width")
|
||||
.`as`("mediaType", MediaType::class.java)
|
||||
.`as`("duration", Duration::class.java)
|
||||
.get()
|
||||
|
||||
val activity: Model = Model.Companion.make("object")
|
||||
.type(
|
||||
Activity::class.java,
|
||||
Activity.Builder::class.java
|
||||
)
|
||||
.typeValue("verb")
|
||||
.linkValue(
|
||||
"actor",
|
||||
"participant",
|
||||
"instrument",
|
||||
"object",
|
||||
"target",
|
||||
"result",
|
||||
"to",
|
||||
"bto",
|
||||
"cc",
|
||||
"bcc"
|
||||
)
|
||||
.doub("priority")
|
||||
.`as`("status", Activity.Status::class.java)
|
||||
.get()
|
||||
|
||||
val collection: Model = Model.Companion.make("object")
|
||||
.type(
|
||||
Collection::class.java,
|
||||
Collection.Builder::class.java
|
||||
)
|
||||
.dateTime(
|
||||
"itemsBefore",
|
||||
"itemsAfter"
|
||||
)
|
||||
.linkValue(
|
||||
"first",
|
||||
"last",
|
||||
"prev",
|
||||
"next",
|
||||
"current",
|
||||
"self"
|
||||
)
|
||||
.`object`("items")
|
||||
.integer(
|
||||
"startIndex",
|
||||
"itemsPerPage",
|
||||
"totalItems"
|
||||
)
|
||||
.get()
|
||||
|
||||
/**
|
||||
* Method make.
|
||||
*
|
||||
* @return Schema.Builder
|
||||
*/
|
||||
@JvmStatic
|
||||
fun make(): Builder {
|
||||
return Builder()
|
||||
.map(null, `object`)
|
||||
.map("activity", activity)
|
||||
.map("collection", collection)
|
||||
}
|
||||
|
||||
val DEFAULT_SCHEMA: Schema = make().get()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
/**
|
||||
* Copyright 2013 OpenSocial Foundation
|
||||
* Copyright 2013 International Business Machines Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Utility library for working with Activity Streams Actions
|
||||
* Requires underscorejs.
|
||||
*
|
||||
* @author James M Snell (jasnell@us.ibm.com)
|
||||
*/
|
||||
package com.ibm.common.activitystreams.i
|
||||
|
||||
import com.google.common.base.Function
|
||||
import com.google.gson.JsonDeserializationContext
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonParseException
|
||||
import com.google.gson.JsonSerializationContext
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
* @author james
|
||||
* @version $Revision: 1.0 $
|
||||
*/
|
||||
abstract class SimpleAdapter<T>
|
||||
: Adapter<T>(), Function<String?, T> {
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param t T
|
||||
* @param type Type
|
||||
* @param context JsonSerializationContext
|
||||
*
|
||||
*
|
||||
* @return JsonElement * @see com.google.gson.JsonSerializer#serialize(T, Type, JsonSerializationContext)
|
||||
*/
|
||||
override fun serialize(
|
||||
t: T,
|
||||
type: Type,
|
||||
context: JsonSerializationContext
|
||||
): JsonElement {
|
||||
return context.serialize(serialize(t))
|
||||
}
|
||||
|
||||
/**
|
||||
* Method serialize.
|
||||
* @param t T
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
protected open fun serialize(t: T?): String? {
|
||||
return t?.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param v String
|
||||
*
|
||||
* @return T
|
||||
*/
|
||||
protected fun deserialize(v: String?): T? {
|
||||
return apply(v)
|
||||
}
|
||||
|
||||
/**
|
||||
* Method deserialize.
|
||||
* @param json JsonElement
|
||||
* @param type Type
|
||||
* @param context JsonDeserializationContext
|
||||
*
|
||||
*
|
||||
*
|
||||
* @return T * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
|
||||
*/
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(
|
||||
json: JsonElement,
|
||||
type: Type,
|
||||
context: JsonDeserializationContext
|
||||
): T {
|
||||
return deserialize(json.asJsonPrimitive.asString)!!
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue