Sho SHIMIZU
Committed by Gerrit Code Review

Make use of Optional more idiomatic

Change-Id: I42b3261169e7cb8408f46c5831f72115f77fd779
Showing 22 changed files with 48 additions and 104 deletions
......@@ -256,7 +256,7 @@ public class Olt
CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
TrafficSelector upstream = DefaultTrafficSelector.builder()
.matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
.matchVlanId(defaultVlan.orElse(DEFAULT_VLAN))
.matchInPort(subscriberPort)
.build();
......@@ -276,7 +276,7 @@ public class Olt
TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
.popVlan()
.setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
.setVlanId(defaultVlan.orElse(DEFAULT_VLAN))
.setOutput(subscriberPort)
.build();
......
......@@ -193,21 +193,11 @@ public class PIMInterfaceManager implements PIMInterfaceService {
.withPacketService(packetService)
.withInterface(intf);
if (config.getHelloInterval().isPresent()) {
builder.withHelloInterval(config.getHelloInterval().get());
}
if (config.getHoldTime().isPresent()) {
builder.withHoldTime(config.getHoldTime().get());
}
if (config.getPriority().isPresent()) {
builder.withPriority(config.getPriority().get());
}
if (config.getPropagationDelay().isPresent()) {
builder.withPropagationDelay(config.getPropagationDelay().get());
}
if (config.getOverrideInterval().isPresent()) {
builder.withOverrideInterval(config.getOverrideInterval().get());
}
config.getHelloInterval().ifPresent(builder::withHelloInterval);
config.getHoldTime().ifPresent(builder::withHoldTime);
config.getPriority().ifPresent(builder::withPriority);
config.getPropagationDelay().ifPresent(builder::withPropagationDelay);
config.getOverrideInterval().ifPresent(builder::withOverrideInterval);
return builder.build();
}
......
......@@ -85,7 +85,7 @@ public class BgpConfig extends Config<ApplicationId> {
*/
public BgpSpeakerConfig getSpeakerWithName(String name) {
for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) {
if (speaker.name().isPresent() && speaker.name().get().equals(name)) {
if (speaker.name().filter(name::equals).isPresent()) {
return speaker;
}
}
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.cli.app;
import java.net.URI;
import java.util.Collections;
import java.util.List;
......@@ -75,7 +76,7 @@ public class ApplicationsListCommand extends AbstractShellCommand {
print(FMT, isActive ? "*" : " ",
app.id().id(), app.id().name(), app.version(), app.origin(),
app.category(), app.description(), app.features(),
app.featuresRepo().isPresent() ? app.featuresRepo().get().toString() : "",
app.featuresRepo().map(URI::toString).orElse(""),
app.requiredApps(), app.permissions(), app.url());
}
}
......
......@@ -105,7 +105,7 @@ public final class DefaultPacketRequest implements PacketRequest {
.add("priority", priority)
.add("appId", appId)
.add("nodeId", nodeId)
.add("applies to", deviceId.isPresent() ? deviceId.get() : "all")
.add("applies to", deviceId.map(DeviceId::toString).orElse("all"))
.toString();
}
}
......
......@@ -23,6 +23,8 @@ import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.Application;
import java.net.URI;
import static com.google.common.base.Preconditions.checkNotNull;
/**
......@@ -52,8 +54,7 @@ public final class ApplicationCodec extends JsonCodec<Application> {
.put("readme", StringEscapeUtils.escapeJson(app.readme()))
.put("origin", app.origin())
.put("url", app.url())
.put("featuresRepo", app.featuresRepo().isPresent() ?
app.featuresRepo().get().toString() : "")
.put("featuresRepo", app.featuresRepo().map(URI::toString).orElse(""))
.put("state", service.getState(app.id()).toString());
result.set("features", features);
......
......@@ -148,7 +148,7 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
.filter(d -> matches(d, mfr, hw, sw)).findFirst();
// If no matching driver is found, return default.
return optional.isPresent() ? optional.get() : drivers.get(DEFAULT);
return optional.orElse(drivers.get(DEFAULT));
}
// Matches the given driver using ERE matching against the given criteria.
......
......@@ -129,18 +129,16 @@ public class EdgeManager
@Override
public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) {
TrafficTreatment.Builder builder = treatment.isPresent() ?
DefaultTrafficTreatment.builder(treatment.get()) :
DefaultTrafficTreatment.builder();
TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
.orElse(DefaultTrafficTreatment.builder());
getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
}
@Override
public void emitPacket(DeviceId deviceId, ByteBuffer data,
Optional<TrafficTreatment> treatment) {
TrafficTreatment.Builder builder = treatment.isPresent() ?
DefaultTrafficTreatment.builder(treatment.get()) :
DefaultTrafficTreatment.builder();
TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
.orElse(DefaultTrafficTreatment.builder());
getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data)));
}
......
......@@ -56,11 +56,11 @@ class Compiling implements IntentProcessPhase {
try {
List<Intent> compiled = processor.compile(data.intent(),
//TODO consider passing an optional here in the future
stored.isPresent() ? stored.get().installables() : null);
stored.map(IntentData::installables).orElse(null));
return Optional.of(new Installing(processor, new IntentData(data, compiled), stored));
} catch (IntentException e) {
log.debug("Unable to compile intent {} due to: {}", data.intent(), e);
if (stored.isPresent() && !stored.get().installables().isEmpty()) {
if (stored.filter(x -> x.installables().isEmpty()).isPresent()) {
// removing orphaned flows and deallocating resources
return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables())));
} else {
......
......@@ -121,11 +121,8 @@ public class StoragePartition extends DefaultPartition implements Managed<Storag
}
private CompletableFuture<Void> closeServer() {
if (server.isPresent()) {
return server.get().close();
} else {
return CompletableFuture.completedFuture(null);
}
return server.map(StoragePartitionServer::close)
.orElse(CompletableFuture.completedFuture(null));
}
private CompletableFuture<Void> closeClient() {
......
......@@ -482,15 +482,11 @@ public class CentecV350Pipeline extends AbstractHandlerBehaviour implements Pipe
}
private void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
private void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
private void initializePipeline() {
......
......@@ -156,16 +156,13 @@ public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour impleme
flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
@Override
public void onSuccess(FlowRuleOperations ops) {
if (objective.context().isPresent()) {
objective.context().get().onSuccess(objective);
}
objective.context().ifPresent(context -> context.onSuccess(objective));
}
@Override
public void onError(FlowRuleOperations ops) {
if (objective.context().isPresent()) {
objective.context().get().onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED);
}
objective.context()
.ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED));
}
}));
}
......
......@@ -998,14 +998,10 @@ public class OFDPA2Pipeline extends AbstractHandlerBehaviour implements Pipeline
}
protected static void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
protected static void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
}
......
......@@ -477,15 +477,11 @@ public class OVSCorsaPipeline extends AbstractHandlerBehaviour implements Pipeli
}
protected void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
protected void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
protected void initializePipeline() {
......
......@@ -623,15 +623,11 @@ public class OltPipeline extends AbstractHandlerBehaviour implements Pipeliner {
private void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
private void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
......
......@@ -367,14 +367,10 @@ public class OpenVSwitchPipeline extends DefaultSingleTablePipeline
}
private void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
private void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
}
......
......@@ -273,15 +273,11 @@ public class OpenstackPipeline extends DefaultSingleTablePipeline
private void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
private void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
}
......
......@@ -450,15 +450,11 @@ public class PicaPipeline extends AbstractHandlerBehaviour implements Pipeliner
}
private void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
private void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
private void initializePipeline() {
......
......@@ -179,15 +179,11 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
}
private void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
private void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
private void initializePipeline() {
......
......@@ -998,15 +998,11 @@ public class SpringOpenTTP extends AbstractHandlerBehaviour
}
private void pass(Objective obj) {
if (obj.context().isPresent()) {
obj.context().get().onSuccess(obj);
}
obj.context().ifPresent(context -> context.onSuccess(obj));
}
protected void fail(Objective obj, ObjectiveError error) {
if (obj.context().isPresent()) {
obj.context().get().onError(obj, error);
}
obj.context().ifPresent(context -> context.onError(obj, error));
}
private class InnerGroupListener implements GroupListener {
......
......@@ -109,7 +109,7 @@ public class InterfaceManager extends ListenerRegistry<InterfaceEvent, Interface
.filter(i -> i.name().equals(name))
.findAny();
return intf.isPresent() ? intf.get() : null;
return intf.orElse(null);
}
@Override
......@@ -142,11 +142,7 @@ public class InterfaceManager extends ListenerRegistry<InterfaceEvent, Interface
.anyMatch(intfIp -> intfIp.subnetAddress().contains(ip)))
.findFirst();
if (match.isPresent()) {
return match.get();
}
return null;
return match.orElse(null);
}
@Override
......
......@@ -211,7 +211,7 @@ public class OnosSwaggerMojo extends AbstractMojo {
private JavaAnnotation getPathAnnotation(JavaClass javaClass) {
Optional<JavaAnnotation> optional = javaClass.getAnnotations()
.stream().filter(a -> a.getType().getName().equals(PATH)).findAny();
return optional.isPresent() ? optional.get() : null;
return optional.orElse(null);
}
// Checks whether a class's methods are REST methods and then places all the
......@@ -375,7 +375,7 @@ public class OnosSwaggerMojo extends AbstractMojo {
Optional<JavaAnnotation> optional = javaParameter.getAnnotations().stream().filter(
annotation -> annotation.getType().getName().equals(PATH_PARAM) ||
annotation.getType().getName().equals(QUERY_PARAM)).findAny();
JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;
JavaAnnotation pathType = optional.orElse(null);
String annotationName = javaParameter.getName();
......