tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.ccc; |
| 2 | |
| 3 | import org.onlab.nio.AbstractMessage; |
| 4 | |
| 5 | import java.util.Objects; |
| 6 | |
| 7 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 8 | |
| 9 | /** |
| 10 | * Base message for cluster-wide communications using TLVs. |
| 11 | */ |
| 12 | public class TLVMessage extends AbstractMessage { |
| 13 | |
| 14 | private final int type; |
| 15 | private final Object data; |
| 16 | |
| 17 | /** |
| 18 | * Creates an immutable TLV message. |
| 19 | * |
| 20 | * @param type message type |
| 21 | * @param length message length |
| 22 | * @param data message data |
| 23 | */ |
| 24 | public TLVMessage(int type, int length, Object data) { |
| 25 | this.length = length; |
| 26 | this.type = type; |
| 27 | this.data = data; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Returns the message type indicator. |
| 32 | * |
| 33 | * @return message type |
| 34 | */ |
| 35 | public int type() { |
| 36 | return type; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns the data object. |
| 41 | * |
| 42 | * @return message data |
| 43 | */ |
| 44 | public Object data() { |
| 45 | return data; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public int hashCode() { |
| 50 | return Objects.hash(type, data); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean equals(Object obj) { |
| 55 | if (this == obj) { |
| 56 | return true; |
| 57 | } |
| 58 | if (obj == null || getClass() != obj.getClass()) { |
| 59 | return false; |
| 60 | } |
| 61 | final TLVMessage other = (TLVMessage) obj; |
| 62 | return Objects.equals(this.type, other.type) && |
| 63 | Objects.equals(this.data, other.data); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public String toString() { |
| 68 | return toStringHelper(this).add("type", type).add("length", length).toString(); |
| 69 | } |
| 70 | |
| 71 | } |