Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
Brian O'Connor | f3d0616 | 2014-10-02 15:54:12 -0700 | [diff] [blame^] | 2 | //TODO is this the right package? |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 3 | |
| 4 | import static com.google.common.base.Preconditions.checkNotNull; |
| 5 | |
| 6 | import java.util.Collections; |
| 7 | import java.util.LinkedList; |
| 8 | import java.util.List; |
| 9 | |
| 10 | /** |
| 11 | * A list of BatchOperationEntry. |
| 12 | * |
| 13 | * @param <T> the enum of operators <br> |
| 14 | * This enum must be defined in each sub-classes. |
| 15 | * |
| 16 | */ |
| 17 | public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> { |
| 18 | private List<T> ops; |
| 19 | |
| 20 | /** |
| 21 | * Creates new {@link BatchOperation} object. |
| 22 | */ |
| 23 | public BatchOperation() { |
| 24 | ops = new LinkedList<>(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Creates {@link BatchOperation} object from a list of batch operation |
| 29 | * entries. |
| 30 | * |
| 31 | * @param batchOperations the list of batch operation entries. |
| 32 | */ |
| 33 | public BatchOperation(List<T> batchOperations) { |
| 34 | ops = new LinkedList<>(checkNotNull(batchOperations)); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Removes all operations maintained in this object. |
| 39 | */ |
| 40 | public void clear() { |
| 41 | ops.clear(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns the number of operations in this object. |
| 46 | * |
| 47 | * @return the number of operations in this object |
| 48 | */ |
| 49 | public int size() { |
| 50 | return ops.size(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the operations in this object. |
| 55 | * |
| 56 | * @return the operations in this object |
| 57 | */ |
| 58 | public List<T> getOperations() { |
| 59 | return Collections.unmodifiableList(ops); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Adds an operation. |
| 64 | * |
| 65 | * @param entry the operation to be added |
| 66 | * @return this object if succeeded, null otherwise |
| 67 | */ |
| 68 | public BatchOperation<T> addOperation(T entry) { |
| 69 | return ops.add(entry) ? this : null; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean equals(Object o) { |
| 74 | if (this == o) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | if (o == null) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (getClass() != o.getClass()) { |
| 83 | return false; |
| 84 | } |
| 85 | BatchOperation<?> other = (BatchOperation<?>) o; |
| 86 | |
| 87 | return this.ops.equals(other.ops); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int hashCode() { |
| 92 | return ops.hashCode(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String toString() { |
| 97 | return ops.toString(); |
| 98 | } |
| 99 | } |