Merge "Fix checkstyle warnings in netty-threadgroup-config."
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / example / messages / KeyValue.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.example.messages;
10
11 import com.google.protobuf.GeneratedMessage;
12 import java.io.Serializable;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
16 import org.opendaylight.controller.protobuff.messages.cluster.example.KeyValueMessages;
17 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
18
19 public class KeyValue extends Payload implements Serializable {
20     private static final long serialVersionUID = 1L;
21     private String key;
22     private String value;
23
24     public KeyValue() {
25     }
26
27     public KeyValue(String key, String value){
28         this.key = key;
29         this.value = value;
30     }
31
32     public String getKey() {
33         return key;
34     }
35
36     public String getValue() {
37         return value;
38     }
39
40     public void setKey(String key) {
41         this.key = key;
42     }
43
44     public void setValue(String value) {
45         this.value = value;
46     }
47
48     @Override public String toString() {
49         return "KeyValue{" +
50             "key='" + key + '\'' +
51             ", value='" + value + '\'' +
52             '}';
53     }
54
55     // override this method to return  the protobuff related extension fields and their values
56     @Override public Map<GeneratedMessage.GeneratedExtension, String> encode() {
57         Map<GeneratedMessage.GeneratedExtension, String> map = new HashMap<>();
58         map.put(KeyValueMessages.key, getKey());
59         map.put(KeyValueMessages.value, getValue());
60         return map;
61     }
62
63     // override this method to assign the values from protobuff
64     @Override public Payload decode(
65         AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payloadProtoBuff) {
66         String key = payloadProtoBuff.getExtension(KeyValueMessages.key);
67         String value = payloadProtoBuff.getExtension(KeyValueMessages.value);
68         this.setKey(key);
69         this.setValue(value);
70         return this;
71     }
72
73     @Override
74     public int size() {
75         return this.value.length() + this.key.length();
76     }
77
78 }