io.grpc.CallOptions Maven / Gradle / Ivy
/*
* Copyright 2015, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package io.grpc;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* The collection of runtime options for a new RPC call.
*
* A field that is not set is {@code null}.
*/
@Immutable
public final class CallOptions {
/**
* A blank {@code CallOptions} that all fields are not set.
*/
public static final CallOptions DEFAULT = new CallOptions();
// Although {@code CallOptions} is immutable, its fields are not final, so that we can initialize
// them outside of constructor. Otherwise the constructor will have a potentially long list of
// unnamed arguments, which is undesirable.
private Long deadlineNanoTime;
/**
* Returns a new {@code CallOptions} with the given absolute deadline in nanoseconds in the clock
* as per {@link System#nanoTime()}.
*
*
This is mostly used for propagating an existing deadline. {@link #withDeadlineAfter} is the
* recommended way of setting a new deadline,
*
* @param deadlineNanoTime the deadline in the clock as per {@link System#nanoTime()}.
* {@code null} for unsetting the deadline.
*/
public CallOptions withDeadlineNanoTime(@Nullable Long deadlineNanoTime) {
CallOptions newOptions = new CallOptions(this);
newOptions.deadlineNanoTime = deadlineNanoTime;
return newOptions;
}
/**
* Returns a new {@code CallOptions} with a deadline that is after the given {@code duration} from
* now.
*/
public CallOptions withDeadlineAfter(long duration, TimeUnit unit) {
return withDeadlineNanoTime(System.nanoTime() + unit.toNanos(duration));
}
/**
* Returns the deadline in nanoseconds in the clock as per {@link System#nanoTime()}. {@code null}
* if the deadline is not set.
*/
@Nullable
public Long getDeadlineNanoTime() {
return deadlineNanoTime;
}
private CallOptions() {
}
/**
* Copy constructor.
*/
private CallOptions(CallOptions other) {
deadlineNanoTime = other.deadlineNanoTime;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("[deadlineNanoTime=").append(deadlineNanoTime);
if (deadlineNanoTime != null) {
buffer.append(" (").append(deadlineNanoTime - System.nanoTime()).append(" ns from now)");
}
buffer.append("]");
return buffer.toString();
}
}