All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hudi.common.util.Option Maven / Gradle / Ivy

There is a newer version: 1.0.0-beta2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hudi.common.util;

import java.io.Serializable;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 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 implements Serializable {

  private static final long serialVersionUID = 0L;

  private static final Option EMPTY = new Option<>();

  private final T val;

  /**
   * Convert to java Optional.
   */
  public Optional toJavaOptional() {
    return Optional.ofNullable(val);
  }

  /**
   * Convert from java.util.Optional.
   * 
   * @param v java.util.Optional object
   * @param  type of the value stored in java.util.Optional object
   * @return Option
   */
  public static  Option fromJavaOptional(Optional v) {
    return Option.ofNullable(v.orElse(null));
  }

  private Option() {
    this.val = null;
  }

  private Option(T val) {
    if (null == val) {
      throw new NullPointerException("Expected a non-null value. Got null");
    }
    this.val = val;
  }

  @SuppressWarnings("unchecked")
  public static  Option empty() {
    return (Option) EMPTY;
  }

  public static  Option of(T value) {
    return new Option<>(value);
  }

  public static  Option ofNullable(T value) {
    return null == value ? empty() : of(value);
  }

  public boolean isPresent() {
    return null != val;
  }

  public T get() {
    if (null == val) {
      throw new NoSuchElementException("No value present in Option");
    }
    return val;
  }

  public void ifPresent(Consumer consumer) {
    if (val != null) {
      // process the value
      consumer.accept(val);
    }
  }

  public  Option map(Function mapper) {
    if (null == mapper) {
      throw new NullPointerException("mapper should not be null");
    }
    if (!isPresent()) {
      return empty();
    } else {
      return Option.ofNullable(mapper.apply(val));
    }
  }

  public  Option flatMap(Function> mapper) {
    if (null == mapper) {
      throw new NullPointerException("mapper should not be null");
    }
    if (!isPresent()) {
      return empty();
    } else {
      return Objects.requireNonNull(mapper.apply(val));
    }
  }

  /**
   * Returns this {@link Option} if not empty, otherwise returns an alternative
   */
  public Option or(Option other) {
    return val != null ? this : other;
  }

  /**
   * Returns this {@link Option} if not empty, otherwise evaluates provided supplier
   * and returns its result
   */
  public Option or(Supplier> other) {
    return val != null ? this : other.get();
  }

  /**
   * Identical to {@code Optional.orElse}
   */
  public T orElse(T other) {
    return val != null ? val : other;
  }

  /**
   * Identical to {@code Optional.orElseGet}
   */
  public T orElseGet(Supplier other) {
    return val != null ? val : other.get();
  }

  /**
   * Identical to {@code Optional.orElseThrow}
   */
  public  T orElseThrow(Supplier exceptionSupplier) throws X {
    if (val != null) {
      return val;
    } else {
      throw exceptionSupplier.get();
    }
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Option option = (Option) o;
    return Objects.equals(val, option.val);
  }

  @Override
  public int hashCode() {
    return Objects.hash(val);
  }

  @Override
  public String toString() {
    return val != null
            ? "Option{val=" + val + "}"
            : "Optional.empty";
  }
}