1 /* 2 * Copyright 2018 Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google LLC nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.google.api.gax.rpc; 31 32 import com.google.common.base.Preconditions; 33 34 /** Base implementation of {@link ResponseObserver} that performs state sanity checks. */ 35 public abstract class StateCheckingResponseObserver<V> implements ResponseObserver<V> { 36 private boolean isStarted; 37 private boolean isClosed; 38 39 /** 40 * {@inheritDoc} 41 * 42 * <p>This implementation simply delegates to {@link #onStartImpl(StreamController)} after 43 * ensuring consistent state. 44 */ onStart(StreamController controller)45 public final void onStart(StreamController controller) { 46 Preconditions.checkState(!isStarted, getClass() + " is already started."); 47 isStarted = true; 48 49 onStartImpl(controller); 50 } 51 52 /** 53 * {@inheritDoc} 54 * 55 * <p>This implementation simply delegates to {@link #onResponseImpl(Object)} after ensuring 56 * consistent state. 57 */ onResponse(V response)58 public final void onResponse(V response) { 59 Preconditions.checkState(!isClosed, getClass() + " received a response after being closed."); 60 onResponseImpl(response); 61 } 62 63 /** 64 * {@inheritDoc} 65 * 66 * <p>This implementation simply delegates to {@link #onCompleteImpl()} after ensuring consistent 67 * state. 68 */ onComplete()69 public final void onComplete() { 70 Preconditions.checkState(!isClosed, getClass() + " tried to double close."); 71 isClosed = true; 72 onCompleteImpl(); 73 } 74 75 /** 76 * {@inheritDoc} 77 * 78 * <p>This implementation simply delegates to {@link #onErrorImpl(Throwable)} after ensuring 79 * consistent state. 80 */ onError(Throwable t)81 public final void onError(Throwable t) { 82 Preconditions.checkState(!isClosed, getClass() + " received error after being closed", t); 83 isClosed = true; 84 onErrorImpl(t); 85 } 86 87 /** @see #onStart(StreamController) */ onStartImpl(StreamController controller)88 protected abstract void onStartImpl(StreamController controller); 89 90 /** @see #onResponse(Object) */ onResponseImpl(V response)91 protected abstract void onResponseImpl(V response); 92 93 /** @see #onErrorImpl(Throwable) */ onErrorImpl(Throwable t)94 protected abstract void onErrorImpl(Throwable t); 95 96 /** @see #onComplete() */ onCompleteImpl()97 protected abstract void onCompleteImpl(); 98 } 99