Sho SHIMIZU
Committed by Gerrit Code Review

Make use of Optional more idiomatic

Change-Id: I42b3261169e7cb8408f46c5831f72115f77fd779
Showing 22 changed files with 49 additions and 105 deletions
...@@ -256,7 +256,7 @@ public class Olt ...@@ -256,7 +256,7 @@ public class Olt
256 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture(); 256 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
257 257
258 TrafficSelector upstream = DefaultTrafficSelector.builder() 258 TrafficSelector upstream = DefaultTrafficSelector.builder()
259 - .matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN) 259 + .matchVlanId(defaultVlan.orElse(DEFAULT_VLAN))
260 .matchInPort(subscriberPort) 260 .matchInPort(subscriberPort)
261 .build(); 261 .build();
262 262
...@@ -276,7 +276,7 @@ public class Olt ...@@ -276,7 +276,7 @@ public class Olt
276 276
277 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder() 277 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
278 .popVlan() 278 .popVlan()
279 - .setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN) 279 + .setVlanId(defaultVlan.orElse(DEFAULT_VLAN))
280 .setOutput(subscriberPort) 280 .setOutput(subscriberPort)
281 .build(); 281 .build();
282 282
......
...@@ -193,21 +193,11 @@ public class PIMInterfaceManager implements PIMInterfaceService { ...@@ -193,21 +193,11 @@ public class PIMInterfaceManager implements PIMInterfaceService {
193 .withPacketService(packetService) 193 .withPacketService(packetService)
194 .withInterface(intf); 194 .withInterface(intf);
195 195
196 - if (config.getHelloInterval().isPresent()) { 196 + config.getHelloInterval().ifPresent(builder::withHelloInterval);
197 - builder.withHelloInterval(config.getHelloInterval().get()); 197 + config.getHoldTime().ifPresent(builder::withHoldTime);
198 - } 198 + config.getPriority().ifPresent(builder::withPriority);
199 - if (config.getHoldTime().isPresent()) { 199 + config.getPropagationDelay().ifPresent(builder::withPropagationDelay);
200 - builder.withHoldTime(config.getHoldTime().get()); 200 + config.getOverrideInterval().ifPresent(builder::withOverrideInterval);
201 - }
202 - if (config.getPriority().isPresent()) {
203 - builder.withPriority(config.getPriority().get());
204 - }
205 - if (config.getPropagationDelay().isPresent()) {
206 - builder.withPropagationDelay(config.getPropagationDelay().get());
207 - }
208 - if (config.getOverrideInterval().isPresent()) {
209 - builder.withOverrideInterval(config.getOverrideInterval().get());
210 - }
211 201
212 return builder.build(); 202 return builder.build();
213 } 203 }
......
...@@ -85,7 +85,7 @@ public class BgpConfig extends Config<ApplicationId> { ...@@ -85,7 +85,7 @@ public class BgpConfig extends Config<ApplicationId> {
85 */ 85 */
86 public BgpSpeakerConfig getSpeakerWithName(String name) { 86 public BgpSpeakerConfig getSpeakerWithName(String name) {
87 for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) { 87 for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) {
88 - if (speaker.name().isPresent() && speaker.name().get().equals(name)) { 88 + if (speaker.name().filter(name::equals).isPresent()) {
89 return speaker; 89 return speaker;
90 } 90 }
91 } 91 }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 */ 15 */
16 package org.onosproject.cli.app; 16 package org.onosproject.cli.app;
17 17
18 +import java.net.URI;
18 import java.util.Collections; 19 import java.util.Collections;
19 import java.util.List; 20 import java.util.List;
20 21
...@@ -75,7 +76,7 @@ public class ApplicationsListCommand extends AbstractShellCommand { ...@@ -75,7 +76,7 @@ public class ApplicationsListCommand extends AbstractShellCommand {
75 print(FMT, isActive ? "*" : " ", 76 print(FMT, isActive ? "*" : " ",
76 app.id().id(), app.id().name(), app.version(), app.origin(), 77 app.id().id(), app.id().name(), app.version(), app.origin(),
77 app.category(), app.description(), app.features(), 78 app.category(), app.description(), app.features(),
78 - app.featuresRepo().isPresent() ? app.featuresRepo().get().toString() : "", 79 + app.featuresRepo().map(URI::toString).orElse(""),
79 app.requiredApps(), app.permissions(), app.url()); 80 app.requiredApps(), app.permissions(), app.url());
80 } 81 }
81 } 82 }
......
...@@ -105,7 +105,7 @@ public final class DefaultPacketRequest implements PacketRequest { ...@@ -105,7 +105,7 @@ public final class DefaultPacketRequest implements PacketRequest {
105 .add("priority", priority) 105 .add("priority", priority)
106 .add("appId", appId) 106 .add("appId", appId)
107 .add("nodeId", nodeId) 107 .add("nodeId", nodeId)
108 - .add("applies to", deviceId.isPresent() ? deviceId.get() : "all") 108 + .add("applies to", deviceId.map(DeviceId::toString).orElse("all"))
109 .toString(); 109 .toString();
110 } 110 }
111 -}
...\ No newline at end of file ...\ No newline at end of file
111 +}
......
...@@ -23,6 +23,8 @@ import org.onosproject.codec.CodecContext; ...@@ -23,6 +23,8 @@ import org.onosproject.codec.CodecContext;
23 import org.onosproject.codec.JsonCodec; 23 import org.onosproject.codec.JsonCodec;
24 import org.onosproject.core.Application; 24 import org.onosproject.core.Application;
25 25
26 +import java.net.URI;
27 +
26 import static com.google.common.base.Preconditions.checkNotNull; 28 import static com.google.common.base.Preconditions.checkNotNull;
27 29
28 /** 30 /**
...@@ -52,8 +54,7 @@ public final class ApplicationCodec extends JsonCodec<Application> { ...@@ -52,8 +54,7 @@ public final class ApplicationCodec extends JsonCodec<Application> {
52 .put("readme", StringEscapeUtils.escapeJson(app.readme())) 54 .put("readme", StringEscapeUtils.escapeJson(app.readme()))
53 .put("origin", app.origin()) 55 .put("origin", app.origin())
54 .put("url", app.url()) 56 .put("url", app.url())
55 - .put("featuresRepo", app.featuresRepo().isPresent() ? 57 + .put("featuresRepo", app.featuresRepo().map(URI::toString).orElse(""))
56 - app.featuresRepo().get().toString() : "")
57 .put("state", service.getState(app.id()).toString()); 58 .put("state", service.getState(app.id()).toString());
58 59
59 result.set("features", features); 60 result.set("features", features);
......
...@@ -148,7 +148,7 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS ...@@ -148,7 +148,7 @@ public class DriverManager extends DefaultDriverProvider implements DriverAdminS
148 .filter(d -> matches(d, mfr, hw, sw)).findFirst(); 148 .filter(d -> matches(d, mfr, hw, sw)).findFirst();
149 149
150 // If no matching driver is found, return default. 150 // If no matching driver is found, return default.
151 - return optional.isPresent() ? optional.get() : drivers.get(DEFAULT); 151 + return optional.orElse(drivers.get(DEFAULT));
152 } 152 }
153 153
154 // Matches the given driver using ERE matching against the given criteria. 154 // Matches the given driver using ERE matching against the given criteria.
......
...@@ -129,18 +129,16 @@ public class EdgeManager ...@@ -129,18 +129,16 @@ public class EdgeManager
129 129
130 @Override 130 @Override
131 public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) { 131 public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) {
132 - TrafficTreatment.Builder builder = treatment.isPresent() ? 132 + TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
133 - DefaultTrafficTreatment.builder(treatment.get()) : 133 + .orElse(DefaultTrafficTreatment.builder());
134 - DefaultTrafficTreatment.builder();
135 getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data))); 134 getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
136 } 135 }
137 136
138 @Override 137 @Override
139 public void emitPacket(DeviceId deviceId, ByteBuffer data, 138 public void emitPacket(DeviceId deviceId, ByteBuffer data,
140 Optional<TrafficTreatment> treatment) { 139 Optional<TrafficTreatment> treatment) {
141 - TrafficTreatment.Builder builder = treatment.isPresent() ? 140 + TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
142 - DefaultTrafficTreatment.builder(treatment.get()) : 141 + .orElse(DefaultTrafficTreatment.builder());
143 - DefaultTrafficTreatment.builder();
144 getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data))); 142 getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data)));
145 } 143 }
146 144
......
...@@ -56,11 +56,11 @@ class Compiling implements IntentProcessPhase { ...@@ -56,11 +56,11 @@ class Compiling implements IntentProcessPhase {
56 try { 56 try {
57 List<Intent> compiled = processor.compile(data.intent(), 57 List<Intent> compiled = processor.compile(data.intent(),
58 //TODO consider passing an optional here in the future 58 //TODO consider passing an optional here in the future
59 - stored.isPresent() ? stored.get().installables() : null); 59 + stored.map(IntentData::installables).orElse(null));
60 return Optional.of(new Installing(processor, new IntentData(data, compiled), stored)); 60 return Optional.of(new Installing(processor, new IntentData(data, compiled), stored));
61 } catch (IntentException e) { 61 } catch (IntentException e) {
62 log.debug("Unable to compile intent {} due to: {}", data.intent(), e); 62 log.debug("Unable to compile intent {} due to: {}", data.intent(), e);
63 - if (stored.isPresent() && !stored.get().installables().isEmpty()) { 63 + if (stored.filter(x -> x.installables().isEmpty()).isPresent()) {
64 // removing orphaned flows and deallocating resources 64 // removing orphaned flows and deallocating resources
65 return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables()))); 65 return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables())));
66 } else { 66 } else {
......
...@@ -121,11 +121,8 @@ public class StoragePartition extends DefaultPartition implements Managed<Storag ...@@ -121,11 +121,8 @@ public class StoragePartition extends DefaultPartition implements Managed<Storag
121 } 121 }
122 122
123 private CompletableFuture<Void> closeServer() { 123 private CompletableFuture<Void> closeServer() {
124 - if (server.isPresent()) { 124 + return server.map(StoragePartitionServer::close)
125 - return server.get().close(); 125 + .orElse(CompletableFuture.completedFuture(null));
126 - } else {
127 - return CompletableFuture.completedFuture(null);
128 - }
129 } 126 }
130 127
131 private CompletableFuture<Void> closeClient() { 128 private CompletableFuture<Void> closeClient() {
......
...@@ -482,15 +482,11 @@ public class CentecV350Pipeline extends AbstractHandlerBehaviour implements Pipe ...@@ -482,15 +482,11 @@ public class CentecV350Pipeline extends AbstractHandlerBehaviour implements Pipe
482 } 482 }
483 483
484 private void pass(Objective obj) { 484 private void pass(Objective obj) {
485 - if (obj.context().isPresent()) { 485 + obj.context().ifPresent(context -> context.onSuccess(obj));
486 - obj.context().get().onSuccess(obj);
487 - }
488 } 486 }
489 487
490 private void fail(Objective obj, ObjectiveError error) { 488 private void fail(Objective obj, ObjectiveError error) {
491 - if (obj.context().isPresent()) { 489 + obj.context().ifPresent(context -> context.onError(obj, error));
492 - obj.context().get().onError(obj, error);
493 - }
494 } 490 }
495 491
496 private void initializePipeline() { 492 private void initializePipeline() {
......
...@@ -156,16 +156,13 @@ public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour impleme ...@@ -156,16 +156,13 @@ public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour impleme
156 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { 156 flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
157 @Override 157 @Override
158 public void onSuccess(FlowRuleOperations ops) { 158 public void onSuccess(FlowRuleOperations ops) {
159 - if (objective.context().isPresent()) { 159 + objective.context().ifPresent(context -> context.onSuccess(objective));
160 - objective.context().get().onSuccess(objective);
161 - }
162 } 160 }
163 161
164 @Override 162 @Override
165 public void onError(FlowRuleOperations ops) { 163 public void onError(FlowRuleOperations ops) {
166 - if (objective.context().isPresent()) { 164 + objective.context()
167 - objective.context().get().onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED); 165 + .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED));
168 - }
169 } 166 }
170 })); 167 }));
171 } 168 }
......
...@@ -998,14 +998,10 @@ public class OFDPA2Pipeline extends AbstractHandlerBehaviour implements Pipeline ...@@ -998,14 +998,10 @@ public class OFDPA2Pipeline extends AbstractHandlerBehaviour implements Pipeline
998 } 998 }
999 999
1000 protected static void pass(Objective obj) { 1000 protected static void pass(Objective obj) {
1001 - if (obj.context().isPresent()) { 1001 + obj.context().ifPresent(context -> context.onSuccess(obj));
1002 - obj.context().get().onSuccess(obj);
1003 - }
1004 } 1002 }
1005 1003
1006 protected static void fail(Objective obj, ObjectiveError error) { 1004 protected static void fail(Objective obj, ObjectiveError error) {
1007 - if (obj.context().isPresent()) { 1005 + obj.context().ifPresent(context -> context.onError(obj, error));
1008 - obj.context().get().onError(obj, error);
1009 - }
1010 } 1006 }
1011 } 1007 }
......
...@@ -477,15 +477,11 @@ public class OVSCorsaPipeline extends AbstractHandlerBehaviour implements Pipeli ...@@ -477,15 +477,11 @@ public class OVSCorsaPipeline extends AbstractHandlerBehaviour implements Pipeli
477 } 477 }
478 478
479 protected void pass(Objective obj) { 479 protected void pass(Objective obj) {
480 - if (obj.context().isPresent()) { 480 + obj.context().ifPresent(context -> context.onSuccess(obj));
481 - obj.context().get().onSuccess(obj);
482 - }
483 } 481 }
484 482
485 protected void fail(Objective obj, ObjectiveError error) { 483 protected void fail(Objective obj, ObjectiveError error) {
486 - if (obj.context().isPresent()) { 484 + obj.context().ifPresent(context -> context.onError(obj, error));
487 - obj.context().get().onError(obj, error);
488 - }
489 } 485 }
490 486
491 protected void initializePipeline() { 487 protected void initializePipeline() {
......
...@@ -623,15 +623,11 @@ public class OltPipeline extends AbstractHandlerBehaviour implements Pipeliner { ...@@ -623,15 +623,11 @@ public class OltPipeline extends AbstractHandlerBehaviour implements Pipeliner {
623 623
624 624
625 private void fail(Objective obj, ObjectiveError error) { 625 private void fail(Objective obj, ObjectiveError error) {
626 - if (obj.context().isPresent()) { 626 + obj.context().ifPresent(context -> context.onError(obj, error));
627 - obj.context().get().onError(obj, error);
628 - }
629 } 627 }
630 628
631 private void pass(Objective obj) { 629 private void pass(Objective obj) {
632 - if (obj.context().isPresent()) { 630 + obj.context().ifPresent(context -> context.onSuccess(obj));
633 - obj.context().get().onSuccess(obj);
634 - }
635 } 631 }
636 632
637 633
......
...@@ -367,14 +367,10 @@ public class OpenVSwitchPipeline extends DefaultSingleTablePipeline ...@@ -367,14 +367,10 @@ public class OpenVSwitchPipeline extends DefaultSingleTablePipeline
367 } 367 }
368 368
369 private void fail(Objective obj, ObjectiveError error) { 369 private void fail(Objective obj, ObjectiveError error) {
370 - if (obj.context().isPresent()) { 370 + obj.context().ifPresent(context -> context.onError(obj, error));
371 - obj.context().get().onError(obj, error);
372 - }
373 } 371 }
374 372
375 private void pass(Objective obj) { 373 private void pass(Objective obj) {
376 - if (obj.context().isPresent()) { 374 + obj.context().ifPresent(context -> context.onSuccess(obj));
377 - obj.context().get().onSuccess(obj);
378 - }
379 } 375 }
380 } 376 }
......
...@@ -273,15 +273,11 @@ public class OpenstackPipeline extends DefaultSingleTablePipeline ...@@ -273,15 +273,11 @@ public class OpenstackPipeline extends DefaultSingleTablePipeline
273 273
274 274
275 private void pass(Objective obj) { 275 private void pass(Objective obj) {
276 - if (obj.context().isPresent()) { 276 + obj.context().ifPresent(context -> context.onSuccess(obj));
277 - obj.context().get().onSuccess(obj);
278 - }
279 } 277 }
280 278
281 private void fail(Objective obj, ObjectiveError error) { 279 private void fail(Objective obj, ObjectiveError error) {
282 - if (obj.context().isPresent()) { 280 + obj.context().ifPresent(context -> context.onError(obj, error));
283 - obj.context().get().onError(obj, error);
284 - }
285 } 281 }
286 } 282 }
287 283
......
...@@ -450,15 +450,11 @@ public class PicaPipeline extends AbstractHandlerBehaviour implements Pipeliner ...@@ -450,15 +450,11 @@ public class PicaPipeline extends AbstractHandlerBehaviour implements Pipeliner
450 } 450 }
451 451
452 private void pass(Objective obj) { 452 private void pass(Objective obj) {
453 - if (obj.context().isPresent()) { 453 + obj.context().ifPresent(context -> context.onSuccess(obj));
454 - obj.context().get().onSuccess(obj);
455 - }
456 } 454 }
457 455
458 private void fail(Objective obj, ObjectiveError error) { 456 private void fail(Objective obj, ObjectiveError error) {
459 - if (obj.context().isPresent()) { 457 + obj.context().ifPresent(context -> context.onError(obj, error));
460 - obj.context().get().onError(obj, error);
461 - }
462 } 458 }
463 459
464 private void initializePipeline() { 460 private void initializePipeline() {
......
...@@ -179,15 +179,11 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe ...@@ -179,15 +179,11 @@ public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipe
179 } 179 }
180 180
181 private void pass(Objective obj) { 181 private void pass(Objective obj) {
182 - if (obj.context().isPresent()) { 182 + obj.context().ifPresent(context -> context.onSuccess(obj));
183 - obj.context().get().onSuccess(obj);
184 - }
185 } 183 }
186 184
187 private void fail(Objective obj, ObjectiveError error) { 185 private void fail(Objective obj, ObjectiveError error) {
188 - if (obj.context().isPresent()) { 186 + obj.context().ifPresent(context -> context.onError(obj, error));
189 - obj.context().get().onError(obj, error);
190 - }
191 } 187 }
192 188
193 private void initializePipeline() { 189 private void initializePipeline() {
......
...@@ -998,15 +998,11 @@ public class SpringOpenTTP extends AbstractHandlerBehaviour ...@@ -998,15 +998,11 @@ public class SpringOpenTTP extends AbstractHandlerBehaviour
998 } 998 }
999 999
1000 private void pass(Objective obj) { 1000 private void pass(Objective obj) {
1001 - if (obj.context().isPresent()) { 1001 + obj.context().ifPresent(context -> context.onSuccess(obj));
1002 - obj.context().get().onSuccess(obj);
1003 - }
1004 } 1002 }
1005 1003
1006 protected void fail(Objective obj, ObjectiveError error) { 1004 protected void fail(Objective obj, ObjectiveError error) {
1007 - if (obj.context().isPresent()) { 1005 + obj.context().ifPresent(context -> context.onError(obj, error));
1008 - obj.context().get().onError(obj, error);
1009 - }
1010 } 1006 }
1011 1007
1012 private class InnerGroupListener implements GroupListener { 1008 private class InnerGroupListener implements GroupListener {
......
...@@ -109,7 +109,7 @@ public class InterfaceManager extends ListenerRegistry<InterfaceEvent, Interface ...@@ -109,7 +109,7 @@ public class InterfaceManager extends ListenerRegistry<InterfaceEvent, Interface
109 .filter(i -> i.name().equals(name)) 109 .filter(i -> i.name().equals(name))
110 .findAny(); 110 .findAny();
111 111
112 - return intf.isPresent() ? intf.get() : null; 112 + return intf.orElse(null);
113 } 113 }
114 114
115 @Override 115 @Override
...@@ -142,11 +142,7 @@ public class InterfaceManager extends ListenerRegistry<InterfaceEvent, Interface ...@@ -142,11 +142,7 @@ public class InterfaceManager extends ListenerRegistry<InterfaceEvent, Interface
142 .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip))) 142 .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip)))
143 .findFirst(); 143 .findFirst();
144 144
145 - if (match.isPresent()) { 145 + return match.orElse(null);
146 - return match.get();
147 - }
148 -
149 - return null;
150 } 146 }
151 147
152 @Override 148 @Override
......
...@@ -211,7 +211,7 @@ public class OnosSwaggerMojo extends AbstractMojo { ...@@ -211,7 +211,7 @@ public class OnosSwaggerMojo extends AbstractMojo {
211 private JavaAnnotation getPathAnnotation(JavaClass javaClass) { 211 private JavaAnnotation getPathAnnotation(JavaClass javaClass) {
212 Optional<JavaAnnotation> optional = javaClass.getAnnotations() 212 Optional<JavaAnnotation> optional = javaClass.getAnnotations()
213 .stream().filter(a -> a.getType().getName().equals(PATH)).findAny(); 213 .stream().filter(a -> a.getType().getName().equals(PATH)).findAny();
214 - return optional.isPresent() ? optional.get() : null; 214 + return optional.orElse(null);
215 } 215 }
216 216
217 // Checks whether a class's methods are REST methods and then places all the 217 // Checks whether a class's methods are REST methods and then places all the
...@@ -375,7 +375,7 @@ public class OnosSwaggerMojo extends AbstractMojo { ...@@ -375,7 +375,7 @@ public class OnosSwaggerMojo extends AbstractMojo {
375 Optional<JavaAnnotation> optional = javaParameter.getAnnotations().stream().filter( 375 Optional<JavaAnnotation> optional = javaParameter.getAnnotations().stream().filter(
376 annotation -> annotation.getType().getName().equals(PATH_PARAM) || 376 annotation -> annotation.getType().getName().equals(PATH_PARAM) ||
377 annotation.getType().getName().equals(QUERY_PARAM)).findAny(); 377 annotation.getType().getName().equals(QUERY_PARAM)).findAny();
378 - JavaAnnotation pathType = optional.isPresent() ? optional.get() : null; 378 + JavaAnnotation pathType = optional.orElse(null);
379 379
380 String annotationName = javaParameter.getName(); 380 String annotationName = javaParameter.getName();
381 381
......