blob: 38f021195387fb864a60bbcc5d98853db289bbbf [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
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 */
19package org.onlab.onos.net.flow;
Brian O'Connorb876bf12014-10-02 14:59:37 -070020
alshabib902d41b2014-10-07 16:52:05 -070021import static com.google.common.base.Preconditions.checkNotNull;
22
23import java.util.Collection;
Brian O'Connorb876bf12014-10-02 14:59:37 -070024import java.util.Collections;
25import java.util.LinkedList;
26import java.util.List;
27
28/**
29 * A list of BatchOperationEntry.
30 *
31 * @param <T> the enum of operators <br>
toma1d16b62014-10-02 23:45:11 -070032 * This enum must be defined in each sub-classes.
Brian O'Connorb876bf12014-10-02 14:59:37 -070033 */
34public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> {
toma1d16b62014-10-02 23:45:11 -070035
alshabib902d41b2014-10-07 16:52:05 -070036 private final List<T> ops;
Brian O'Connorb876bf12014-10-02 14:59:37 -070037
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 */
alshabib902d41b2014-10-07 16:52:05 -070051 public BatchOperation(Collection<T> batchOperations) {
Brian O'Connorb876bf12014-10-02 14:59:37 -070052 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.
alshabib902d41b2014-10-07 16:52:05 -070082 * 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'Connorb876bf12014-10-02 14:59:37 -070086 *
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}