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      /**
068       * A sensible default implementation of {@link #startAndWait()}, in terms of
069       * {@link #start}. If you override {@link #start}, you may wish to override
070       * {@link #startAndWait()} to forward to this implementation.
071       * @since 9.0
072       */
073      protected State standardStartAndWait() {
074        return Futures.getUnchecked(start());
075      }
076    
077      /**
078       * A sensible default implementation of {@link #stopAndWait()}, in terms of
079       * {@link #stop}. If you override {@link #stop}, you may wish to override
080       * {@link #stopAndWait()} to forward to this implementation.
081       * @since 9.0
082       */
083      protected State standardStopAndWait() {
084        return Futures.getUnchecked(stop());
085      }
086    }