cd674b48569e109ba1bd60be9dd386641e770820
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / Envelope.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.controller.cluster.access.concepts;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.io.Serializable;
13 import org.opendaylight.yangtools.concepts.Immutable;
14
15 public abstract class Envelope<T extends Message<?, ?>> implements Immutable, Serializable {
16     private static final long serialVersionUID = 1L;
17
18     private final T message;
19     private final long sequence;
20     private final long retry;
21
22     Envelope(final T message, final long sequence, final long retry) {
23         this.message = Preconditions.checkNotNull(message);
24         this.sequence = sequence;
25         this.retry = retry;
26     }
27
28     /**
29      * Get the enclosed message
30      *
31      * @return enclose message
32      */
33     public T getMessage() {
34         return message;
35     }
36
37     /**
38      * Get the message sequence of this envelope.
39      *
40      * @return Message sequence
41      */
42     public long getSequence() {
43         return sequence;
44     }
45
46     /**
47      * Get the message retry counter.
48      *
49      * @return Retry counter
50      */
51     public long getRetry() {
52         return retry;
53     }
54
55     @Override
56     public String toString() {
57         return MoreObjects.toStringHelper(Envelope.class).add("sequence", Long.toUnsignedString(sequence, 16)).
58                 add("retry", retry).add("message", message).toString();
59     }
60
61     final Object writeReplace() {
62         return createProxy();
63     }
64
65     abstract AbstractEnvelopeProxy<T> createProxy();
66 }