blob: 65836c469b00e3880a88645444117ef403a1090e [file] [log] [blame]
Toshio Koidea03915e2014-07-01 18:39:52 -07001package net.onrc.onos.api.batchoperation;
2
Toshio Koide3c846312014-08-21 19:47:15 -07003import static com.google.common.base.Preconditions.checkNotNull;
4
Toshio Koidecff5d982014-07-31 11:41:56 -07005import java.util.Collections;
Toshio Koidea03915e2014-07-01 18:39:52 -07006import java.util.LinkedList;
7import java.util.List;
8
9/**
10 * A list of BatchOperationEntry.
11 *
Toshio Koide4ea84192014-07-31 12:10:12 -070012 * @param <T> the enum of operators <br>
13 * This enum must be defined in each sub-classes.
14 *
Toshio Koidea03915e2014-07-01 18:39:52 -070015 */
Toshio Koide4ea84192014-07-31 12:10:12 -070016public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> {
17 private List<T> ops;
Toshio Koidea03915e2014-07-01 18:39:52 -070018
19 /**
Toshio Koide3c846312014-08-21 19:47:15 -070020 * Creates new {@link BatchOperation} object.
Toshio Koidea03915e2014-07-01 18:39:52 -070021 */
22 public BatchOperation() {
Toshio Koidea8e8c542014-07-31 12:10:12 -070023 ops = new LinkedList<>();
Toshio Koidea03915e2014-07-01 18:39:52 -070024 }
25
26 /**
Toshio Koide3c846312014-08-21 19:47:15 -070027 * Creates {@link BatchOperation} object from a list of batch operation
28 * entries.
29 *
30 * @param batchOperations the list of batch operation entries.
31 */
32 public BatchOperation(List<T> batchOperations) {
33 ops = new LinkedList<>(checkNotNull(batchOperations));
34 }
35
36 /**
Toshio Koidea03915e2014-07-01 18:39:52 -070037 * Removes all operations maintained in this object.
38 */
39 public void clear() {
40 ops.clear();
41 }
42
43 /**
Toshio Koidecff5d982014-07-31 11:41:56 -070044 * Returns the number of operations in this object.
45 *
Toshio Koidea8e8c542014-07-31 12:10:12 -070046 * @return the number of operations in this object
Toshio Koidecff5d982014-07-31 11:41:56 -070047 */
48 public int size() {
49 return ops.size();
50 }
51
52 /**
Toshio Koidecff5d982014-07-31 11:41:56 -070053 * Returns the operations in this object.
54 *
Toshio Koidea8e8c542014-07-31 12:10:12 -070055 * @return the operations in this object
Toshio Koidecff5d982014-07-31 11:41:56 -070056 */
Toshio Koide4ea84192014-07-31 12:10:12 -070057 public List<T> getOperations() {
Toshio Koidecff5d982014-07-31 11:41:56 -070058 return Collections.unmodifiableList(ops);
59 }
60
61 /**
Toshio Koide4ea84192014-07-31 12:10:12 -070062 * Adds an operation.
Toshio Koidea03915e2014-07-01 18:39:52 -070063 *
Toshio Koide4ea84192014-07-31 12:10:12 -070064 * @param entry the operation to be added
65 * @return this object if succeeded, null otherwise
Toshio Koidea03915e2014-07-01 18:39:52 -070066 */
Toshio Koide4ea84192014-07-31 12:10:12 -070067 public BatchOperation<T> addOperation(T entry) {
68 return ops.add(entry) ? this : null;
Toshio Koidea03915e2014-07-01 18:39:52 -070069 }
Pavlin Radoslavovdd08e8c2014-08-14 11:02:57 -070070
71 @Override
72 public boolean equals(Object o) {
73 if (this == o) {
74 return true;
75 }
76
77 if (o == null) {
78 return false;
79 }
80
81 if (getClass() != o.getClass()) {
82 return false;
83 }
84 BatchOperation<?> other = (BatchOperation<?>) o;
85
86 return this.ops.equals(other.ops);
87 }
88
89 @Override
90 public int hashCode() {
91 return ops.hashCode();
92 }
93
94 @Override
95 public String toString() {
96 return ops.toString();
97 }
Toshio Koidea03915e2014-07-01 18:39:52 -070098}