001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2024–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.List;
017
018/**
019 * A notional list of elements from which immutable sublists may be <dfn>selected</dfn> according to some
020 * <dfn>criteria</dfn>.
021 *
022 * @param <C> the criteria type
023 *
024 * @param <E> the element type
025 *
026 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
027 */
028@FunctionalInterface
029public interface Selectable<C, E> {
030
031  /**
032   * <em>Selects</em> and returns an immutable {@link List} representing a sublist of this {@link Selectable}'s
033   * elements, as mediated by the supplied criteria.
034   *
035   * <p>Implementations of this method must be idempotent and must return a determinate value.</p>
036   *
037   * <p>Implementations of this method must not return {@code null}.</p>
038   *
039   * <p>Implementations of this method should not call {@link #list()}, since that method is typically implemented in
040   * terms of this one, or undefined behavior may result.</p>
041   *
042   * @param criteria the criteria to use; may be {@code null}
043   *
044   * @return an immutable sublist of this {@link Selectable}'s elements; never {@code null}
045   *
046   * @see #list()
047   */
048  // Filters this thing according to the supplied criteria, producing a List.
049  // List not Stream to permit caching
050  // List not Collection so equals() is well-defined
051  // List is unmodifiable and is always valid for the supplied criteria (unenforceable)
052  // C and not Predicate because equality semantics for Predicate are not well-defined (caching again)
053  public List<E> select(final C criteria);
054
055  /**
056   * Returns an immutable {@link List} of all of this {@link Selectable}'s elements.
057   *
058   * <p>Implementations of this method must be idempotent and must return a determinate value.</p>
059   *
060   * <p>Implementations of this method must not return {@code null}.</p>
061   *
062   * <p>The default implementation of this method calls the {@link #select(Object)} method with {@code null} as the sole
063   * argument.</p>
064   *
065   * @return an immutable {@link List} of all of this {@link Selectable}'s elements; never {@code null}
066   *
067   * @see #select(Object)
068   */
069  public default List<E> list() {
070    return this.select(null);
071  }
072
073}