b24b51b5de2c6d63a9a5a0bd8d8dc40218c9d997
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / DataExistsReply.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.datastore.messages;
10
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
15 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
16
17 public class DataExistsReply extends VersionedExternalizableMessage {
18     private static final long serialVersionUID = 1L;
19
20     @Deprecated
21     private static final ShardTransactionMessages.DataExistsReply SERIALIZABLE_TRUE =
22             ShardTransactionMessages.DataExistsReply.newBuilder().setExists(true).build();
23     @Deprecated
24     private static final ShardTransactionMessages.DataExistsReply SERIALIZABLE_FALSE =
25             ShardTransactionMessages.DataExistsReply.newBuilder().setExists(false).build();
26
27     private boolean exists;
28
29     public DataExistsReply() {
30     }
31
32     public DataExistsReply(final boolean exists, final short version) {
33         super(version);
34         this.exists = exists;
35     }
36
37     public boolean exists() {
38         return exists;
39     }
40
41     @Override
42     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
43         super.readExternal(in);
44         exists = in.readBoolean();
45     }
46
47     @Override
48     public void writeExternal(ObjectOutput out) throws IOException {
49         super.writeExternal(out);
50         out.writeBoolean(exists);
51     }
52
53     @Deprecated
54     @Override
55     protected Object newLegacySerializedInstance() {
56         return exists ? SERIALIZABLE_TRUE : SERIALIZABLE_FALSE;
57     }
58
59     public static DataExistsReply fromSerializable(final Object serializable) {
60         if(serializable instanceof DataExistsReply) {
61             return (DataExistsReply)serializable;
62         } else {
63             ShardTransactionMessages.DataExistsReply o = (ShardTransactionMessages.DataExistsReply) serializable;
64             return new DataExistsReply(o.getExists(), DataStoreVersions.LITHIUM_VERSION);
65         }
66     }
67
68     public static boolean isSerializedType(Object message) {
69         return message instanceof DataExistsReply || message instanceof ShardTransactionMessages.DataExistsReply;
70     }
71 }