001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2023–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.scopelet;
015
016import java.lang.constant.ClassDesc;
017import java.lang.constant.Constable;
018import java.lang.constant.ConstantDesc;
019import java.lang.constant.DynamicConstantDesc;
020import java.lang.constant.MethodHandleDesc;
021
022import java.util.Optional;
023
024import org.microbean.bean.AutoCloseableRegistry;
025import org.microbean.bean.Creation;
026import org.microbean.bean.Destruction;
027import org.microbean.bean.DisposableReference;
028import org.microbean.bean.Factory;
029
030import static java.lang.constant.ConstantDescs.BSM_INVOKE;
031
032/**
033 * A {@link Scopelet} implementation that does not cache objects at all.
034 *
035 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
036 */
037public class NoneScopelet extends Scopelet<NoneScopelet> implements Constable {
038
039  private static final boolean useDisposableReferences =
040    Boolean.parseBoolean(System.getProperty("useDisposableReferences", "false"));
041
042  /**
043   * Creates a new {@link NoneScopelet}.
044   */
045  public NoneScopelet() {
046    super();
047  }
048
049  // All parameters are nullable.
050  // Non-final to permit subclasses to, e.g., add logging.
051  @Override // Scopelet<NoneScopelet>
052  public <I> I instance(final Object ignoredBeanId, final Factory<I> factory, final Creation<I> creation) {
053    if (!this.active()) {
054      throw new InactiveScopeletException();
055    } else if (factory == null) {
056      return null;
057    }
058    final I returnValue = factory.create(creation);
059    if (factory.destroys()) {
060      if (useDisposableReferences) {
061        // Merely creating a DisposableReference will cause it to get disposed *IF* garbage collection runs (which is not
062        // guaranteed).
063        new DisposableReference<>(returnValue, referent -> factory.destroy(referent, (Destruction)creation));
064      } else if (creation instanceof AutoCloseableRegistry acr) {
065        acr.register(new Instance<I>(returnValue, factory::destroy, (Destruction)creation));
066      } else {
067        // TODO: warn or otherwise point out that dependencies will not be destroyed
068      }
069    }
070    return returnValue;
071  }
072
073  @Override // Constable
074  public Optional<? extends ConstantDesc> describeConstable() {
075    return
076      Optional.of(DynamicConstantDesc.of(BSM_INVOKE,
077                                         MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()))));
078  }
079
080}