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.Iterator; |
| 5 | import java.util.LinkedList; |
| 6 | import java.util.List; |
| 7 | |
| 8 | /** |
| 9 | * A list of BatchOperationEntry. |
| 10 | * |
| 11 | * @param <T> IBatchOperationTarget. This should be Intent, IFlow, or |
| 12 | * MatchAction. |
| 13 | */ |
| 14 | public class BatchOperation<T extends IBatchOperationTarget> { |
| 15 | private List<BatchOperationEntry> ops; |
| 16 | |
| 17 | /** |
| 18 | * Constructor. |
| 19 | */ |
| 20 | public BatchOperation() { |
| 21 | ops = new LinkedList<BatchOperationEntry>(); |
| 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 | * |
| 34 | * @return the number of operations in this object. |
| 35 | */ |
| 36 | public int size() { |
| 37 | return ops.size(); |
| 38 | } |
| 39 | |
| 40 | /** |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 41 | * Returns an iterator over the operations in this object. |
| 42 | * |
| 43 | * @return an iterator over the operations in this object. |
| 44 | */ |
| 45 | public Iterator<BatchOperationEntry> iterator() { |
| 46 | return ops.iterator(); |
| 47 | } |
| 48 | |
| 49 | /** |
Toshio Koide | cff5d98 | 2014-07-31 11:41:56 -0700 | [diff] [blame^] | 50 | * Returns the operations in this object. |
| 51 | * |
| 52 | * @return the operations in this object. |
| 53 | */ |
| 54 | public List<BatchOperationEntry> getOperations() { |
| 55 | return Collections.unmodifiableList(ops); |
| 56 | } |
| 57 | |
| 58 | /** |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 59 | * Adds an add-operation. |
| 60 | * |
Toshio Koide | 473b1f6 | 2014-07-31 11:38:15 -0700 | [diff] [blame] | 61 | * @param target IBatchOperationTarget object to be added. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 62 | * @return true if succeeded, false otherwise. |
| 63 | */ |
| 64 | public boolean addAddOperation(T target) { |
| 65 | return ops.add(new AddOperation(target)); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Adds a remove-operation. |
| 70 | * |
| 71 | * @param id ID of the target to be removed. |
| 72 | * @return true if succeeded, false otherwise. |
| 73 | */ |
Toshio Koide | 025a915 | 2014-07-21 11:00:34 -0700 | [diff] [blame] | 74 | public boolean addRemoveOperation(BatchOperationTargetId id) { |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 75 | return ops.add(new RemoveOperation(id)); |
| 76 | } |
| 77 | |
| 78 | /** |
Toshio Koide | 473b1f6 | 2014-07-31 11:38:15 -0700 | [diff] [blame] | 79 | * Adds an update-operation. |
| 80 | * <p> |
| 81 | * The existing entry having the same ID with the new target is overwritten. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 82 | * |
Toshio Koide | 473b1f6 | 2014-07-31 11:38:15 -0700 | [diff] [blame] | 83 | * @param target The new target to be used for the update. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 84 | * @return true if succeeded, false otherwise. |
| 85 | */ |
Toshio Koide | 473b1f6 | 2014-07-31 11:38:15 -0700 | [diff] [blame] | 86 | public boolean addUpdateOperation(T target) { |
| 87 | return ops.add(new UpdateOperation(target)); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 88 | } |
| 89 | } |