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
038 @Override
039 protected abstract Service delegate();
040
041 public ListenableFuture<State> start() {
042 return delegate().start();
043 }
044
045 public State state() {
046 return delegate().state();
047 }
048
049 public ListenableFuture<State> stop() {
050 return delegate().stop();
051 }
052
053 public State startAndWait() {
054 return delegate().startAndWait();
055 }
056
057 public State stopAndWait() {
058 return delegate().stopAndWait();
059 }
060
061 public boolean isRunning() {
062 return delegate().isRunning();
063 }
064
065 public void addListener(Listener listener, Executor executor) {
066 delegate().addListener(listener, executor);
067 }
068
069 /**
070 * A sensible default implementation of {@link #startAndWait()}, in terms of
071 * {@link #start}. If you override {@link #start}, you may wish to override
072 * {@link #startAndWait()} to forward to this implementation.
073 * @since 9.0
074 */
075 protected State standardStartAndWait() {
076 return Futures.getUnchecked(start());
077 }
078
079 /**
080 * A sensible default implementation of {@link #stopAndWait()}, in terms of
081 * {@link #stop}. If you override {@link #stop}, you may wish to override
082 * {@link #stopAndWait()} to forward to this implementation.
083 * @since 9.0
084 */
085 protected State standardStopAndWait() {
086 return Futures.getUnchecked(stop());
087 }
088 }