Coverage Summary for Class: ForwardingObject (com.google.common.collect)

Class Class, % Method, % Line, %
ForwardingObject 100% (1/1) 50% (1/2) 50% (1/2)


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>. The {@link 25  * #delegate()} method must be overridden to return the instance being decorated. 26  * 27  * <p>This class does <i>not</i> forward the {@code hashCode} and {@code equals} methods through to 28  * the backing object, but relies on {@code Object}'s implementation. This is necessary to preserve 29  * the symmetry of {@code equals}. Custom definitions of equality are usually based on an interface, 30  * such as {@code Set} or {@code List}, so that the implementation of {@code equals} can cast the 31  * object being tested for equality to the custom interface. {@code ForwardingObject} implements no 32  * such custom interfaces directly; they are implemented only in subclasses. Therefore, forwarding 33  * {@code equals} would break symmetry, as the forwarding object might consider itself equal to the 34  * object being tested, but the reverse could not be true. This behavior is consistent with the 35  * JDK's collection wrappers, such as {@link java.util.Collections#unmodifiableCollection}. Use an 36  * interface-specific subclass of {@code ForwardingObject}, such as {@link ForwardingList}, to 37  * preserve equality behavior, or override {@code equals} directly. 38  * 39  * <p>The {@code toString} method is forwarded to the delegate. Although this class does not 40  * implement {@link Serializable}, a serializable subclass may be created since this class has a 41  * parameter-less constructor. 42  * 43  * @author Mike Bostock 44  * @since 2.0 45  */ 46 @GwtCompatible 47 public abstract class ForwardingObject { 48  49  /** Constructor for use by subclasses. */ 50  protected ForwardingObject() {} 51  52  /** 53  * Returns the backing delegate instance that methods are forwarded to. Abstract subclasses 54  * generally override this method with an abstract method that has a more specific return type, 55  * such as {@link ForwardingSet#delegate}. Concrete subclasses override this method to supply the 56  * instance being decorated. 57  */ 58  protected abstract Object delegate(); 59  60  /** Returns the string representation generated by the delegate's {@code toString} method. */ 61  @Override 62  public String toString() { 63  return delegate().toString(); 64  } 65  66  /* No equals or hashCode. See class comments for details. */ 67 }