Sho SHIMIZU
Committed by Thomas Vachuska

Apply Null Object pattern

Change-Id: I9b4d30114b22dcd32b228e4f17bb541beed4ebed
......@@ -23,6 +23,7 @@ import com.google.common.collect.Lists;
import java.util.List;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.flow.FlowRuleOperation.Type.*;
/**
......@@ -32,7 +33,7 @@ import static org.onosproject.net.flow.FlowRuleOperation.Type.*;
public class FlowRuleOperations {
private final List<Set<FlowRuleOperation>> stages;
private final FlowRuleOperationsContext callback; // TODO consider Optional
private final FlowRuleOperationsContext callback;
private FlowRuleOperations(List<Set<FlowRuleOperation>> stages,
FlowRuleOperationsContext cb) {
......@@ -164,7 +165,7 @@ public class FlowRuleOperations {
* @return flow rule operations
*/
public FlowRuleOperations build() {
return build(null);
return build(NullFlowRuleOperationsContext.getInstance());
}
/**
......@@ -174,6 +175,8 @@ public class FlowRuleOperations {
* @return flow rule operations
*/
public FlowRuleOperations build(FlowRuleOperationsContext cb) {
checkNotNull(cb);
closeStage();
return new FlowRuleOperations(listBuilder.build(), cb);
}
......
/*
* 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;
}
}
......@@ -614,7 +614,7 @@ public class FlowRuleManager
public synchronized void run() {
if (stages.size() > 0) {
process(stages.remove(0));
} else if (!hasFailed && fops.callback() != null) {
} else if (!hasFailed) {
fops.callback().onSuccess(fops);
}
}
......@@ -651,13 +651,10 @@ public class FlowRuleManager
operationsService.execute(this);
}
if (fops.callback() != null) {
final FlowRuleOperations.Builder failedOpsBuilder =
FlowRuleOperations.builder();
failures.forEach(failedOpsBuilder::add);
FlowRuleOperations.Builder failedOpsBuilder = FlowRuleOperations.builder();
failures.forEach(failedOpsBuilder::add);
fops.callback().onError(failedOpsBuilder.build());
}
fops.callback().onError(failedOpsBuilder.build());
}
}
......