2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.messages;
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.cohort3pc.ThreePhaseCommitCohortMessages;
17 public class CanCommitTransactionReply extends VersionedExternalizableMessage {
18 private static final CanCommitTransactionReply YES =
19 new CanCommitTransactionReply(true, DataStoreVersions.CURRENT_VERSION);
20 private static final CanCommitTransactionReply NO =
21 new CanCommitTransactionReply(false, DataStoreVersions.CURRENT_VERSION);
23 private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply YES_SERIALIZED =
24 ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(true).build();
26 private static final ThreePhaseCommitCohortMessages.CanCommitTransactionReply NO_SERIALIZED =
27 ThreePhaseCommitCohortMessages.CanCommitTransactionReply.newBuilder().setCanCommit(false).build();
29 private boolean canCommit;
31 public CanCommitTransactionReply() {
34 private CanCommitTransactionReply(final boolean canCommit, final short version) {
36 this.canCommit = canCommit;
39 public boolean getCanCommit() {
44 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
45 super.readExternal(in);
46 canCommit = in.readBoolean();
50 public void writeExternal(ObjectOutput out) throws IOException {
51 super.writeExternal(out);
52 out.writeBoolean(canCommit);
56 protected Object newLegacySerializedInstance() {
57 return canCommit ? YES_SERIALIZED : NO_SERIALIZED;
61 public String toString() {
62 return "CanCommitTransactionReply [canCommit=" + canCommit + ", version=" + getVersion() + "]";
65 public static CanCommitTransactionReply yes(short version) {
66 return version == DataStoreVersions.CURRENT_VERSION ? YES : new CanCommitTransactionReply(true, version);
69 public static CanCommitTransactionReply no(short version) {
70 return version == DataStoreVersions.CURRENT_VERSION ? NO : new CanCommitTransactionReply(false, version);
73 public static CanCommitTransactionReply fromSerializable(final Object serializable) {
74 if(serializable instanceof CanCommitTransactionReply) {
75 return (CanCommitTransactionReply)serializable;
77 ThreePhaseCommitCohortMessages.CanCommitTransactionReply serialized =
78 (ThreePhaseCommitCohortMessages.CanCommitTransactionReply) serializable;
79 return serialized.getCanCommit() ? YES : NO;
83 public static boolean isSerializedType(Object message) {
84 return message instanceof CanCommitTransactionReply ||
85 message instanceof ThreePhaseCommitCohortMessages.CanCommitTransactionReply;