Apply Null Object pattern

Change-Id: I9b4d30114b22dcd32b228e4f17bb541beed4ebed
This commit is contained in:
Sho SHIMIZU 2016-09-01 13:05:56 -07:00 committed by Thomas Vachuska
parent c9e4bb0048
commit ad4f2cd9da
3 changed files with 44 additions and 9 deletions

View File

@ -23,6 +23,7 @@ import com.google.common.collect.Lists;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.flow.FlowRuleOperation.Type.*; import static org.onosproject.net.flow.FlowRuleOperation.Type.*;
/** /**
@ -32,7 +33,7 @@ import static org.onosproject.net.flow.FlowRuleOperation.Type.*;
public class FlowRuleOperations { public class FlowRuleOperations {
private final List<Set<FlowRuleOperation>> stages; private final List<Set<FlowRuleOperation>> stages;
private final FlowRuleOperationsContext callback; // TODO consider Optional private final FlowRuleOperationsContext callback;
private FlowRuleOperations(List<Set<FlowRuleOperation>> stages, private FlowRuleOperations(List<Set<FlowRuleOperation>> stages,
FlowRuleOperationsContext cb) { FlowRuleOperationsContext cb) {
@ -164,7 +165,7 @@ public class FlowRuleOperations {
* @return flow rule operations * @return flow rule operations
*/ */
public FlowRuleOperations build() { public FlowRuleOperations build() {
return build(null); return build(NullFlowRuleOperationsContext.getInstance());
} }
/** /**
@ -174,6 +175,8 @@ public class FlowRuleOperations {
* @return flow rule operations * @return flow rule operations
*/ */
public FlowRuleOperations build(FlowRuleOperationsContext cb) { public FlowRuleOperations build(FlowRuleOperationsContext cb) {
checkNotNull(cb);
closeStage(); closeStage();
return new FlowRuleOperations(listBuilder.build(), cb); return new FlowRuleOperations(listBuilder.build(), cb);
} }

View File

@ -0,0 +1,35 @@
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.net.flow;
/**
* Represents FlowRuleOperations that does nothing on success or on error.
*/
final class NullFlowRuleOperationsContext implements FlowRuleOperationsContext {
private static final FlowRuleOperationsContext INSTANCE = new NullFlowRuleOperationsContext();
private NullFlowRuleOperationsContext() {}
/**
* Returns an instance of this class.
*
* @return instance
*/
public static FlowRuleOperationsContext getInstance() {
return INSTANCE;
}
}

View File

@ -614,7 +614,7 @@ public class FlowRuleManager
public synchronized void run() { public synchronized void run() {
if (stages.size() > 0) { if (stages.size() > 0) {
process(stages.remove(0)); process(stages.remove(0));
} else if (!hasFailed && fops.callback() != null) { } else if (!hasFailed) {
fops.callback().onSuccess(fops); fops.callback().onSuccess(fops);
} }
} }
@ -651,13 +651,10 @@ public class FlowRuleManager
operationsService.execute(this); operationsService.execute(this);
} }
if (fops.callback() != null) { FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
final FlowRuleOperations.Builder failedOpsBuilder = failures.forEach(failedOpsBuilder::add);
FlowRuleOperations.builder();
failures.forEach(failedOpsBuilder::add);
fops.callback().onError(failedOpsBuilder.build()); fops.callback().onError(failedOpsBuilder.build());
}
} }
} }