Sho SHIMIZU

Remove a method checking if non-null by using Optional

Change-Id: Iec8d9016e09c6637574bf5b6f1aab5d8bfbec07a
......@@ -18,6 +18,8 @@ package org.onosproject.net.intent;
import com.google.common.annotations.Beta;
import org.onosproject.event.AbstractEvent;
import java.util.Optional;
/**
* A class to represent an intent related event.
*/
......@@ -95,7 +97,7 @@ public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> {
* @param data the intent data to create an event for
* @return new intent event if the state is valid, otherwise null.
*/
public static IntentEvent getEvent(IntentData data) {
public static Optional<IntentEvent> getEvent(IntentData data) {
return getEvent(data.state(), data.intent());
}
......@@ -107,7 +109,7 @@ public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> {
* @param intent intent to put in event
* @return new intent event if the state is valid, otherwise null.
*/
public static IntentEvent getEvent(IntentState state, Intent intent) {
public static Optional<IntentEvent> getEvent(IntentState state, Intent intent) {
Type type;
switch (state) {
case INSTALL_REQ:
......@@ -138,9 +140,9 @@ public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> {
case RECOMPILING:
case WITHDRAWING:
default:
return null;
return Optional.empty();
}
return new IntentEvent(type, intent);
return Optional.of(new IntentEvent(type, intent));
}
}
......
......@@ -126,17 +126,11 @@ public class SimpleIntentStore
pending.remove(newData.key());
}
}
notifyDelegateIfNotNull(IntentEvent.getEvent(newData));
IntentEvent.getEvent(newData).ifPresent(this::notifyDelegate);
}
}
}
private void notifyDelegateIfNotNull(IntentEvent event) {
if (event != null) {
notifyDelegate(event);
}
}
@Override
public void batchWrite(Iterable<IntentData> updates) {
for (IntentData data : updates) {
......@@ -174,7 +168,7 @@ public class SimpleIntentStore
pending.put(data.key(), data);
checkNotNull(delegate, "Store delegate is not set")
.process(new IntentData(data));
notifyDelegateIfNotNull(IntentEvent.getEvent(data));
IntentEvent.getEvent(data).ifPresent(this::notifyDelegate);
} else {
log.debug("IntentData {} is older than existing: {}",
data, existingData);
......
......@@ -284,12 +284,6 @@ public class GossipIntentStore
.collect(Collectors.toList());
}
private void notifyDelegateIfNotNull(IntentEvent event) {
if (event != null) {
notifyDelegate(event);
}
}
private final class InternalCurrentListener implements
EventuallyConsistentMapListener<Key, IntentData> {
@Override
......@@ -303,7 +297,7 @@ public class GossipIntentStore
if (delegate != null && isMaster(event.value().intent().key())) {
delegate.onUpdate(new IntentData(intentData)); // copy for safety, likely unnecessary
}
notifyDelegateIfNotNull(IntentEvent.getEvent(intentData));
IntentEvent.getEvent(intentData).ifPresent(e -> notifyDelegate(e));
}
}
}
......@@ -323,7 +317,7 @@ public class GossipIntentStore
}
}
notifyDelegateIfNotNull(IntentEvent.getEvent(event.value()));
IntentEvent.getEvent(event.value()).ifPresent(e -> notifyDelegate(e));
}
}
}
......