Bump cds-access-api ABIVersion
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / AbstractMessageProxy.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 static com.google.common.base.Verify.verifyNotNull;
11
12 import java.io.DataInput;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.concepts.WritableIdentifier;
19 import org.opendaylight.yangtools.concepts.WritableObjects;
20
21 /**
22  * Abstract Externalizable proxy for use with {@link Message} subclasses.
23  *
24  * @author Robert Varga
25  *
26  * @param <T> Target identifier type
27  * @param <C> Message class
28  */
29 abstract class AbstractMessageProxy<T extends WritableIdentifier, C extends Message<T, C>> implements Externalizable {
30     private static final long serialVersionUID = 1L;
31     private T target;
32     private long sequence;
33
34     protected AbstractMessageProxy() {
35         // For Externalizable
36     }
37
38     AbstractMessageProxy(final @NonNull C message) {
39         this.target = message.getTarget();
40         this.sequence = message.getSequence();
41     }
42
43     @Override
44     public void writeExternal(final ObjectOutput out) throws IOException {
45         target.writeTo(out);
46         WritableObjects.writeLong(out, sequence);
47     }
48
49     @Override
50     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
51         target = verifyNotNull(readTarget(in));
52         sequence = WritableObjects.readLong(in);
53     }
54
55     protected final Object readResolve() {
56         return verifyNotNull(createMessage(target, sequence));
57     }
58
59     protected abstract @NonNull T readTarget(@NonNull DataInput in) throws IOException;
60
61     abstract @NonNull C createMessage(@NonNull T msgTarget, long msgSequence);
62 }