001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 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.util.Iterator;
017
018/**
019 * An {@link Iterable} {@linkplain #iterator() providing access to contextual references of a given type}, and a {@link
020 * ReferencesSelector} providing access to a {@link References} of a different type.
021 *
022 * @param <R> the contextual reference type
023 *
024 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
025 *
026 * @see ReferencesSelector
027 */
028// TODO: not crazy about the circular dependencies, but they're no worse than, say, a sealed class
029public interface References<R> extends Iterable<R>, ReferencesSelector {
030
031  /**
032   * A convenience method that acquires and returns what is presumed, possibly incorrectly, to be the sole contextual
033   * reference available from this {@link References}.
034   *
035   * @return a contextual reference; never {@code null}
036   *
037   * @exception UnsatisfiedReductionException if there are no contextual references to return
038   *
039   * @exception AmbiguousReductionException if there is more than one contextual reference to return
040   */
041  public default R get() {
042    final Iterator<R> i = this.iterator();
043    if (!i.hasNext()) {
044      throw new UnsatisfiedReductionException(null, null, null);
045    }
046    final R r = i.next();
047    if (i.hasNext()) {
048      throw new AmbiguousReductionException(null, null, null);
049    }
050    return r;
051  }
052
053  /**
054   * Returns the size of this {@link References}.
055   *
056   * @return the size of this {@link References} (a positive integer)
057   */
058  public int size();
059
060}