Committed by
Thomas Vachuska
Apply Null Object pattern
Change-Id: I9b4d30114b22dcd32b228e4f17bb541beed4ebed
Showing
3 changed files
with
42 additions
and
7 deletions
... | @@ -23,6 +23,7 @@ import com.google.common.collect.Lists; | ... | @@ -23,6 +23,7 @@ import com.google.common.collect.Lists; |
23 | import java.util.List; | 23 | import java.util.List; |
24 | import java.util.Set; | 24 | import java.util.Set; |
25 | 25 | ||
26 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
26 | import static org.onosproject.net.flow.FlowRuleOperation.Type.*; | 27 | import static org.onosproject.net.flow.FlowRuleOperation.Type.*; |
27 | 28 | ||
28 | /** | 29 | /** |
... | @@ -32,7 +33,7 @@ import static org.onosproject.net.flow.FlowRuleOperation.Type.*; | ... | @@ -32,7 +33,7 @@ import static org.onosproject.net.flow.FlowRuleOperation.Type.*; |
32 | public class FlowRuleOperations { | 33 | public class FlowRuleOperations { |
33 | 34 | ||
34 | private final List<Set<FlowRuleOperation>> stages; | 35 | private final List<Set<FlowRuleOperation>> stages; |
35 | - private final FlowRuleOperationsContext callback; // TODO consider Optional | 36 | + private final FlowRuleOperationsContext callback; |
36 | 37 | ||
37 | private FlowRuleOperations(List<Set<FlowRuleOperation>> stages, | 38 | private FlowRuleOperations(List<Set<FlowRuleOperation>> stages, |
38 | FlowRuleOperationsContext cb) { | 39 | FlowRuleOperationsContext cb) { |
... | @@ -164,7 +165,7 @@ public class FlowRuleOperations { | ... | @@ -164,7 +165,7 @@ public class FlowRuleOperations { |
164 | * @return flow rule operations | 165 | * @return flow rule operations |
165 | */ | 166 | */ |
166 | public FlowRuleOperations build() { | 167 | public FlowRuleOperations build() { |
167 | - return build(null); | 168 | + return build(NullFlowRuleOperationsContext.getInstance()); |
168 | } | 169 | } |
169 | 170 | ||
170 | /** | 171 | /** |
... | @@ -174,6 +175,8 @@ public class FlowRuleOperations { | ... | @@ -174,6 +175,8 @@ public class FlowRuleOperations { |
174 | * @return flow rule operations | 175 | * @return flow rule operations |
175 | */ | 176 | */ |
176 | public FlowRuleOperations build(FlowRuleOperationsContext cb) { | 177 | public FlowRuleOperations build(FlowRuleOperationsContext cb) { |
178 | + checkNotNull(cb); | ||
179 | + | ||
177 | closeStage(); | 180 | closeStage(); |
178 | return new FlowRuleOperations(listBuilder.build(), cb); | 181 | return new FlowRuleOperations(listBuilder.build(), cb); |
179 | } | 182 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016-present Open Networking Laboratory | ||
3 | + * | ||
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 | ||
7 | + * | ||
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. | ||
15 | + */ | ||
16 | + | ||
17 | +package org.onosproject.net.flow; | ||
18 | + | ||
19 | +/** | ||
20 | + * Represents FlowRuleOperations that does nothing on success or on error. | ||
21 | + */ | ||
22 | +final class NullFlowRuleOperationsContext implements FlowRuleOperationsContext { | ||
23 | + private static final FlowRuleOperationsContext INSTANCE = new NullFlowRuleOperationsContext(); | ||
24 | + | ||
25 | + private NullFlowRuleOperationsContext() {} | ||
26 | + | ||
27 | + /** | ||
28 | + * Returns an instance of this class. | ||
29 | + * | ||
30 | + * @return instance | ||
31 | + */ | ||
32 | + public static FlowRuleOperationsContext getInstance() { | ||
33 | + return INSTANCE; | ||
34 | + } | ||
35 | +} |
... | @@ -614,7 +614,7 @@ public class FlowRuleManager | ... | @@ -614,7 +614,7 @@ public class FlowRuleManager |
614 | public synchronized void run() { | 614 | public synchronized void run() { |
615 | if (stages.size() > 0) { | 615 | if (stages.size() > 0) { |
616 | process(stages.remove(0)); | 616 | process(stages.remove(0)); |
617 | - } else if (!hasFailed && fops.callback() != null) { | 617 | + } else if (!hasFailed) { |
618 | fops.callback().onSuccess(fops); | 618 | fops.callback().onSuccess(fops); |
619 | } | 619 | } |
620 | } | 620 | } |
... | @@ -651,15 +651,12 @@ public class FlowRuleManager | ... | @@ -651,15 +651,12 @@ public class FlowRuleManager |
651 | operationsService.execute(this); | 651 | operationsService.execute(this); |
652 | } | 652 | } |
653 | 653 | ||
654 | - if (fops.callback() != null) { | 654 | + FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder(); |
655 | - final FlowRuleOperations.Builder failedOpsBuilder = | ||
656 | - FlowRuleOperations.builder(); | ||
657 | failures.forEach(failedOpsBuilder::add); | 655 | failures.forEach(failedOpsBuilder::add); |
658 | 656 | ||
659 | fops.callback().onError(failedOpsBuilder.build()); | 657 | fops.callback().onError(failedOpsBuilder.build()); |
660 | } | 658 | } |
661 | } | 659 | } |
662 | - } | ||
663 | 660 | ||
664 | @Override | 661 | @Override |
665 | public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) { | 662 | public Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId) { | ... | ... |
-
Please register or login to post a comment