Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.batchoperation; |
| 2 | |
Toshio Koide | cff5d98 | 2014-07-31 11:41:56 -0700 | [diff] [blame] | 3 | import java.util.Collections; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | /** |
| 8 | * A list of BatchOperationEntry. |
| 9 | * |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 10 | * @param <T> the enum of operators <br> |
| 11 | * This enum must be defined in each sub-classes. |
| 12 | * |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 13 | */ |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 14 | public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> { |
| 15 | private List<T> ops; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Constructor. |
| 19 | */ |
| 20 | public BatchOperation() { |
Toshio Koide | a8e8c54 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 21 | ops = new LinkedList<>(); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Removes all operations maintained in this object. |
| 26 | */ |
| 27 | public void clear() { |
| 28 | ops.clear(); |
| 29 | } |
| 30 | |
| 31 | /** |
Toshio Koide | cff5d98 | 2014-07-31 11:41:56 -0700 | [diff] [blame] | 32 | * Returns the number of operations in this object. |
| 33 | * |
Toshio Koide | a8e8c54 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 34 | * @return the number of operations in this object |
Toshio Koide | cff5d98 | 2014-07-31 11:41:56 -0700 | [diff] [blame] | 35 | */ |
| 36 | public int size() { |
| 37 | return ops.size(); |
| 38 | } |
| 39 | |
| 40 | /** |
Toshio Koide | cff5d98 | 2014-07-31 11:41:56 -0700 | [diff] [blame] | 41 | * Returns the operations in this object. |
| 42 | * |
Toshio Koide | a8e8c54 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 43 | * @return the operations in this object |
Toshio Koide | cff5d98 | 2014-07-31 11:41:56 -0700 | [diff] [blame] | 44 | */ |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 45 | public List<T> getOperations() { |
Toshio Koide | cff5d98 | 2014-07-31 11:41:56 -0700 | [diff] [blame] | 46 | return Collections.unmodifiableList(ops); |
| 47 | } |
| 48 | |
| 49 | /** |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 50 | * Adds an operation. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 51 | * |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 52 | * @param entry the operation to be added |
| 53 | * @return this object if succeeded, null otherwise |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 54 | */ |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 55 | public BatchOperation<T> addOperation(T entry) { |
| 56 | return ops.add(entry) ? this : null; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 57 | } |
Pavlin Radoslavov | dd08e8c | 2014-08-14 11:02:57 -0700 | [diff] [blame^] | 58 | |
| 59 | @Override |
| 60 | public boolean equals(Object o) { |
| 61 | if (this == o) { |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | if (o == null) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (getClass() != o.getClass()) { |
| 70 | return false; |
| 71 | } |
| 72 | BatchOperation<?> other = (BatchOperation<?>) o; |
| 73 | |
| 74 | return this.ops.equals(other.ops); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public int hashCode() { |
| 79 | return ops.hashCode(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String toString() { |
| 84 | return ops.toString(); |
| 85 | } |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 86 | } |