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