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.List; 023import java.util.Objects; 024import java.util.Optional; 025 026import org.microbean.bean.Id; 027 028import org.microbean.construct.Domain; 029 030import static java.lang.constant.ConstantDescs.BSM_INVOKE; 031 032import static org.microbean.assign.Qualifiers.anyQualifier; 033 034import static org.microbean.scope.Scope.SINGLETON_ID; 035 036/** 037 * A {@link MapBackedScopelet} implementation that caches singletons. 038 * 039 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a> 040 */ 041public class SingletonScopelet extends MapBackedScopelet<SingletonScopelet> implements Constable { 042 043 private final Domain domain; 044 045 /** 046 * Creates a new {@link SingletonScopelet}. 047 * 048 * @param domain a {@link Domain}; must not be {@code null} 049 * 050 * @exception NullPointerException if {@code domain} is {@code null} 051 */ 052 public SingletonScopelet(final Domain domain) { 053 super(SINGLETON_ID); // the scope we implement 054 this.domain = Objects.requireNonNull(domain, "domain"); 055 } 056 057 @Override // Scopelet<SingletonScopelet> 058 public Id id() { 059 return 060 new Id(List.of(this.domain.declaredType(SingletonScopelet.class.getName()), 061 this.domain.declaredType(null, 062 this.domain.typeElement(Scopelet.class.getName()), 063 this.domain.declaredType(SingletonScopelet.class.getName()))), 064 List.of(SINGLETON_ID, anyQualifier()), // qualifiers 065 SINGLETON_ID); // the scope we belong to 066 } 067 068 @Override // Constable 069 public Optional<? extends ConstantDesc> describeConstable() { 070 return (this.domain instanceof Constable c ? c.describeConstable() : Optional.<ConstantDesc>empty()) 071 .map(domainDesc -> DynamicConstantDesc.of(BSM_INVOKE, 072 MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()), 073 ClassDesc.of(Domain.class.getName())), 074 domainDesc)); 075 } 076 077}