Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | |
| 5 | import java.util.Collections; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | |
| 9 | /** |
| 10 | * A list of BatchOperationEntry. |
| 11 | * |
| 12 | * @param <T> the enum of operators <br> |
| 13 | * This enum must be defined in each sub-classes. |
| 14 | * |
| 15 | */ |
| 16 | public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> { |
| 17 | private List<T> ops; |
| 18 | |
| 19 | /** |
| 20 | * Creates new {@link BatchOperation} object. |
| 21 | */ |
| 22 | public BatchOperation() { |
| 23 | ops = new LinkedList<>(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * 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 | /** |
| 37 | * Removes all operations maintained in this object. |
| 38 | */ |
| 39 | public void clear() { |
| 40 | ops.clear(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns the number of operations in this object. |
| 45 | * |
| 46 | * @return the number of operations in this object |
| 47 | */ |
| 48 | public int size() { |
| 49 | return ops.size(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the operations in this object. |
| 54 | * |
| 55 | * @return the operations in this object |
| 56 | */ |
| 57 | public List<T> getOperations() { |
| 58 | return Collections.unmodifiableList(ops); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Adds an operation. |
| 63 | * |
| 64 | * @param entry the operation to be added |
| 65 | * @return this object if succeeded, null otherwise |
| 66 | */ |
| 67 | public BatchOperation<T> addOperation(T entry) { |
| 68 | return ops.add(entry) ? this : null; |
| 69 | } |
| 70 | |
| 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 | } |
| 98 | } |