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