blob: 7bc6bf4ce80bb99240b8e0f25e62429de4589160 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
alshabib902d41b2014-10-07 16:52:05 -070018import java.util.Collection;
Brian O'Connorb876bf12014-10-02 14:59:37 -070019import java.util.Collections;
20import java.util.LinkedList;
21import java.util.List;
22
Brian O'Connorfa81eae2014-10-30 13:20:05 -070023import static com.google.common.base.Preconditions.checkNotNull;
24
Brian O'Connorb876bf12014-10-02 14:59:37 -070025/**
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
Brian O'Connorfa81eae2014-10-30 13:20:05 -070091 /**
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'Connorb876bf12014-10-02 14:59:37 -0700101 @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}