001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * 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
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.common.annotations.GwtCompatible;
020import java.io.Serializable;
021
022/**
023 * An abstract base class for implementing the <a
024 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
025 * The {@link #delegate()} method must be overridden to return the instance
026 * being decorated.
027 *
028 * <p>This class does <i>not</i> forward the {@code hashCode} and {@code equals}
029 * methods through to the backing object, but relies on {@code Object}'s
030 * implementation. This is necessary to preserve the symmetry of {@code equals}.
031 * Custom definitions of equality are usually based on an interface, such as
032 * {@code Set} or {@code List}, so that the implementation of {@code equals} can
033 * cast the object being tested for equality to the custom interface. {@code
034 * ForwardingObject} implements no such custom interfaces directly; they
035 * are implemented only in subclasses. Therefore, forwarding {@code equals}
036 * would break symmetry, as the forwarding object might consider itself equal to
037 * the object being tested, but the reverse could not be true. This behavior is
038 * consistent with the JDK's collection wrappers, such as
039 * {@link java.util.Collections#unmodifiableCollection}. Use an
040 * interface-specific subclass of {@code ForwardingObject}, such as {@link
041 * ForwardingList}, to preserve equality behavior, or override {@code equals}
042 * directly.
043 *
044 * <p>The {@code toString} method is forwarded to the delegate. Although this
045 * class does not implement {@link Serializable}, a serializable subclass may be
046 * created since this class has a parameter-less constructor.
047 *
048 * @author Mike Bostock
049 * @since 2.0
050 */
051@GwtCompatible
052public abstract class ForwardingObject {
053
054  /** Constructor for use by subclasses. */
055  protected ForwardingObject() {}
056
057  /**
058   * Returns the backing delegate instance that methods are forwarded to.
059   * Abstract subclasses generally override this method with an abstract method
060   * that has a more specific return type, such as {@link
061   * ForwardingSet#delegate}. Concrete subclasses override this method to supply
062   * the instance being decorated.
063   */
064  protected abstract Object delegate();
065
066  /**
067   * Returns the string representation generated by the delegate's
068   * {@code toString} method.
069   */
070  @Override
071  public String toString() {
072    return delegate().toString();
073  }
074
075  /* No equals or hashCode. See class comments for details. */
076}