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