Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2012 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 java.util.Deque;
     20 import java.util.Iterator;
     21 
     22 /**
     23  * A deque which forwards all its method calls to another deque. Subclasses
     24  * should override one or more methods to modify the behavior of the backing
     25  * deque as desired per the <a
     26  * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
     27  *
     28  * <p><b>Warning:</b> The methods of {@code ForwardingDeque} forward
     29  * <b>indiscriminately</b> to the methods of the delegate. For example,
     30  * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
     31  * #offer} which can lead to unexpected behavior. In this case, you should
     32  * override {@code offer} as well.
     33  *
     34  * @author Kurt Alfred Kluever
     35  * @since 12.0
     36  */
     37 public abstract class ForwardingDeque<E> extends ForwardingQueue<E>
     38     implements Deque<E> {
     39 
     40   /** Constructor for use by subclasses. */
     41   protected ForwardingDeque() {}
     42 
     43   @Override protected abstract Deque<E> delegate();
     44 
     45   @Override
     46   public void addFirst(E e) {
     47     delegate().addFirst(e);
     48   }
     49 
     50   @Override
     51   public void addLast(E e) {
     52     delegate().addLast(e);
     53   }
     54 
     55   @Override
     56   public Iterator<E> descendingIterator() {
     57     return delegate().descendingIterator();
     58   }
     59 
     60   @Override
     61   public E getFirst() {
     62     return delegate().getFirst();
     63   }
     64 
     65   @Override
     66   public E getLast() {
     67     return delegate().getLast();
     68   }
     69 
     70   @Override
     71   public boolean offerFirst(E e) {
     72     return delegate().offerFirst(e);
     73   }
     74 
     75   @Override
     76   public boolean offerLast(E e) {
     77     return delegate().offerLast(e);
     78   }
     79 
     80   @Override
     81   public E peekFirst() {
     82     return delegate().peekFirst();
     83   }
     84 
     85   @Override
     86   public E peekLast() {
     87     return delegate().peekLast();
     88   }
     89 
     90   @Override
     91   public E pollFirst() {
     92     return delegate().pollFirst();
     93   }
     94 
     95   @Override
     96   public E pollLast() {
     97     return delegate().pollLast();
     98   }
     99 
    100   @Override
    101   public E pop() {
    102     return delegate().pop();
    103   }
    104 
    105   @Override
    106   public void push(E e) {
    107     delegate().push(e);
    108   }
    109 
    110   @Override
    111   public E removeFirst() {
    112     return delegate().removeFirst();
    113   }
    114 
    115   @Override
    116   public E removeLast() {
    117     return delegate().removeLast();
    118   }
    119 
    120   @Override
    121   public boolean removeFirstOccurrence(Object o) {
    122     return delegate().removeFirstOccurrence(o);
    123   }
    124 
    125   @Override
    126   public boolean removeLastOccurrence(Object o) {
    127     return delegate().removeLastOccurrence(o);
    128   }
    129 }
    130