001    /*
002     * Copyright (C) 2009 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    
017    package com.google.common.util.concurrent;
018    
019    import com.google.common.annotations.Beta;
020    import com.google.common.collect.ForwardingObject;
021    
022    import java.util.concurrent.Executor;
023    
024    /**
025     * A {@link Service} that forwards all method calls to another service.
026     *
027     * @author Chris Nokleberg
028     * @since 1.0
029     */
030    @Beta
031    public abstract class ForwardingService extends ForwardingObject
032        implements Service {
033    
034      /** Constructor for use by subclasses. */
035      protected ForwardingService() {}
036    
037      @Override protected abstract Service delegate();
038    
039      @Override public ListenableFuture<State> start() {
040        return delegate().start();
041      }
042    
043      @Override public State state() {
044        return delegate().state();
045      }
046    
047      @Override public ListenableFuture<State> stop() {
048        return delegate().stop();
049      }
050    
051      @Override public State startAndWait() {
052        return delegate().startAndWait();
053      }
054    
055      @Override public State stopAndWait() {
056        return delegate().stopAndWait();
057      }
058    
059      @Override public boolean isRunning() {
060        return delegate().isRunning();
061      }
062      
063      @Override public void addListener(Listener listener, Executor executor) {
064        delegate().addListener(listener, executor);
065      }
066      
067      @Override public Throwable failureCause() {
068        return delegate().failureCause();
069      }
070    
071      /**
072       * A sensible default implementation of {@link #startAndWait()}, in terms of
073       * {@link #start}. If you override {@link #start}, you may wish to override
074       * {@link #startAndWait()} to forward to this implementation.
075       * @since 9.0
076       */
077      protected State standardStartAndWait() {
078        return Futures.getUnchecked(start());
079      }
080    
081      /**
082       * A sensible default implementation of {@link #stopAndWait()}, in terms of
083       * {@link #stop}. If you override {@link #stop}, you may wish to override
084       * {@link #stopAndWait()} to forward to this implementation.
085       * @since 9.0
086       */
087      protected State standardStopAndWait() {
088        return Futures.getUnchecked(stop());
089      }
090    }