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 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 4 | import static com.google.common.base.Preconditions.checkNotNull; |
| 5 | |
| 6 | import java.util.Collection; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 7 | import java.util.Collections; |
| 8 | import java.util.LinkedList; |
| 9 | import java.util.List; |
| 10 | |
| 11 | /** |
| 12 | * A list of BatchOperationEntry. |
| 13 | * |
| 14 | * @param <T> the enum of operators <br> |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 15 | * This enum must be defined in each sub-classes. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 16 | */ |
| 17 | public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> { |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 18 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 19 | private final List<T> ops; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 20 | |
| 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 | */ |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 34 | public BatchOperation(Collection<T> batchOperations) { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 35 | 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. |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 65 | * 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'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 69 | * |
| 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 | } |