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
047public abstract class ForwardingObject {
048
049  /** Constructor for use by subclasses. */
050  protected ForwardingObject() {}
051
052  /**
053   * Returns the backing delegate instance that methods are forwarded to. Abstract subclasses
054   * generally override this method with an abstract method that has a more specific return type,
055   * such as {@link ForwardingSet#delegate}. Concrete subclasses override this method to supply the
056   * instance being decorated.
057   */
058  protected abstract Object delegate();
059
060  /** Returns the string representation generated by the delegate's {@code toString} method. */
061  @Override
062  public String toString() {
063    return delegate().toString();
064  }
065
066  /* No equals or hashCode. See class comments for details. */
067}