e68724a2626766091bd988f60ed54cd1b5fe9b16
[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     private static final ShardTransactionMessages.DataExistsReply SERIALIZABLE_TRUE =
21             ShardTransactionMessages.DataExistsReply.newBuilder().setExists(true).build();
22     private static final ShardTransactionMessages.DataExistsReply SERIALIZABLE_FALSE =
23             ShardTransactionMessages.DataExistsReply.newBuilder().setExists(false).build();
24
25     private boolean exists;
26
27     public DataExistsReply() {
28     }
29
30     public DataExistsReply(final boolean exists, final short version) {
31         super(version);
32         this.exists = exists;
33     }
34
35     public boolean exists() {
36         return exists;
37     }
38
39     @Override
40     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
41         super.readExternal(in);
42         exists = in.readBoolean();
43     }
44
45     @Override
46     public void writeExternal(ObjectOutput out) throws IOException {
47         super.writeExternal(out);
48         out.writeBoolean(exists);
49     }
50
51     @Override
52     public Object toSerializable() {
53         if(getVersion() >= DataStoreVersions.BORON_VERSION) {
54             return this;
55         } else {
56             return exists ? SERIALIZABLE_TRUE : SERIALIZABLE_FALSE;
57         }
58     }
59
60     public static DataExistsReply fromSerializable(final Object serializable) {
61         if(serializable instanceof DataExistsReply) {
62             return (DataExistsReply)serializable;
63         } else {
64             ShardTransactionMessages.DataExistsReply o = (ShardTransactionMessages.DataExistsReply) serializable;
65             return new DataExistsReply(o.getExists(), DataStoreVersions.LITHIUM_VERSION);
66         }
67     }
68
69     public static boolean isSerializedType(Object message) {
70         return message instanceof DataExistsReply || message instanceof ShardTransactionMessages.DataExistsReply;
71     }
72 }