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
045import static org.microbean.bean.ConstantDescs.CD_Id;
046
047/**
048 * An identifier for a {@link Bean}.
049 *
050 * @param types a {@link BeanTypeList}
051 *
052 * @param attributes a {@link List} of {@link Attributes}s
053 *
054 * @param alternate whether this {@link Id} is to be considered an <dfn>alternate</dfn>
055 *
056 * @param rank the {@linkplain Ranked rank} of this {@link Id}
057 *
058 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
059 *
060 * @see Ranked
061 */
062public final record Id(BeanTypeList types,
063                       List<Attributes> attributes,
064                       boolean alternate,
065                       int rank)
066  implements Attributed, Constable, Ranked {
067
068
069  /*
070   * Constructors.
071   */
072
073
074  /**
075   * Creates a new {@link Id} that is not an alternate and that has a {@linkplain Ranked#DEFAULT_RANK default 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, Ranked.DEFAULT_RANK);
086  }
087
088  /*
089   * Creates a new {@link Id} that is not an alternate and that has a {@linkplain Ranked#DEFAULT_RANK default rank}.
090   *
091   * @param types a {@link BeanTypeList}; must not be {@code null}; must not be {@linkplain List#isEmpty() empty}
092   *
093   * @param attributes an array of {@link Attributes}s; must not be {@code null}
094   *
095   * @exception NullPointerException if {@code types} or {@code attributes} is {@code null}
096   */
097  /*
098  public Id(final BeanTypeList types,
099            final Attributes... attributes) {
100    this(types, Arrays.asList(attributes), false, Ranked.DEFAULT_RANK);
101  }
102  */
103  
104  /**
105   * Creates a new {@link Id} that is not an alternate.
106   *
107   * @param types a {@link BeanTypeList}; must not be {@code null}; must not be {@linkplain List#isEmpty() empty}
108   *
109   * @param attributes a {@link List} of {@link Attributes}s; must not be {@code null}
110   *
111   * @param rank the {@linkplain Ranked rank} of this {@link Id}
112   *
113   * @exception NullPointerException if {@code types} or {@code attributes} is {@code null}
114   */
115  public Id(final BeanTypeList types,
116            final List<Attributes> attributes,
117            final int rank) {
118    this(types, attributes, 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 Attributes}s; must not be {@code null}
127   *
128   * @param alternate whether this {@link Id} is to be considered an <dfn>alternate</dfn>
129   *
130   * @param rank the {@linkplain Ranked rank} of this {@link Id}
131   *
132   * @exception NullPointerException if {@code types} or {@code attributes} is {@code null}
133   */
134  public Id {
135    Objects.requireNonNull(types, "types");
136    attributes = List.copyOf(attributes);
137  }
138
139
140  /*
141   * Instance methods.
142   */
143
144
145  @Override // Constable
146  public final Optional<DynamicConstantDesc<Id>> describeConstable() {
147    final ClassDesc CD_Attributes = ClassDesc.of(Attributes.class.getName());
148    return Constables.describeConstable(this.attributes())
149      .flatMap(attributesDesc -> this.types().describeConstable()
150               .map(typesDesc -> DynamicConstantDesc.of(BSM_INVOKE,
151                                                        MethodHandleDesc.ofConstructor(CD_Id,
152                                                                                       CD_List,
153                                                                                       CD_List,
154                                                                                       CD_boolean,
155                                                                                       CD_int),
156                                                        typesDesc,
157                                                        attributesDesc,
158                                                        this.alternate() ? TRUE : FALSE,
159                                                        this.rank())));
160  }
161
162}