001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2022 microBean™.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014 * implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.microbean.invoke;
018
019/**
020 * An {@link OptionalSupplier} implementation that supplies a fixed
021 * value.
022 *
023 * @author <a href="https://about.me/lairdnelson"
024 * target="_parent">Laird Nelson</a>
025 */
026public final class FixedValueSupplier<T> implements OptionalSupplier<T> {
027
028
029  private final T value;
030
031
032  /*
033   * Constructors.
034   */
035
036
037  /**
038   * Creates a new {@link FixedValueSupplier} that will {@linkplain
039   * #get() supply} the supplied value.
040   *
041   * @param value the value to be {@linkplain #get() supplied}; may be
042   * {@code null}
043   */
044  private FixedValueSupplier(final T value) {
045    super();
046    this.value = value;
047  }
048
049
050  /*
051   * Instance methods.
052   */
053
054
055  /**
056   * Returns {@link Determinism#PRESENT} when invoked.
057   *
058   * @return {@link Determinism#PRESENT} when invoked
059   *
060   * @nullability This method never returns {@code null}.
061   *
062   * @idempotency This method is idempotent and deterministic.
063   *
064   * @threadsafety This method is safe for concurrent use by multiple
065   * threads.
066   */
067  @Override // OptionalSupplier<T>
068  public final Determinism determinism() {
069    return Determinism.PRESENT;
070  }
071
072  /**
073   * Returns the value supplied {@linkplain
074   * #FixedValueSupplier(Object) at construction time}, which may be
075   * {@code null}.
076   *
077   * @return the value supplied {@linkplain
078   * #FixedValueSupplier(Object) at construction time}
079   *
080   * @nullability This method may return {@code null}.
081   *
082   * @idempotency This method is idempotent and deterministic.
083   *
084   * @threadsafety This method is safe for concurrent use by
085   * multiple threads.
086   */
087  @Override // OptionalSupplier<T>
088  public final T get() {
089    return this.value;
090  }
091
092
093  /*
094   * Static methods.
095   */
096
097
098  /**
099   * Returns a {@link FixedValueSupplier} {@linkplain #get()
100   * supplying} the supplied value.
101   *
102   * @param <T> the type of the value the returned {@link
103   * FixedValueSupplier} will {@linkplain #get() supply}
104   *
105   * @param value the value the {@link #get()} method will return;
106   * may be {@code null}
107   *
108   * @return a {@link FixedValueSupplier} {@linkplain #get()
109   * supplying} the supplied value
110   *
111   * @nullability This method never returns {@code null}.
112   *
113   * @idempotency This method is idempotent and deterministic.
114   *
115   * @threadsafety This method is safe for concurrent use by
116   * multiple threads.
117   */
118  public static final <T> FixedValueSupplier<T> of(final T value) {
119    return new FixedValueSupplier<>(value);
120  }
121
122}