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.bean; 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.ArrayList; 023import java.util.Arrays; 024import java.util.Collections; 025import java.util.List; 026import java.util.Objects; 027import java.util.Optional; 028 029import javax.lang.model.type.TypeMirror; 030 031import org.microbean.attributes.Attributed; 032import org.microbean.attributes.Attributes; 033 034import org.microbean.constant.Constables; 035 036import static java.lang.constant.ConstantDescs.BSM_INVOKE; 037import static java.lang.constant.ConstantDescs.CD_boolean; 038import static java.lang.constant.ConstantDescs.CD_int; 039import static java.lang.constant.ConstantDescs.CD_List; 040import static java.lang.constant.ConstantDescs.FALSE; 041import static java.lang.constant.ConstantDescs.TRUE; 042 043import static org.microbean.bean.BeanTypes.legalBeanType; 044 045/** 046 * An identifier for a {@link Bean}. 047 * 048 * @param types a {@link BeanTypeList} 049 * 050 * @param attributes a {@link List} of {@link Attributes}s 051 * 052 * @param alternate whether this {@link Id} is to be considered an <dfn>alternate</dfn> 053 * 054 * @param rank the {@linkplain Ranked#rank() rank} of this {@link Id}; often {@link Ranked#DEFAULT_RANK} to indicate no 055 * particular rank; <strong>always {@link Ranked#DEFAULT_RANK} if {@code alternate} is {@code false}</strong> 056 * 057 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a> 058 * 059 * @see Ranked 060 */ 061public final record Id(BeanTypeList types, 062 List<Attributes> attributes, 063 boolean alternate, 064 int rank) 065 implements Attributed, Constable, Ranked { 066 067 068 /* 069 * Constructors. 070 */ 071 072 073 /** 074 * Creates a new {@link Id} that is not an alternate and that therefore has a {@linkplain Ranked#DEFAULT_RANK default 075 * rank}. 076 * 077 * @param types a {@link BeanTypeList}; must not be {@code null}; must not be {@linkplain List#isEmpty() empty} 078 * 079 * @param attributes a {@link List} of {@link Attributes}s; must not be {@code null} 080 * 081 * @exception NullPointerException if {@code types} or {@code attributes} is {@code null} 082 */ 083 public Id(final BeanTypeList types, 084 final List<Attributes> attributes) { 085 this(types, attributes, false, DEFAULT_RANK); 086 } 087 088 /** 089 * Creates a new {@link Id}. 090 * 091 * @param types a {@link BeanTypeList}; must not be {@code null}; must not be {@linkplain List#isEmpty() empty} 092 * 093 * @param attributes a {@link List} of {@link Attributes}s; must not be {@code null} 094 * 095 * @param alternate whether this {@link Id} is to be considered an <dfn>alternate</dfn> 096 * 097 * @param rank the {@linkplain Ranked#rank() rank} of this {@link Id}; often {@link Ranked#DEFAULT_RANK} to indicate 098 * no particular rank; <strong>always {@link Ranked#DEFAULT_RANK} if {@code alternate} is {@code false}</strong> 099 * 100 * @exception NullPointerException if {@code types} or {@code attributes} is {@code null} 101 */ 102 public Id { 103 Objects.requireNonNull(types, "types"); 104 attributes = List.copyOf(attributes); 105 if (!alternate) { 106 rank = DEFAULT_RANK; 107 } 108 } 109 110 111 /* 112 * Instance methods. 113 */ 114 115 116 @Override // Constable 117 public final Optional<DynamicConstantDesc<Id>> describeConstable() { 118 return Constables.describeConstable(this.attributes()) 119 .flatMap(attributesDesc -> this.types().describeConstable() 120 .map(typesDesc -> DynamicConstantDesc.of(BSM_INVOKE, 121 MethodHandleDesc.ofConstructor(this.getClass() 122 .describeConstable() 123 .orElseThrow(), 124 CD_List, 125 CD_List, 126 CD_boolean, 127 CD_int), 128 typesDesc, 129 attributesDesc, 130 this.alternate() ? TRUE : FALSE, 131 this.rank()))); 132 } 133 134}