001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2024–2025 microBean™.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
011 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
012 * specific language governing permissions and limitations under the License.
013 */
014package org.microbean.bean;
015
016import java.lang.constant.ClassDesc;
017import java.lang.constant.Constable;
018import java.lang.constant.DynamicConstantDesc;
019import java.lang.constant.MethodHandleDesc;
020
021import java.util.Objects;
022import java.util.Optional;
023
024import org.microbean.constant.Constables;
025
026import static java.lang.constant.ConstantDescs.BSM_INVOKE;
027import static java.lang.constant.ConstantDescs.CD_Object;
028
029/**
030 * A {@link Factory} that returns its singleton from its {@link #create(Request)} method.
031 *
032 * @param <I> the type of the singleton
033 *
034 * @param singleton the singleton
035 *
036 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
037 *
038 * @see #singleton()
039 *
040 * @see #create(Request)
041 */
042public final record Constant<I>(I singleton) implements Constable, Factory<I> {
043
044  /**
045   * Creates a new {@link Constant}.
046   *
047   * @param singleton an object; must not be {@code null}
048   *
049   * @exception NullPointerException if {@code singleton} is {@code null}
050   */
051  public Constant {
052    Objects.requireNonNull(singleton, "singleton");
053  }
054
055  /**
056   * Invokes the {@link #singleton()} method and returns its result.
057   *
058   * @param r a {@link Request}; may be {@code null}; ignored
059   *
060   * @return the result of invoking the {@link #singleton()} method
061   *
062   * @see #singleton()
063   */
064  @Override // Factory<I>
065  public final I create(final Request<I> r) {
066    return this.singleton();
067  }
068
069  /**
070   * Returns {@code true} if and only if the return value of an invocation of the {@link #singleton()} method is {@link
071   * AutoCloseable}.
072   *
073   * @return {@code true} if and only if the return value of an invocation of the {@link #singleton()} method is {@link
074   * AutoCloseable}
075   */
076  @Override // Factory<I>
077  public final boolean destroys() {
078    return this.singleton() instanceof AutoCloseable;
079  }
080
081  @Override // Factory<I>
082  public final void destroy(final I i, final Request<I> request) {
083    Factory.super.destroy(i, request);
084  }
085
086  @Override // Constable
087  public final Optional<DynamicConstantDesc<I>> describeConstable() {
088    return Constables.describeConstable(this.singleton())
089      .map(iDesc -> DynamicConstantDesc.of(BSM_INVOKE,
090                                           MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()),
091                                                                          CD_Object),
092                                           iDesc));
093  }
094
095
096}