1 /* 2 * Copyright 2016 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 static com.google.common.base.Preconditions.checkNotNull; 33 34 import com.google.api.core.ApiFunction; 35 import com.google.api.gax.longrunning.OperationSnapshot; 36 import com.google.api.gax.retrying.TimedRetryAlgorithm; 37 38 /** 39 * A settings class to configure an {@link OperationCallable} for calls to initiate, resume, and 40 * cancel a long-running operation. 41 */ 42 public final class OperationCallSettings<RequestT, ResponseT, MetadataT> { 43 private final UnaryCallSettings<RequestT, OperationSnapshot> initialCallSettings; 44 private final TimedRetryAlgorithm pollingAlgorithm; 45 private final ApiFunction<OperationSnapshot, ResponseT> responseTransformer; 46 private final ApiFunction<OperationSnapshot, MetadataT> metadataTransformer; 47 getInitialCallSettings()48 public final UnaryCallSettings<RequestT, OperationSnapshot> getInitialCallSettings() { 49 return initialCallSettings; 50 } 51 getPollingAlgorithm()52 public final TimedRetryAlgorithm getPollingAlgorithm() { 53 return pollingAlgorithm; 54 } 55 getResponseTransformer()56 public final ApiFunction<OperationSnapshot, ResponseT> getResponseTransformer() { 57 return responseTransformer; 58 } 59 getMetadataTransformer()60 public final ApiFunction<OperationSnapshot, MetadataT> getMetadataTransformer() { 61 return metadataTransformer; 62 } 63 OperationCallSettings( UnaryCallSettings<RequestT, OperationSnapshot> initialCallSettings, TimedRetryAlgorithm pollingAlgorithm, ApiFunction<OperationSnapshot, ResponseT> responseTransformer, ApiFunction<OperationSnapshot, MetadataT> metadataTransformer)64 private OperationCallSettings( 65 UnaryCallSettings<RequestT, OperationSnapshot> initialCallSettings, 66 TimedRetryAlgorithm pollingAlgorithm, 67 ApiFunction<OperationSnapshot, ResponseT> responseTransformer, 68 ApiFunction<OperationSnapshot, MetadataT> metadataTransformer) { 69 this.initialCallSettings = checkNotNull(initialCallSettings); 70 this.pollingAlgorithm = checkNotNull(pollingAlgorithm); 71 this.responseTransformer = checkNotNull(responseTransformer); 72 this.metadataTransformer = metadataTransformer; 73 } 74 75 /** Create a new builder which can construct an instance of OperationCallSettings. */ 76 public static <RequestT, ResponseT, MetadataT> newBuilder()77 Builder<RequestT, ResponseT, MetadataT> newBuilder() { 78 return new Builder<>(); 79 } 80 toBuilder()81 public final Builder<RequestT, ResponseT, MetadataT> toBuilder() { 82 return new Builder<>(this); 83 } 84 85 public static class Builder<RequestT, ResponseT, MetadataT> { 86 private UnaryCallSettings<RequestT, OperationSnapshot> initialCallSettings; 87 private TimedRetryAlgorithm pollingAlgorithm; 88 private ApiFunction<OperationSnapshot, ResponseT> responseTransformer; 89 private ApiFunction<OperationSnapshot, MetadataT> metadataTransformer; 90 Builder()91 public Builder() {} 92 Builder(OperationCallSettings<RequestT, ResponseT, MetadataT> settings)93 public Builder(OperationCallSettings<RequestT, ResponseT, MetadataT> settings) { 94 this.initialCallSettings = settings.initialCallSettings.toBuilder().build(); 95 this.pollingAlgorithm = settings.pollingAlgorithm; 96 this.responseTransformer = settings.responseTransformer; 97 this.metadataTransformer = settings.metadataTransformer; 98 } 99 100 /** Set the polling algorithm of the operation. */ setPollingAlgorithm( TimedRetryAlgorithm pollingAlgorithm)101 public Builder<RequestT, ResponseT, MetadataT> setPollingAlgorithm( 102 TimedRetryAlgorithm pollingAlgorithm) { 103 this.pollingAlgorithm = pollingAlgorithm; 104 return this; 105 } 106 107 /** Get the polling algorithm of the operation. */ getPollingAlgorithm()108 public TimedRetryAlgorithm getPollingAlgorithm() { 109 return pollingAlgorithm; 110 } 111 112 /** Set the call settings which are used on the call to initiate the operation. */ setInitialCallSettings( UnaryCallSettings<RequestT, OperationSnapshot> initialCallSettings)113 public Builder<RequestT, ResponseT, MetadataT> setInitialCallSettings( 114 UnaryCallSettings<RequestT, OperationSnapshot> initialCallSettings) { 115 this.initialCallSettings = initialCallSettings; 116 return this; 117 } 118 119 /** Get the call settings which are used on the call to initiate the operation. */ getInitialCallSettings()120 public UnaryCallSettings<RequestT, OperationSnapshot> getInitialCallSettings() { 121 return initialCallSettings; 122 } 123 getResponseTransformer()124 public final ApiFunction<OperationSnapshot, ResponseT> getResponseTransformer() { 125 return responseTransformer; 126 } 127 setResponseTransformer( ApiFunction<OperationSnapshot, ResponseT> responseTransformer)128 public Builder<RequestT, ResponseT, MetadataT> setResponseTransformer( 129 ApiFunction<OperationSnapshot, ResponseT> responseTransformer) { 130 this.responseTransformer = responseTransformer; 131 return this; 132 } 133 getMetadataTransformer()134 public final ApiFunction<OperationSnapshot, MetadataT> getMetadataTransformer() { 135 return metadataTransformer; 136 } 137 setMetadataTransformer( ApiFunction<OperationSnapshot, MetadataT> metadataTransformer)138 public Builder<RequestT, ResponseT, MetadataT> setMetadataTransformer( 139 ApiFunction<OperationSnapshot, MetadataT> metadataTransformer) { 140 this.metadataTransformer = metadataTransformer; 141 return this; 142 } 143 build()144 public OperationCallSettings<RequestT, ResponseT, MetadataT> build() { 145 return new OperationCallSettings<>( 146 initialCallSettings, pollingAlgorithm, responseTransformer, metadataTransformer); 147 } 148 } 149 } 150