From ef7e290e208f16ab77e37fe4ffde7be2f1ec2252 Mon Sep 17 00:00:00 2001 From: Sho SHIMIZU Date: Fri, 12 Feb 2016 18:38:29 -0800 Subject: [PATCH] Make use of Optional more idiomatic Change-Id: I42b3261169e7cb8408f46c5831f72115f77fd779 --- .../java/org/onosproject/olt/impl/Olt.java | 4 ++-- .../pim/impl/PIMInterfaceManager.java | 20 +++++-------------- .../onosproject/routing/config/BgpConfig.java | 2 +- .../cli/app/ApplicationsListCommand.java | 3 ++- .../net/packet/DefaultPacketRequest.java | 4 ++-- .../codec/impl/ApplicationCodec.java | 5 +++-- .../net/driver/impl/DriverManager.java | 2 +- .../net/edgeservice/impl/EdgeManager.java | 10 ++++------ .../net/intent/impl/phase/Compiling.java | 4 ++-- .../primitives/impl/StoragePartition.java | 7 ++----- .../driver/pipeline/CentecV350Pipeline.java | 8 ++------ .../pipeline/DefaultSingleTablePipeline.java | 9 +++------ .../driver/pipeline/OFDPA2Pipeline.java | 8 ++------ .../driver/pipeline/OVSCorsaPipeline.java | 8 ++------ .../driver/pipeline/OltPipeline.java | 8 ++------ .../driver/pipeline/OpenVSwitchPipeline.java | 8 ++------ .../driver/pipeline/OpenstackPipeline.java | 8 ++------ .../driver/pipeline/PicaPipeline.java | 8 ++------ .../driver/pipeline/SoftRouterPipeline.java | 8 ++------ .../driver/pipeline/SpringOpenTTP.java | 8 ++------ .../net/intf/impl/InterfaceManager.java | 8 ++------ .../onosproject/maven/OnosSwaggerMojo.java | 4 ++-- 22 files changed, 49 insertions(+), 105 deletions(-) diff --git a/apps/olt/app/src/main/java/org/onosproject/olt/impl/Olt.java b/apps/olt/app/src/main/java/org/onosproject/olt/impl/Olt.java index 16e8a9e176..bfc05c878b 100644 --- a/apps/olt/app/src/main/java/org/onosproject/olt/impl/Olt.java +++ b/apps/olt/app/src/main/java/org/onosproject/olt/impl/Olt.java @@ -256,7 +256,7 @@ public class Olt CompletableFuture 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(); diff --git a/apps/pim/src/main/java/org/onosproject/pim/impl/PIMInterfaceManager.java b/apps/pim/src/main/java/org/onosproject/pim/impl/PIMInterfaceManager.java index 248a233601..9f56f10758 100644 --- a/apps/pim/src/main/java/org/onosproject/pim/impl/PIMInterfaceManager.java +++ b/apps/pim/src/main/java/org/onosproject/pim/impl/PIMInterfaceManager.java @@ -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(); } diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java b/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java index 209b0fb838..dc1cc81c35 100644 --- a/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java +++ b/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java @@ -85,7 +85,7 @@ public class BgpConfig extends Config { */ 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; } } diff --git a/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java index 9efa612462..2f82f1878b 100644 --- a/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java +++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java @@ -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()); } } diff --git a/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java b/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java index 3bddee4cb5..07d2440f8d 100644 --- a/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java +++ b/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java @@ -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(); } -} \ No newline at end of file +} diff --git a/core/common/src/main/java/org/onosproject/codec/impl/ApplicationCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/ApplicationCodec.java index 4886e604c5..18ecedb3b6 100644 --- a/core/common/src/main/java/org/onosproject/codec/impl/ApplicationCodec.java +++ b/core/common/src/main/java/org/onosproject/codec/impl/ApplicationCodec.java @@ -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 { .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); diff --git a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java index 53a9a68191..c16f5580c1 100644 --- a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java +++ b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java @@ -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. diff --git a/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java b/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java index cd7335d62e..7340fc5b8b 100644 --- a/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java +++ b/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java @@ -129,18 +129,16 @@ public class EdgeManager @Override public void emitPacket(ByteBuffer data, Optional 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 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))); } diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Compiling.java b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Compiling.java index 6cef3a4df0..1f5eb380e4 100644 --- a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Compiling.java +++ b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Compiling.java @@ -56,11 +56,11 @@ class Compiling implements IntentProcessPhase { try { List 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 { diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/StoragePartition.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/StoragePartition.java index acf88fa47b..d8fb580b12 100644 --- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/StoragePartition.java +++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/StoragePartition.java @@ -121,11 +121,8 @@ public class StoragePartition extends DefaultPartition implements Managed closeServer() { - if (server.isPresent()) { - return server.get().close(); - } else { - return CompletableFuture.completedFuture(null); - } + return server.map(StoragePartitionServer::close) + .orElse(CompletableFuture.completedFuture(null)); } private CompletableFuture closeClient() { diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CentecV350Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CentecV350Pipeline.java index 2c6e24cf24..9203a00ebf 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CentecV350Pipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CentecV350Pipeline.java @@ -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() { diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java index 7e22501f13..baa08b8e45 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java @@ -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)); } })); } diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OFDPA2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OFDPA2Pipeline.java index f949bdffc8..2f58bfb2dc 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OFDPA2Pipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OFDPA2Pipeline.java @@ -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)); } } diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java index 46a360b42c..7e9dfec747 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java @@ -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() { diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OltPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OltPipeline.java index 211ed7de2a..f59c527647 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OltPipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OltPipeline.java @@ -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)); } diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenVSwitchPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenVSwitchPipeline.java index 9e24a1761c..1a7f576a04 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenVSwitchPipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenVSwitchPipeline.java @@ -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)); } } diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenstackPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenstackPipeline.java index 7b1a1562b4..a8b386168d 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenstackPipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenstackPipeline.java @@ -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)); } } diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/PicaPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/PicaPipeline.java index 6cb5b82ac9..14b729ce71 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/PicaPipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/PicaPipeline.java @@ -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() { diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/SoftRouterPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/SoftRouterPipeline.java index be1c00d7bd..55e8bbf272 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/SoftRouterPipeline.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/SoftRouterPipeline.java @@ -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() { diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/SpringOpenTTP.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/SpringOpenTTP.java index 1277c75f3c..5c65e11a83 100644 --- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/SpringOpenTTP.java +++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/SpringOpenTTP.java @@ -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 { diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java index 9da21778e6..d297080ebf 100644 --- a/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java +++ b/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java @@ -109,7 +109,7 @@ public class InterfaceManager extends ListenerRegistry 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 intfIp.subnetAddress().contains(ip))) .findFirst(); - if (match.isPresent()) { - return match.get(); - } - - return null; + return match.orElse(null); } @Override diff --git a/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java b/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java index 4618cd06e3..6d9ff13466 100644 --- a/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java +++ b/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java @@ -211,7 +211,7 @@ public class OnosSwaggerMojo extends AbstractMojo { private JavaAnnotation getPathAnnotation(JavaClass javaClass) { Optional 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 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();