blob: 16ce1844a02b05d2fbcdd9f383c416dceaf75359 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
16package org.onlab.onos.net.flow;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
alshabib902d41b2014-10-07 16:52:05 -070018import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Collection;
Brian O'Connorb876bf12014-10-02 14:59:37 -070021import java.util.Collections;
22import java.util.LinkedList;
23import java.util.List;
24
25/**
26 * A list of BatchOperationEntry.
27 *
28 * @param <T> the enum of operators <br>
toma1d16b62014-10-02 23:45:11 -070029 * This enum must be defined in each sub-classes.
Brian O'Connorb876bf12014-10-02 14:59:37 -070030 */
31public abstract class BatchOperation<T extends BatchOperationEntry<?, ?>> {
toma1d16b62014-10-02 23:45:11 -070032
alshabib902d41b2014-10-07 16:52:05 -070033 private final List<T> ops;
Brian O'Connorb876bf12014-10-02 14:59:37 -070034
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 */
alshabib902d41b2014-10-07 16:52:05 -070048 public BatchOperation(Collection<T> batchOperations) {
Brian O'Connorb876bf12014-10-02 14:59:37 -070049 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.
alshabib902d41b2014-10-07 16:52:05 -070079 * 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'Connorb876bf12014-10-02 14:59:37 -070083 *
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
91 @Override
92 public boolean equals(Object o) {
93 if (this == o) {
94 return true;
95 }
96
97 if (o == null) {
98 return false;
99 }
100
101 if (getClass() != o.getClass()) {
102 return false;
103 }
104 BatchOperation<?> other = (BatchOperation<?>) o;
105
106 return this.ops.equals(other.ops);
107 }
108
109 @Override
110 public int hashCode() {
111 return ops.hashCode();
112 }
113
114 @Override
115 public String toString() {
116 return ops.toString();
117 }
118}