Committed by
Thomas Vachuska
Apply Null Object pattern
Change-Id: I9b4d30114b22dcd32b228e4f17bb541beed4ebed
Showing
3 changed files
with
44 additions
and
9 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 | +} |
| ... | @@ -593,7 +593,7 @@ public class FlowRuleManager | ... | @@ -593,7 +593,7 @@ public class FlowRuleManager |
| 593 | public synchronized void run() { | 593 | public synchronized void run() { |
| 594 | if (stages.size() > 0) { | 594 | if (stages.size() > 0) { |
| 595 | process(stages.remove(0)); | 595 | process(stages.remove(0)); |
| 596 | - } else if (!hasFailed && fops.callback() != null) { | 596 | + } else if (!hasFailed) { |
| 597 | fops.callback().onSuccess(fops); | 597 | fops.callback().onSuccess(fops); |
| 598 | } | 598 | } |
| 599 | } | 599 | } |
| ... | @@ -630,13 +630,10 @@ public class FlowRuleManager | ... | @@ -630,13 +630,10 @@ public class FlowRuleManager |
| 630 | operationsService.execute(this); | 630 | operationsService.execute(this); |
| 631 | } | 631 | } |
| 632 | 632 | ||
| 633 | - if (fops.callback() != null) { | 633 | + FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder(); |
| 634 | - final FlowRuleOperations.Builder failedOpsBuilder = | 634 | + failures.stream().forEach(failedOpsBuilder::add); |
| 635 | - FlowRuleOperations.builder(); | ||
| 636 | - failures.stream().forEach(failedOpsBuilder::add); | ||
| 637 | 635 | ||
| 638 | - fops.callback().onError(failedOpsBuilder.build()); | 636 | + fops.callback().onError(failedOpsBuilder.build()); |
| 639 | - } | ||
| 640 | } | 637 | } |
| 641 | } | 638 | } |
| 642 | 639 | ... | ... |
-
Please register or login to post a comment