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>. The {@link
025 * #delegate()} method must be overridden to return the instance being decorated.
026 *
027 * <p>This class does <i>not</i> forward the {@code hashCode} and {@code equals} methods through to
028 * the backing object, but relies on {@code Object}'s implementation. This is necessary to preserve
029 * the symmetry of {@code equals}. Custom definitions of equality are usually based on an interface,
030 * such as {@code Set} or {@code List}, so that the implementation of {@code equals} can cast the
031 * object being tested for equality to the custom interface. {@code ForwardingObject} implements no
032 * such custom interfaces directly; they are implemented only in subclasses. Therefore, forwarding
033 * {@code equals} would break symmetry, as the forwarding object might consider itself equal to the
034 * object being tested, but the reverse could not be true. This behavior is consistent with the
035 * JDK's collection wrappers, such as {@link java.util.Collections#unmodifiableCollection}. Use an
036 * interface-specific subclass of {@code ForwardingObject}, such as {@link ForwardingList}, to
037 * preserve equality behavior, or override {@code equals} directly.
038 *
039 * <p>The {@code toString} method is forwarded to the delegate. Although this class does not
040 * implement {@link Serializable}, a serializable subclass may be created since this class has a
041 * parameter-less constructor.
042 *
043 * @author Mike Bostock
044 * @since 2.0
045 */
046@GwtCompatible
047@ElementTypesAreNonnullByDefault
048public abstract class ForwardingObject {
049
050  /** Constructor for use by subclasses. */
051  protected ForwardingObject() {}
052
053  /**
054   * Returns the backing delegate instance that methods are forwarded to. Abstract subclasses
055   * generally override this method with an abstract method that has a more specific return type,
056   * such as {@link ForwardingSet#delegate}. Concrete subclasses override this method to supply the
057   * instance being decorated.
058   */
059  protected abstract Object delegate();
060
061  /** Returns the string representation generated by the delegate's {@code toString} method. */
062  @Override
063  public String toString() {
064    return delegate().toString();
065  }
066
067  /* No equals or hashCode. See class comments for details. */
068}