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

org.microbean.invoke.OptionalSupplierAdapter Maven / Gradle / Ivy

There is a newer version: 0.0.17
Show newest version
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
 *
 * Copyright © 2022 microBean™.
 *
 * Licensed 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.microbean.invoke;

import java.util.Objects;

import java.util.function.Supplier;

final class OptionalSupplierAdapter implements OptionalSupplier {

  private final Determinism determinism;

  private final Supplier supplier;

  OptionalSupplierAdapter() {
    this(Determinism.ABSENT, Absence.instance());
  }

  OptionalSupplierAdapter(Supplier supplier) {
    this(Determinism.NON_DETERMINISTIC, supplier);
  }

  OptionalSupplierAdapter(final Determinism determinism, Supplier supplier) {
    super();
    if (supplier == null) {
      this.determinism = Determinism.ABSENT;
      this.supplier = Absence.instance();
    } else if (supplier instanceof OptionalSupplier os) {
      if (Objects.requireNonNull(determinism, "determinism") == os.determinism() ||
          determinism == Determinism.NON_DETERMINISTIC) {
        this.determinism = os.determinism();
        this.supplier = supplier;
      } else {
        throw new IllegalArgumentException("determinism: " + determinism + "; supplier: " + supplier);
      }
    } else {
      this.determinism = Objects.requireNonNull(determinism, "determinism");
      this.supplier = supplier;
    }
  }

  @Override
  public final Determinism determinism() {
    return this.determinism;
  }

  @Override
  public final T get() {
    return this.supplier.get();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy