View Javadoc
1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import java.io.Serializable;
21  
22  /**
23   * An abstract base class for implementing the <a
24   * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
25   * The {@link #delegate()} method must be overridden to return the instance
26   * being decorated.
27   *
28   * <p>This class does <i>not</i> forward the {@code hashCode} and {@code equals}
29   * methods through to the backing object, but relies on {@code Object}'s
30   * implementation. This is necessary to preserve the symmetry of {@code equals}.
31   * Custom definitions of equality are usually based on an interface, such as
32   * {@code Set} or {@code List}, so that the implementation of {@code equals} can
33   * cast the object being tested for equality to the custom interface. {@code
34   * ForwardingObject} implements no such custom interfaces directly; they
35   * are implemented only in subclasses. Therefore, forwarding {@code equals}
36   * would break symmetry, as the forwarding object might consider itself equal to
37   * the object being tested, but the reverse could not be true. This behavior is
38   * consistent with the JDK's collection wrappers, such as
39   * {@link java.util.Collections#unmodifiableCollection}. Use an
40   * interface-specific subclass of {@code ForwardingObject}, such as {@link
41   * ForwardingList}, to preserve equality behavior, or override {@code equals}
42   * directly.
43   *
44   * <p>The {@code toString} method is forwarded to the delegate. Although this
45   * class does not implement {@link Serializable}, a serializable subclass may be
46   * created since this class has a parameter-less constructor.
47   *
48   * @author Mike Bostock
49   * @since 2.0
50   */
51  @GwtCompatible
52  public abstract class ForwardingObject {
53  
54    /** Constructor for use by subclasses. */
55    protected ForwardingObject() {}
56  
57    /**
58     * Returns the backing delegate instance that methods are forwarded to.
59     * Abstract subclasses generally override this method with an abstract method
60     * that has a more specific return type, such as {@link
61     * ForwardingSet#delegate}. Concrete subclasses override this method to supply
62     * the instance being decorated.
63     */
64    protected abstract Object delegate();
65  
66    /**
67     * Returns the string representation generated by the delegate's
68     * {@code toString} method.
69     */
70    @Override
71    public String toString() {
72      return delegate().toString();
73    }
74  
75    /* No equals or hashCode. See class comments for details. */
76  }