Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 19 | import java.util.Collections; |
| 20 | import java.util.LinkedList; |
| 21 | import java.util.List; |
| 22 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 25 | /** |
| 26 | * A list of BatchOperationEntry. |
| 27 | * |
| 28 | * @param <T> the enum of operators <br> |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 29 | * This enum must be defined in each sub-classes. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 30 | */ |
| 31 | public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> { |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 32 | |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 33 | private final List<T> ops; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Creates new {@link BatchOperation} object. |
| 37 | */ |
| 38 | public BatchOperation() { |
| 39 | ops = new LinkedList<>(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Creates {@link BatchOperation} object from a list of batch operation |
| 44 | * entries. |
| 45 | * |
| 46 | * @param batchOperations the list of batch operation entries. |
| 47 | */ |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 48 | public BatchOperation(Collection<T> batchOperations) { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 49 | ops = new LinkedList<>(checkNotNull(batchOperations)); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Removes all operations maintained in this object. |
| 54 | */ |
| 55 | public void clear() { |
| 56 | ops.clear(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the number of operations in this object. |
| 61 | * |
| 62 | * @return the number of operations in this object |
| 63 | */ |
| 64 | public int size() { |
| 65 | return ops.size(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the operations in this object. |
| 70 | * |
| 71 | * @return the operations in this object |
| 72 | */ |
| 73 | public List<T> getOperations() { |
| 74 | return Collections.unmodifiableList(ops); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Adds an operation. |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 79 | * FIXME: Brian promises that the Intent Framework |
| 80 | * will not modify the batch operation after it has submitted it. |
| 81 | * Ali would prefer immutablity, but trusts brian for better or |
| 82 | * for worse. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 83 | * |
| 84 | * @param entry the operation to be added |
| 85 | * @return this object if succeeded, null otherwise |
| 86 | */ |
| 87 | public BatchOperation<T> addOperation(T entry) { |
| 88 | return ops.add(entry) ? this : null; |
| 89 | } |
| 90 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Add all operations from another batch to this batch. |
| 93 | * |
| 94 | * @param another another batch |
| 95 | * @return true if success |
| 96 | */ |
| 97 | public boolean addAll(BatchOperation<T> another) { |
| 98 | return ops.addAll(another.getOperations()); |
| 99 | } |
| 100 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public boolean equals(Object o) { |
| 103 | if (this == o) { |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | if (o == null) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if (getClass() != o.getClass()) { |
| 112 | return false; |
| 113 | } |
| 114 | BatchOperation<?> other = (BatchOperation<?>) o; |
| 115 | |
| 116 | return this.ops.equals(other.ops); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public int hashCode() { |
| 121 | return ops.hashCode(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public String toString() { |
| 126 | return ops.toString(); |
| 127 | } |
| 128 | } |