View Javadoc
1   /*
2    * Copyright 2016 The Lannister Project
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.anyflow.lannister.topic;
18  
19  import java.io.IOException;
20  
21  import com.fasterxml.jackson.annotation.JsonIgnore;
22  import com.hazelcast.nio.ObjectDataInput;
23  import com.hazelcast.nio.ObjectDataOutput;
24  
25  import net.anyflow.lannister.message.Message;
26  import net.anyflow.lannister.serialization.SerializableFactory;
27  
28  public class Notification implements com.hazelcast.nio.serialization.IdentifiedDataSerializable {
29      public final static int ID = 5;
30  
31      private String clientId;
32      private Topic topic;
33      private Message message;
34  
35      public Notification() { // just for Serialization
36      }
37  
38      protected Notification(String clientId, Topic topic, Message message) {
39          this.clientId = clientId;
40          this.topic = topic;
41          this.message = message;
42      }
43  
44      public String clientId() {
45          return clientId;
46      }
47  
48      public Topic topic() {
49          return topic;
50      }
51  
52      public Message message() {
53          return message;
54      }
55  
56      @JsonIgnore
57      @Override
58      public int getFactoryId() {
59          return SerializableFactory.ID;
60      }
61  
62      @Override
63      public int getId() {
64          return ID;
65      }
66  
67      @Override
68      public void writeData(ObjectDataOutput out) throws IOException {
69          out.writeUTF(clientId);
70          out.writeBoolean(topic != null);
71          if (topic != null) {
72              topic.writeData(out);
73          }
74          out.writeBoolean(message != null);
75          if (message != null) {
76              message.writeData(out);
77          }
78      }
79  
80      @Override
81      public void readData(ObjectDataInput in) throws IOException {
82          clientId = in.readUTF();
83          if (in.readBoolean()) {
84              Topic temp = new Topic();
85              temp.readData(in);
86  
87              topic = temp;
88          }
89          if (in.readBoolean()) {
90              Message temp = new Message();
91              temp.readData(in);
92  
93              message = temp;
94          }
95      }
96  }