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.reference;
015
016import java.util.function.Supplier;
017
018/**
019 * An interface whose implementations pretend to be another type and alter the behavior of instances of that type in
020 * some way.
021 *
022 * <p>An implementation of this interface must, somehow, be able to be {@linkplain #$cast() cast} to the type of the
023 * instance it logically proxies.</p>
024 *
025 * @param <T> the proxied type
026 *
027 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
028 *
029 * @see #$proxied()
030 *
031 * @see #$cast()
032 */
033// TODO: this interface may move to another module or package.
034public interface ClientProxy<T> {
035
036  /**
037   * Returns the instance being proxied, not this {@link ClientProxy}.
038   *
039   * @return the instance being proxied; never {@code null}; never {@code this}
040   *
041   * @microbean.nullability Implementations of this method must never return {@code null}.
042   *
043   * @microbean.idempotency Implementations of this method must be idempotent and deterministic. Specifically, they must
044   * always return the instance being proxied, not {@code this} or anything else.
045   *
046   * @microbean.threadsafety Implementations of this method must be safe for concurrent use by multiple threads.
047   */
048  public T $proxied();
049
050  /**
051   * Returns this {@link ClientProxy}, or a completely substitutable replacement for it, cast to the type of the
052   * instance it proxies.
053   *
054   * <p>The default implementation of this method returns, effectively, {@code (T)this}.</p>
055   *
056   * @return this {@link ClientProxy}; never {@code null}
057   *
058   * @exception ClassCastException if the cast could not take place for any reason; this indicates a violation of this
059   * interface's structural contracts by the implementation class
060   *
061   * @microbean.nullability Implementations of this method must never return {@code null}.
062   *
063   * @microbean.idempotency Implementations of this method must be idempotent and deterministic. Specifically, they must
064   * always return this {@link ClientProxy} or a completely substitutable replacement for it, not anything else.
065   *
066   * @microbean.threadsafety Implementations of this method must be safe for concurrent use by multiple threads.
067   */
068  @SuppressWarnings("unchecked")
069  public default T $cast() {
070    return (T)this;
071  }
072
073}