vinothchandar commented on a change in pull request #953: [HUDI-121] Fix 
licensing issues found during RC voting by general incubator group
URL: https://github.com/apache/incubator-hudi/pull/953#discussion_r335264478
 
 

 ##########
 File path: hudi-common/src/main/java/org/apache/hudi/common/util/Option.java
 ##########
 @@ -24,291 +24,122 @@
 import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Function;
-import java.util.function.Predicate;
 import java.util.function.Supplier;
 
 /**
- * Copied from java.util.Optional and made Serializable along with methods to 
convert to/from standard Option
+ * Provides functionality same as java.util.Optional but is also made 
Serializable. Additional APIs are provided to
+ * convert to/from java.util.Optional
  */
 public final class Option<T> implements Serializable {
 
   private static final long serialVersionUID = 0L;
 
-  /**
-   * Common instance for {@code empty()}.
-   */
-  private static final Option<?> EMPTY = new Option<>();
+  private static final Option<?> NULL_VAL = new Option<>();
+
+  private final T val;
 
   /**
-   * If non-null, the value; if null, indicates no value is present
+   * Convert to java Optional
    */
-  private final T value;
+  public Optional<T> toJavaOptional() {
+    return Optional.ofNullable(val);
+  }
 
   /**
-   * Constructs an empty instance.
-   *
-   * @implNote Generally only one empty instance, {@link Option#EMPTY}, should 
exist per VM.
+   * Convert from java.util.Optional
+   * 
+   * @param v java.util.Optional object
+   * @param <T> type of the value stored in java.util.Optional object
+   * @return Option
    */
+  public static <T> Option<T> fromJavaOptional(Optional<T> v) {
+    return Option.ofNullable(v.orElse(null));
+  }
+
   private Option() {
-    this.value = null;
+    this.val = null;
   }
 
-  /**
-   * Returns an empty {@code Option} instance. No value is present for this 
Option.
-   *
-   * @param <T> Type of the non-existent value
-   * @return an empty {@code Option}
-   * @apiNote Though it may be tempting to do so, avoid testing if an object 
is empty by comparing with {@code ==}
-   *          against instances returned by {@code Option.empty()}. There is 
no guarantee that it is a singleton.
-   *          Instead, use {@link #isPresent()}.
-   */
-  public static <T> Option<T> empty() {
-    @SuppressWarnings("unchecked")
-    Option<T> t = (Option<T>) EMPTY;
-    return t;
+  private Option(T val) {
+    if (null == val) {
+      throw new NullPointerException("Expected a non-null value. Got null");
+    }
+    this.val = val;
   }
 
-  /**
-   * Constructs an instance with the value present.
-   *
-   * @param value the non-null value to be present
-   * @throws NullPointerException if value is null
-   */
-  private Option(T value) {
-    this.value = Objects.requireNonNull(value);
+  public static <T> Option<T> empty() {
+    return (Option<T>) NULL_VAL;
   }
 
-  /**
-   * Returns an {@code Option} with the specified present non-null value.
-   *
-   * @param <T> the class of the value
-   * @param value the value to be present, which must be non-null
-   * @return an {@code Option} with the value present
-   * @throws NullPointerException if value is null
-   */
   public static <T> Option<T> of(T value) {
     return new Option<>(value);
   }
 
-  /**
-   * Returns an {@code Option} describing the specified value, if non-null, 
otherwise returns an empty {@code Option}.
-   *
-   * @param <T> the class of the value
-   * @param value the possibly-null value to describe
-   * @return an {@code Option} with a present value if the specified value is 
non-null, otherwise an empty {@code
-   * Option}
-   */
   public static <T> Option<T> ofNullable(T value) {
-    return value == null ? empty() : of(value);
-  }
-
-  /**
-   * If a value is present in this {@code Option}, returns the value, 
otherwise throws {@code NoSuchElementException}.
-   *
-   * @return the non-null value held by this {@code Option}
-   * @throws NoSuchElementException if there is no value present
-   * @see Option#isPresent()
-   */
-  public T get() {
-    if (value == null) {
-      throw new NoSuchElementException("No value present");
-    }
-    return value;
+    return null == value ? empty() : of(value);
   }
 
-  /**
-   * Return {@code true} if there is a value present, otherwise {@code false}.
-   *
-   * @return {@code true} if there is a value present, otherwise {@code false}
-   */
   public boolean isPresent() {
-    return value != null;
+    return null != val;
   }
 
-  /**
-   * If a value is present, invoke the specified consumer with the value, 
otherwise do nothing.
-   *
-   * @param consumer block to be executed if a value is present
-   * @throws NullPointerException if value is present and {@code consumer} is 
null
-   */
-  public void ifPresent(Consumer<? super T> consumer) {
-    if (value != null) {
-      consumer.accept(value);
+  public T get() {
+    if (null == val) {
+      throw new NoSuchElementException("No value present in Option");
     }
+    return val;
   }
 
-  /**
-   * If a value is present, and the value matches the given predicate, return 
an {@code Option} describing the value,
-   * otherwise return an empty {@code Option}.
-   *
-   * @param predicate a predicate to apply to the value, if present
-   * @return an {@code Option} describing the value of this {@code Option} if 
a value is present and the value matches
-   *         the given predicate, otherwise an empty {@code Option}
-   * @throws NullPointerException if the predicate is null
-   */
-  public Option<T> filter(Predicate<? super T> predicate) {
-    Objects.requireNonNull(predicate);
-    if (!isPresent()) {
-      return this;
-    } else {
-      return predicate.test(value) ? this : empty();
+  public void ifPresent(Consumer<? super T> consumer) {
+    if (val != null) {
+      // process the value
+      consumer.accept(val);
     }
   }
 
-  /**
-   * If a value is present, apply the provided mapping function to it, and if 
the result is non-null, return an {@code
-   * Option} describing the result. Otherwise return an empty {@code Option}.
-   *
-   * @param <U> The type of the result of the mapping function
-   * @param mapper a mapping function to apply to the value, if present
-   * @return an {@code Option} describing the result of applying a mapping 
function to the value of this {@code Option},
-   *         if a value is present, otherwise an empty {@code Option}
-   * @throws NullPointerException if the mapping function is null
-   * @apiNote This method supports post-processing on optional values, without 
the need to explicitly check for a return
-   *          status. For example, the following code traverses a stream of 
file names, selects one that has not yet
-   *          been processed, and then opens that file, returning an {@code 
Option<FileInputStream>}:
-   *
-   *          <pre>
-   * {@code
-   *     Option<FileInputStream> fis =
-   *         names.stream().filter(name -> !isProcessedYet(name))
-   *                       .findFirst()
-   *                       .map(name -> new FileInputStream(name));
-   * }
-   *          </pre>
-   *
-   *          Here, {@code findFirst} returns an {@code Option<String>}, and 
then {@code map} returns an {@code
-   * Option<FileInputStream>} for the desired file if one exists.
-   */
   public <U> Option<U> map(Function<? super T, ? extends U> mapper) {
 
 Review comment:
   do we actually call this method? 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to