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.Collections;
024import java.util.List;
025import java.util.Objects;
026import java.util.Optional;
027
028import javax.lang.model.type.TypeMirror;
029
030import org.microbean.constant.Constables;
031
032import org.microbean.qualifier.NamedAttributeMap;
033
034import org.microbean.scope.ScopeMember;
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
045import static org.microbean.bean.ConstantDescs.CD_Id;
046
047import static org.microbean.qualifier.ConstantDescs.CD_NamedAttributeMap;
048
049/**
050 * An identifier for a {@link Bean}.
051 *
052 * @param types a {@link BeanTypeList}
053 *
054 * @param attributes a {@link List} of {@link NamedAttributeMap}s
055 *
056 * @param governingScopeId a {@link NamedAttributeMap} identifying the <dfn>scope</dfn> to which this {@link Id}
057 * logically belongs
058 *
059 * @param alternate whether this {@link Id} is to be considered an <dfn>alternate</dfn>
060 *
061 * @param rank the {@linkplain Ranked rank} of this {@link Id}
062 *
063 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
064 *
065 * @see Ranked
066 *
067 * @see ScopeMember
068 */
069public final record Id(BeanTypeList types,
070                       List<NamedAttributeMap<?>> attributes,
071                       NamedAttributeMap<?> governingScopeId,
072                       boolean alternate,
073                       int rank)
074  implements Constable, Ranked, ScopeMember {
075
076
077  /*
078   * Constructors.
079   */
080
081
082  /**
083   * Creates a new {@link Id} that is not an alternate and that has a {@linkplain Ranked#DEFAULT_RANK default rank}.
084   *
085   * @param types a {@link BeanTypeList}; must not be {@code null}; must not be {@linkplain List#isEmpty() empty}
086   *
087   * @param attributes a {@link List} of {@link NamedAttributeMap}s; must not be {@code null}
088   *
089   * @param governingScopeId a {@link NamedAttributeMap} identifying the <dfn>scope</dfn> to which this {@link Id}
090   * logically belongs; must not be {@code null}
091   *
092   * @exception NullPointerException if {@code types}, {@code attributes}, or {@code governingScopeId} is {@code null}
093   */
094  public Id(final BeanTypeList types,
095            final List<NamedAttributeMap<?>> attributes,
096            final NamedAttributeMap<?> governingScopeId) {
097    this(types, attributes, governingScopeId, false, Ranked.DEFAULT_RANK);
098  }
099
100  /**
101   * Creates a new {@link Id} that is not an alternate.
102   *
103   * @param types a {@link BeanTypeList}; must not be {@code null}; must not be {@linkplain List#isEmpty() empty}
104   *
105   * @param attributes a {@link List} of {@link NamedAttributeMap}s; must not be {@code null}
106   *
107   * @param governingScopeId a {@link NamedAttributeMap} identifying the <dfn>scope</dfn> to which this {@link Id}
108   * logically belongs; must not be {@code null}
109   *
110   * @param rank the {@linkplain Ranked rank} of this {@link Id}
111   *
112   * @exception NullPointerException if {@code types}, {@code attributes}, or {@code governingScopeId} is {@code null}
113   */
114  public Id(final BeanTypeList types,
115            final List<NamedAttributeMap<?>> attributes,
116            final NamedAttributeMap<?> governingScopeId,
117            final int rank) {
118    this(types, attributes, governingScopeId, false, rank);
119  }
120
121  /**
122   * Creates a new {@link Id}.
123   *
124   * @param types a {@link BeanTypeList}; must not be {@code null}; must not be {@linkplain List#isEmpty() empty}
125   *
126   * @param attributes a {@link List} of {@link NamedAttributeMap}s; must not be {@code null}
127   *
128   * @param governingScopeId a {@link NamedAttributeMap} identifying the <dfn>scope</dfn> to which this {@link Id}
129   * logically belongs; must not be {@code null}
130   *
131   * @param alternate whether this {@link Id} is to be considered an <dfn>alternate</dfn>
132   *
133   * @param rank the {@linkplain Ranked rank} of this {@link Id}
134   *
135   * @exception NullPointerException if {@code types}, {@code attributes}, or {@code governingScopeId} is {@code null}
136   */
137  public Id {
138    Objects.requireNonNull(types, "types");
139    attributes = List.copyOf(attributes);
140    Objects.requireNonNull(governingScopeId, "governingScopeId");
141  }
142
143
144  /*
145   * Instance methods.
146   */
147
148  
149  @Override // Constable
150  public final Optional<DynamicConstantDesc<Id>> describeConstable() {
151    return Constables.describeConstable(this.attributes())
152      .flatMap(attributesDesc -> this.governingScopeId().describeConstable()
153               .flatMap(governingScopeIdDesc -> this.types().describeConstable()
154                        .map(typesDesc -> DynamicConstantDesc.of(BSM_INVOKE,
155                                                                 MethodHandleDesc.ofConstructor(CD_Id,
156                                                                                                CD_List,
157                                                                                                CD_List,
158                                                                                                CD_NamedAttributeMap,
159                                                                                                CD_boolean,
160                                                                                                CD_int),
161                                                                 typesDesc,
162                                                                 attributesDesc,
163                                                                 governingScopeIdDesc,
164                                                                 this.alternate() ? TRUE : FALSE,
165                                                                 this.rank()))));
166  }
167
168}