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