001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2024 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.util.Collection;
017import java.util.Collections;
018import java.util.LinkedHashSet;
019import java.util.List;
020import java.util.SequencedSet;
021
022/**
023 * An object with {@linkplain AttributedElement dependencies}.
024 *
025 * <p>By default, {@link Aggregate}s have no dependencies.</p>
026 *
027 * @author <a href="https://about.me/lairdnelson/" target="_top">Laird Nelson</a>
028 *
029 * @see #dependencies()
030 */
031public interface Aggregate {
032
033
034  /*
035   * Static fields.
036   */
037
038
039  /**
040   * An immutable, empty {@link SequencedSet} of {@link Assignment}s.
041   */
042  public static final SequencedSet<Assignment<?>> EMPTY_ASSIGNMENTS = Collections.unmodifiableSequencedSet(new LinkedHashSet<>(0));
043
044  /**
045   * An immutable, empty {@link SequencedSet} of {@link AttributedElement}s.
046   */
047  public static final SequencedSet<AttributedElement> EMPTY_DEPENDENCIES = Collections.unmodifiableSequencedSet(new LinkedHashSet<>(0));
048
049
050  /*
051   * Default instance methods.
052   */
053
054
055  /**
056   * Returns an unmodifiable {@link SequencedSet} of {@link AttributedElement} instances.
057   *
058   * @return an unmodifiable {@link SequencedSet} of {@link AttributedElement} instances; never {@code null}
059   *
060   * @see AttributedElement
061   */
062  public default SequencedSet<AttributedElement> dependencies() {
063    return EMPTY_DEPENDENCIES;
064  }
065
066  /**
067   * Assigns a contextual reference to each of this {@link Aggregate}'s {@link AttributedElement} instances and returns the
068   * resulting {@link List} of {@link Assignment}s.
069   *
070   * @param r a {@link Request}; must not be {@code null}
071   *
072   * @return a {@link List} of {@link Assignment} instances; never {@code null}
073   *
074   * @exception NullPointerException if {@code r} is {@code null}
075   */
076  public default SequencedSet<? extends Assignment<?>> assign(final Request<?> r) {
077    final Collection<? extends AttributedElement> ds = this.dependencies();
078    if (ds == null || ds.isEmpty()) {
079      return EMPTY_ASSIGNMENTS;
080    }
081    final SequencedSet<Assignment<?>> assignments = new LinkedHashSet<>();
082    for (final AttributedElement d : ds) {
083      assignments.add(new Assignment<>(d, r.reference(d.attributedType())));
084    }
085    return Collections.unmodifiableSequencedSet(assignments);
086  }
087
088}