<NME> Unsubscribed.java
<BEF> ADDFILE
<MSG> Add subscription related messages. (#178)

* Add subscription related messages.

* revert stuff

<DFF> @@ -0,0 +1,69 @@
+package io.crossbar.autobahn.wamp.messages;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import io.crossbar.autobahn.wamp.exceptions.ProtocolError;
+import io.crossbar.autobahn.wamp.interfaces.IMessage;
+
+public class Unsubscribed implements IMessage {
+
+    public static final int MESSAGE_TYPE = 35;
+
+    private static final long SUBSCRIPTON_NULL = -1;
+    private final long request;
+    private final long subscription;
+    private final String reason;
+
+    public Unsubscribed(long request, long subscription, String reason) {
+        this.request = request;
+        this.subscription = subscription;
+        this.reason = reason;
+    }
+
+    public static Unsubscribed parse(List<Object> wmsg) {
+        if (wmsg.size() == 0 || !(wmsg.get(0) instanceof Integer) || (int) wmsg.get(0) != MESSAGE_TYPE) {
+            throw new IllegalArgumentException("Invalid message.");
+        }
+
+        if (wmsg.size() < 2 || wmsg.size() > 3) {
+            throw new ProtocolError(String.format("invalid message length %s for UNSUBSCRIBED", wmsg.size()));
+        }
+
+        long request = (long) wmsg.get(1);
+
+        long subscription = SUBSCRIPTON_NULL;
+        String reason = null;
+        if (wmsg.size() > 2) {
+            Map<String, Object> details = (Map<String, Object>) wmsg.get(2);
+            if (details.containsKey("subscription")) {
+                subscription = (long) details.get("subscription");
+            }
+            if (details.containsKey("reason")) {
+                reason = (String) details.get("reason");
+            }
+        }
+
+        return new Unsubscribed(request, subscription, reason);
+    }
+
+    @Override
+    public List<Object> marshal() {
+        List<Object> marshaled = new ArrayList<>();
+        marshaled.add(MESSAGE_TYPE);
+        marshaled.add(request);
+        if (subscription != SUBSCRIPTON_NULL || reason != null) {
+            Map<String, Object> details = new HashMap<>();
+            if (reason != null) {
+                details.put("reason", reason);
+            }
+            if (subscription != SUBSCRIPTON_NULL) {
+                details.put("subscription", subscription);
+            }
+            marshaled.add(details);
+        }
+        return marshaled;
+    }
+}

{'commit': 'diff --git a/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Subscribe.java b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Subscribe.java\nnew file mode 100644\nindex 0000000..2d931d7\n--- /dev/null\n+++ b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Subscribe.java\n@@ -0,0 +1,69 @@\n+package io.crossbar.autobahn.wamp.messages;\n+\n+import java.util.ArrayList;\n+import java.util.HashMap;\n+import java.util.List;\n+import java.util.Map;\n+import java.util.Objects;\n+\n+import io.crossbar.autobahn.wamp.exceptions.ProtocolError;\n+import io.crossbar.autobahn.wamp.interfaces.IMessage;\n+\n+public class Subscribe implements IMessage {\n+\n+    public static final int MESSAGE_TYPE = 32;\n+\n+    private final long request;\n+    private final String topic;\n+    private final String match;\n+    private final boolean getRetained;\n+\n+    private final String MATCH_EXACT = "exact";\n+    private final String MATCH_PREFIX = "prefix";\n+    private final String MATCH_WILDCARD = "wildcard";\n+\n+    public Subscribe(long request, String topic, String match, boolean getRetained) {\n+        this.request = request;\n+        this.topic = topic;\n+        if (match != null) {\n+            if (!Objects.equals(match, MATCH_EXACT) && !Objects.equals(match, MATCH_PREFIX) &&\n+                    !Objects.equals(match, MATCH_WILDCARD)) {\n+                throw new IllegalArgumentException("match must be one of exact, prefix or wildcard.");\n+            }\n+            this.match = match;\n+        } else {\n+            this.match = MATCH_EXACT;\n+        }\n+        this.getRetained = getRetained;\n+    }\n+\n+    public static Subscribe parse(List<Object> wmsg) {\n+        if (wmsg.size() == 0 || !(wmsg.get(0) instanceof Integer) || (int) wmsg.get(0) != MESSAGE_TYPE) {\n+            throw new IllegalArgumentException("Invalid message.");\n+        }\n+\n+        if (wmsg.size() != 4) {\n+            throw new ProtocolError(String.format("invalid message length %s for SUBSCRIBE", wmsg.size()));\n+        }\n+\n+        long request = (long) wmsg.get(1);\n+        Map<String, Object> options = (Map<String, Object>) wmsg.get(2);\n+        String match = (String) options.get("match");\n+        boolean getRetained = (boolean) options.get("get_retained");\n+        String topic = (String) wmsg.get(3);\n+        return new Subscribe(request, topic, match,getRetained);\n+    }\n+\n+    @Override\n+    public List<Object> marshal() {\n+        List<Object> marshaled = new ArrayList<>();\n+        marshaled.add(MESSAGE_TYPE);\n+        marshaled.add(request);\n+        Map<String, Object> extra = new HashMap<>();\n+        extra.put("match", match);\n+        extra.put("get_retained", getRetained);\n+        marshaled.add(extra);\n+        marshaled.add(topic);\n+        return marshaled;\n+    }\n+}\ndiff --git a/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Subscribed.java b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Subscribed.java\nnew file mode 100644\nindex 0000000..3b74faf\n--- /dev/null\n+++ b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Subscribed.java\n@@ -0,0 +1,39 @@\n+package io.crossbar.autobahn.wamp.messages;\n+\n+import java.util.ArrayList;\n+import java.util.List;\n+\n+import io.crossbar.autobahn.wamp.exceptions.ProtocolError;\n+import io.crossbar.autobahn.wamp.interfaces.IMessage;\n+\n+public class Subscribed implements IMessage {\n+\n+    public static final int MESSAGE_TYPE = 33;\n+    private final long request;\n+    private final long subscription;\n+\n+    public Subscribed(long request, long subscription) {\n+        this.request = request;\n+        this.subscription = subscription;\n+    }\n+\n+    public static Subscribed parse(List<Object> wmsg) {\n+        if (wmsg.size() == 0 || !(wmsg.get(0) instanceof Integer) || (int) wmsg.get(0) != MESSAGE_TYPE) {\n+            throw new IllegalArgumentException("Invalid message.");\n+        }\n+\n+        if (wmsg.size() != 3) {\n+            throw new ProtocolError(String.format("invalid message length %s for SUBSCRIBED", wmsg.size()));\n+        }\n+        return new Subscribed((long) wmsg.get(1), (long) wmsg.get(2));\n+    }\n+\n+    @Override\n+    public List<Object> marshal() {\n+        List<Object> marshaled = new ArrayList<>();\n+        marshaled.add(MESSAGE_TYPE);\n+        marshaled.add(request);\n+        marshaled.add(subscription);\n+        return marshaled;\n+    }\n+}\ndiff --git a/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Unsubscribe.java b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Unsubscribe.java\nnew file mode 100644\nindex 0000000..c1e982d\n--- /dev/null\n+++ b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Unsubscribe.java\n@@ -0,0 +1,39 @@\n+package io.crossbar.autobahn.wamp.messages;\n+\n+import java.util.ArrayList;\n+import java.util.List;\n+\n+import io.crossbar.autobahn.wamp.exceptions.ProtocolError;\n+import io.crossbar.autobahn.wamp.interfaces.IMessage;\n+\n+public class Unsubscribe implements IMessage {\n+\n+    public static final int MESSAGE_TYPE = 34;\n+    private final long request;\n+    private final long subscription;\n+\n+    public Unsubscribe(long request, long subscription) {\n+        this.request = request;\n+        this.subscription = subscription;\n+    }\n+\n+    public static Unsubscribe parse(List<Object> wmsg) {\n+        if (wmsg.size() == 0 || !(wmsg.get(0) instanceof Integer) || (int) wmsg.get(0) != MESSAGE_TYPE) {\n+            throw new IllegalArgumentException("Invalid message.");\n+        }\n+\n+        if (wmsg.size() != 3) {\n+            throw new ProtocolError(String.format("invalid message length %s for UNSUBSCRIBE", wmsg.size()));\n+        }\n+        return new Unsubscribe((long) wmsg.get(1), (long) wmsg.get(2));\n+    }\n+\n+    @Override\n+    public List<Object> marshal() {\n+        List<Object> marshaled = new ArrayList<>();\n+        marshaled.add(MESSAGE_TYPE);\n+        marshaled.add(request);\n+        marshaled.add(subscription);\n+        return marshaled;\n+    }\n+}\ndiff --git a/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Unsubscribed.java b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Unsubscribed.java\nnew file mode 100644\nindex 0000000..e377195\n--- /dev/null\n+++ b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Unsubscribed.java\n@@ -0,0 +1,69 @@\n+package io.crossbar.autobahn.wamp.messages;\n+\n+import java.util.ArrayList;\n+import java.util.HashMap;\n+import java.util.List;\n+import java.util.Map;\n+\n+import io.crossbar.autobahn.wamp.exceptions.ProtocolError;\n+import io.crossbar.autobahn.wamp.interfaces.IMessage;\n+\n+public class Unsubscribed implements IMessage {\n+\n+    public static final int MESSAGE_TYPE = 35;\n+\n+    private static final long SUBSCRIPTON_NULL = -1;\n+    private final long request;\n+    private final long subscription;\n+    private final String reason;\n+\n+    public Unsubscribed(long request, long subscription, String reason) {\n+        this.request = request;\n+        this.subscription = subscription;\n+        this.reason = reason;\n+    }\n+\n+    public static Unsubscribed parse(List<Object> wmsg) {\n+        if (wmsg.size() == 0 || !(wmsg.get(0) instanceof Integer) || (int) wmsg.get(0) != MESSAGE_TYPE) {\n+            throw new IllegalArgumentException("Invalid message.");\n+        }\n+\n+        if (wmsg.size() < 2 || wmsg.size() > 3) {\n+            throw new ProtocolError(String.format("invalid message length %s for UNSUBSCRIBED", wmsg.size()));\n+        }\n+\n+        long request = (long) wmsg.get(1);\n+\n+        long subscription = SUBSCRIPTON_NULL;\n+        String reason = null;\n+        if (wmsg.size() > 2) {\n+            Map<String, Object> details = (Map<String, Object>) wmsg.get(2);\n+            if (details.containsKey("subscription")) {\n+                subscription = (long) details.get("subscription");\n+            }\n+            if (details.containsKey("reason")) {\n+                reason = (String) details.get("reason");\n+            }\n+        }\n+\n+        return new Unsubscribed(request, subscription, reason);\n+    }\n+\n+    @Override\n+    public List<Object> marshal() {\n+        List<Object> marshaled = new ArrayList<>();\n+        marshaled.add(MESSAGE_TYPE);\n+        marshaled.add(request);\n+        if (subscription != SUBSCRIPTON_NULL || reason != null) {\n+            Map<String, Object> details = new HashMap<>();\n+            if (reason != null) {\n+                details.put("reason", reason);\n+            }\n+            if (subscription != SUBSCRIPTON_NULL) {\n+                details.put("subscription", subscription);\n+            }\n+            marshaled.add(details);\n+        }\n+        return marshaled;\n+    }\n+}\n', 'commit_subject': 'Add subscription related messages. (#178)', 'repo_name': 'crossbario/autobahn-java', 'committer': {'name': 'Tobias Oberstein', 'email': '48f8b808c28d6b683c4fdb8643f3a26daadcf581@crossbario.com', 'time_sec': '1497619408', 'tz_offset': '120', 'date': {'seconds': '1497619408'}}, 'commit_tree': 'bcb349bdce13639c0131aa28f2137a509a2b18de', 'commit_message': 'Add subscription related messages. (#178)\n\n* Add subscription related messages.\r\n\r\n* revert stuff\r\n', 'language_name': 'Python', 'license': 'mit', 'hunks': '@@ -0,0 +1,69 @@\n+package io.crossbar.autobahn.wamp.messages;\n+\n+import java.util.ArrayList;\n+import java.util.HashMap;\n+import java.util.List;\n+import java.util.Map;\n+\n+import io.crossbar.autobahn.wamp.exceptions.ProtocolError;\n+import io.crossbar.autobahn.wamp.interfaces.IMessage;\n+\n+public class Unsubscribed implements IMessage {\n+\n+    public static final int MESSAGE_TYPE = 35;\n+\n+    private static final long SUBSCRIPTON_NULL = -1;\n+    private final long request;\n+    private final long subscription;\n+    private final String reason;\n+\n+    public Unsubscribed(long request, long subscription, String reason) {\n+        this.request = request;\n+        this.subscription = subscription;\n+        this.reason = reason;\n+    }\n+\n+    public static Unsubscribed parse(List<Object> wmsg) {\n+        if (wmsg.size() == 0 || !(wmsg.get(0) instanceof Integer) || (int) wmsg.get(0) != MESSAGE_TYPE) {\n+            throw new IllegalArgumentException("Invalid message.");\n+        }\n+\n+        if (wmsg.size() < 2 || wmsg.size() > 3) {\n+            throw new ProtocolError(String.format("invalid message length %s for UNSUBSCRIBED", wmsg.size()));\n+        }\n+\n+        long request = (long) wmsg.get(1);\n+\n+        long subscription = SUBSCRIPTON_NULL;\n+        String reason = null;\n+        if (wmsg.size() > 2) {\n+            Map<String, Object> details = (Map<String, Object>) wmsg.get(2);\n+            if (details.containsKey("subscription")) {\n+                subscription = (long) details.get("subscription");\n+            }\n+            if (details.containsKey("reason")) {\n+                reason = (String) details.get("reason");\n+            }\n+        }\n+\n+        return new Unsubscribed(request, subscription, reason);\n+    }\n+\n+    @Override\n+    public List<Object> marshal() {\n+        List<Object> marshaled = new ArrayList<>();\n+        marshaled.add(MESSAGE_TYPE);\n+        marshaled.add(request);\n+        if (subscription != SUBSCRIPTON_NULL || reason != null) {\n+            Map<String, Object> details = new HashMap<>();\n+            if (reason != null) {\n+                details.put("reason", reason);\n+            }\n+            if (subscription != SUBSCRIPTON_NULL) {\n+                details.put("subscription", subscription);\n+            }\n+            marshaled.add(details);\n+        }\n+        return marshaled;\n+    }\n+}\n', 'addition_count': 69, 'deletion_count': 0, 'src_file': '/dev/null', 'tgt_file': 'b/autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Unsubscribed.java', 'file_extension': '.java', 'before_file': 'ADDFILE', 'message': 'Add subscription related messages. (#178)\n\n* Add subscription related messages.\r\n\r\n* revert stuff\r\n', 'author_name': 'Tobias Oberstein', 'lang': 'java'}