Remove a method checking if non-null by using Optional

Change-Id: Iec8d9016e09c6637574bf5b6f1aab5d8bfbec07a
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java b/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java
index b27a507..e3eb84a 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentEvent.java
@@ -18,6 +18,8 @@
 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 @@
      * @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 @@
      * @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 @@
             case RECOMPILING:
             case WITHDRAWING:
             default:
-                return null;
+                return Optional.empty();
         }
-        return new IntentEvent(type, intent);
+        return Optional.of(new IntentEvent(type, intent));
     }
 
 }
diff --git a/core/common/src/test/java/org/onosproject/store/trivial/SimpleIntentStore.java b/core/common/src/test/java/org/onosproject/store/trivial/SimpleIntentStore.java
index 9f95966..4a9eaa9 100644
--- a/core/common/src/test/java/org/onosproject/store/trivial/SimpleIntentStore.java
+++ b/core/common/src/test/java/org/onosproject/store/trivial/SimpleIntentStore.java
@@ -126,17 +126,11 @@
                         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 @@
                 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);
diff --git a/core/store/dist/src/main/java/org/onosproject/store/intent/impl/GossipIntentStore.java b/core/store/dist/src/main/java/org/onosproject/store/intent/impl/GossipIntentStore.java
index 3636f14..f87bd3d 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/intent/impl/GossipIntentStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/intent/impl/GossipIntentStore.java
@@ -284,12 +284,6 @@
                 .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 @@
                 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 @@
                     }
                 }
 
-                notifyDelegateIfNotNull(IntentEvent.getEvent(event.value()));
+                IntentEvent.getEvent(event.value()).ifPresent(e -> notifyDelegate(e));
             }
         }
     }