blob: 72a9847851cbb74021b7dab8aa6be2ad5c6171d5 [file] [log] [blame]
Brian O'Connorb876bf12014-10-02 14:59:37 -07001package org.onlab.onos.net.intent;
Brian O'Connorf3d06162014-10-02 15:54:12 -07002//TODO is this the right package?
Brian O'Connorb876bf12014-10-02 14:59:37 -07003
alshabib902d41b2014-10-07 16:52:05 -07004import static com.google.common.base.Preconditions.checkNotNull;
5
6import java.util.Collection;
Brian O'Connorb876bf12014-10-02 14:59:37 -07007import java.util.Collections;
8import java.util.LinkedList;
9import java.util.List;
10
11/**
12 * A list of BatchOperationEntry.
13 *
14 * @param <T> the enum of operators <br>
toma1d16b62014-10-02 23:45:11 -070015 * This enum must be defined in each sub-classes.
Brian O'Connorb876bf12014-10-02 14:59:37 -070016 */
17public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> {
toma1d16b62014-10-02 23:45:11 -070018
alshabib902d41b2014-10-07 16:52:05 -070019 private final List<T> ops;
Brian O'Connorb876bf12014-10-02 14:59:37 -070020
21 /**
22 * Creates new {@link BatchOperation} object.
23 */
24 public BatchOperation() {
25 ops = new LinkedList<>();
26 }
27
28 /**
29 * Creates {@link BatchOperation} object from a list of batch operation
30 * entries.
31 *
32 * @param batchOperations the list of batch operation entries.
33 */
alshabib902d41b2014-10-07 16:52:05 -070034 public BatchOperation(Collection<T> batchOperations) {
Brian O'Connorb876bf12014-10-02 14:59:37 -070035 ops = new LinkedList<>(checkNotNull(batchOperations));
36 }
37
38 /**
39 * Removes all operations maintained in this object.
40 */
41 public void clear() {
42 ops.clear();
43 }
44
45 /**
46 * Returns the number of operations in this object.
47 *
48 * @return the number of operations in this object
49 */
50 public int size() {
51 return ops.size();
52 }
53
54 /**
55 * Returns the operations in this object.
56 *
57 * @return the operations in this object
58 */
59 public List<T> getOperations() {
60 return Collections.unmodifiableList(ops);
61 }
62
63 /**
64 * Adds an operation.
alshabib902d41b2014-10-07 16:52:05 -070065 * FIXME: Brian promises that the Intent Framework
66 * will not modify the batch operation after it has submitted it.
67 * Ali would prefer immutablity, but trusts brian for better or
68 * for worse.
Brian O'Connorb876bf12014-10-02 14:59:37 -070069 *
70 * @param entry the operation to be added
71 * @return this object if succeeded, null otherwise
72 */
73 public BatchOperation<T> addOperation(T entry) {
74 return ops.add(entry) ? this : null;
75 }
76
77 @Override
78 public boolean equals(Object o) {
79 if (this == o) {
80 return true;
81 }
82
83 if (o == null) {
84 return false;
85 }
86
87 if (getClass() != o.getClass()) {
88 return false;
89 }
90 BatchOperation<?> other = (BatchOperation<?>) o;
91
92 return this.ops.equals(other.ops);
93 }
94
95 @Override
96 public int hashCode() {
97 return ops.hashCode();
98 }
99
100 @Override
101 public String toString() {
102 return ops.toString();
103 }
104}