atomic-storage: remove type dependency at segment level I/O
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ModifyTransactionRequestBuilder.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.commands;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorRef;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19
20 /**
21  * A reusable builder for creating {@link ModifyTransactionRequest} message instances. Its internal state is reset when
22  * {@link #build()} is invoked, hence it can be used to create a sequence of messages. This class is NOT thread-safe.
23  */
24 public final class ModifyTransactionRequestBuilder implements Identifiable<TransactionIdentifier> {
25     private final List<TransactionModification> modifications = new ArrayList<>(1);
26     private final @NonNull TransactionIdentifier identifier;
27     private final ActorRef replyTo;
28
29     private PersistenceProtocol protocol;
30     private boolean haveSequence;
31     private long sequence;
32
33     public ModifyTransactionRequestBuilder(final TransactionIdentifier identifier, final ActorRef replyTo) {
34         this.identifier = requireNonNull(identifier);
35         this.replyTo = requireNonNull(replyTo);
36     }
37
38     @Override
39     public TransactionIdentifier getIdentifier() {
40         return identifier;
41     }
42
43     private void checkNotFinished() {
44         checkState(protocol == null, "Batch has already been finished");
45     }
46
47     public void addModification(final TransactionModification modification) {
48         checkNotFinished();
49         modifications.add(requireNonNull(modification));
50     }
51
52     public void setSequence(final long sequence) {
53         checkState(!haveSequence, "Sequence has already been set");
54         this.sequence = sequence;
55         haveSequence = true;
56     }
57
58     public void setAbort() {
59         checkNotFinished();
60         // Transaction is being aborted, no need to transmit operations
61         modifications.clear();
62         protocol = PersistenceProtocol.ABORT;
63     }
64
65     public void setCommit(final boolean coordinated) {
66         checkNotFinished();
67         protocol = coordinated ? PersistenceProtocol.THREE_PHASE : PersistenceProtocol.SIMPLE;
68     }
69
70     public void setReady() {
71         checkNotFinished();
72         protocol = PersistenceProtocol.READY;
73     }
74
75     public int size() {
76         return modifications.size();
77     }
78
79     public @NonNull ModifyTransactionRequest build() {
80         checkState(haveSequence, "Request sequence has not been set");
81
82         final ModifyTransactionRequest ret = new ModifyTransactionRequest(identifier, sequence, replyTo, modifications,
83             protocol);
84         modifications.clear();
85         protocol = null;
86         haveSequence = false;
87         return ret;
88     }
89 }