4b60aecefa7e01d146a9c3afb9387d5a564a66c8
[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 com.google.common.base.Verify;
11 import java.io.DataInput;
12 import java.io.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.concepts.WritableIdentifier;
18
19 /**
20  * Abstract Externalizable proxy for use with {@link Message} subclasses.
21  *
22  * @author Robert Varga
23  *
24  * @param <T> Target identifier type
25  * @param <C> Message class
26  */
27 abstract class AbstractMessageProxy<T extends WritableIdentifier, C extends Message<T, C>> implements Externalizable {
28     private static final long serialVersionUID = 1L;
29     private T target;
30
31     protected AbstractMessageProxy() {
32         // For Externalizable
33     }
34
35     AbstractMessageProxy(final @Nonnull C message) {
36         this.target = message.getTarget();
37     }
38
39     @Override
40     public void writeExternal(final ObjectOutput out) throws IOException {
41         target.writeTo(out);
42     }
43
44     @Override
45     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
46         target = Verify.verifyNotNull(readTarget(in));
47     }
48
49     protected final Object readResolve() {
50         return Verify.verifyNotNull(createMessage(target));
51     }
52
53     protected abstract @Nonnull T readTarget(@Nonnull DataInput in) throws IOException;
54     abstract @Nonnull C createMessage(@Nonnull T target);
55 }