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 static java.lang.constant.ConstantDescs.BSM_INVOKE;
025
026/**
027 * A {@link MapBackedScopelet} implementation that caches singletons.
028 *
029 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
030 */
031public class SingletonScopelet extends MapBackedScopelet<SingletonScopelet> implements Constable {
032
033  /**
034   * Creates a new {@link SingletonScopelet}.
035   */
036  public SingletonScopelet() {
037    super();
038  }
039
040  @Override // Constable
041  public Optional<? extends ConstantDesc> describeConstable() {
042    return
043      Optional.of(DynamicConstantDesc.of(BSM_INVOKE,
044                                         MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()))));
045  }
046
047}