本文整理了Java中com.google.common.base.Optional.or()
方法的一些代码示例,展示了Optional.or()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional.or()
方法的具体详情如下:
包路径:com.google.common.base.Optional
类名称:Optional
方法名:or
[英]Returns this Optional if it has a value present; secondChoice otherwise.
Comparison to java.util.Optional: this method has no equivalent in Java 8's Optional class; write thisOptional.isPresent() ? thisOptional : secondChoiceinstead.
[中]如果存在值,则返回此可选值;第二个选择是另一个。
与java的比较。util。可选:这个方法在Java8的可选类中没有等价物;写下这个选项。isPresent()?此选项可选:第二选项开始。
代码示例来源:origin: spotify/helios
private TaskStatus(final Builder builder) {
this.job = checkNotNull(builder.job, "job");
this.goal = checkNotNull(builder.goal, "goal");
this.state = checkNotNull(builder.state, "state");
// Optional
this.containerId = builder.containerId;
this.throttled = Optional.fromNullable(builder.throttled).or(ThrottleState.NO);
this.ports = Optional.fromNullable(builder.ports).or(EMPTY_PORTS);
this.env = Optional.fromNullable(builder.env).or(Maps.<String, String>newHashMap());
this.containerError = Optional.fromNullable(builder.containerError).or("");
}
代码示例来源:origin: dreamhead/moco
@Override
public Object exec(final List arguments) {
Optional<Long> range = getRange(arguments);
Optional<? extends NumberFormat> format = getFormat(arguments);
double result = new Random().nextDouble() * range.or(1L);
if (format.isPresent()) {
return format.get().format(result);
}
return result;
}
代码示例来源:origin: apache/incubator-gobblin
protected StandardGobblinInstanceLauncher(String name,
Configurable instanceConf,
StandardGobblinInstanceDriver.Builder driverBuilder,
Optional<MetricContext> metricContext,
Optional<Logger> log,
SharedResourcesBroker<GobblinScopeTypes> instanceBroker) {
_log = log.or(LoggerFactory.getLogger(getClass()));
_name = name;
_instanceConf = instanceConf;
_driver = driverBuilder.withInstanceEnvironment(this).build();
_instrumentationEnabled = metricContext.isPresent();
_metricContext = metricContext.orNull();
_instanceBroker = instanceBroker;
}
代码示例来源:origin: apache/incubator-gobblin
Optional<String> defaultTopic = Optional.fromNullable(properties.getProperty(ConfigurationKeys.METRICS_KAFKA_TOPIC));
Optional<String> metricsTopic = Optional.fromNullable(
properties.getProperty(ConfigurationKeys.METRICS_KAFKA_TOPIC_METRICS));
Optional<String> eventsTopic = Optional.fromNullable(
properties.getProperty(ConfigurationKeys.METRICS_KAFKA_TOPIC_EVENTS));
boolean metricsEnabled = metricsTopic.or(defaultTopic).isPresent();
if (metricsEnabled) log.info("Reporting metrics to Kafka");
boolean eventsEnabled = eventsTopic.or(defaultTopic).isPresent();
if (eventsEnabled) log.info("Reporting events to Kafka");
Preconditions.checkArgument(properties.containsKey(ConfigurationKeys.METRICS_KAFKA_BROKERS),
"Kafka metrics brokers missing.");
Preconditions.checkArgument(metricsTopic.or(eventsTopic).or(defaultTopic).isPresent(), "Kafka topic missing.");
} catch (IllegalArgumentException exception) {
log.error("Not reporting metrics to Kafka due to missing Kafka configuration(s).", exception);
try {
formatEnum.metricReporterBuilder(properties)
.build(brokers, metricsTopic.or(defaultTopic).get(), properties);
} catch (IOException exception) {
log.error("Failed to create Kafka metrics reporter. Will not report metrics to Kafka.", exception);
builder.withPusherClassName(pusherClassName);
return builder.build(brokers, eventsTopic.or(defaultTopic).get());
} catch (IOException exception) {
log.error("Failed to create Kafka events reporter. Will not report events to Kafka.", exception);
代码示例来源:origin: apache/incubator-gobblin
protected InstrumentedDataWriterBase(State state, Optional<Class<?>> classTag) {
this.closer = Closer.create();
this.instrumentationEnabled = GobblinMetrics.isEnabled(state);
this.metricContext = this.closer.register(Instrumented.getMetricContext(state, classTag.or(this.getClass())));
if (this.instrumentationEnabled) {
this.writerMetricsUpdater = Optional.of(buildWriterMetricsUpdater());
scheduleWriterMetricsUpdater(this.writerMetricsUpdater.get(), getWriterMetricsUpdaterInterval(state));
} else {
this.writerMetricsUpdater = Optional.absent();
}
regenerateMetrics();
}
代码示例来源:origin: apache/incubator-gobblin
if (!portStart.isPresent() && !portEnd.isPresent()) {
for (int i = 0; i < 65535; i++) {
try {
for (int port = portStart.or(MINIMUM_PORT); port <= portEnd.or(MAXIMUM_PORT); port++) {
try {
this.portLocator.specific(port);
代码示例来源:origin: spotify/helios
this.hostname = Optional.fromNullable(hostname).orNull();
this.created = Optional.fromNullable(created).orNull();
this.command = Optional.fromNullable(command).or(EMPTY_COMMAND);
this.env = Optional.fromNullable(env).or(EMPTY_ENV);
this.resources = Optional.fromNullable(resources).orNull();
this.ports = Optional.fromNullable(ports).or(EMPTY_PORTS);
this.registration = Optional.fromNullable(registration).or(EMPTY_REGISTRATION);
this.gracePeriod = Optional.fromNullable(gracePeriod).orNull();
this.volumes = Optional.fromNullable(volumes).or(EMPTY_VOLUMES);
this.expires = expires;
this.registrationDomain = Optional.fromNullable(registrationDomain)
.or(EMPTY_REGISTRATION_DOMAIN);
this.creatingUser = Optional.fromNullable(creatingUser).orNull();
this.token = Optional.fromNullable(token).or(EMPTY_TOKEN);
this.healthCheck = Optional.fromNullable(healthCheck).orNull();
this.securityOpt = Optional.fromNullable(securityOpt).or(EMPTY_SECURITY_OPT);
this.networkMode = Optional.fromNullable(networkMode).orNull();
this.metadata = Optional.fromNullable(metadata).or(EMPTY_METADATA);
this.addCapabilities = firstNonNull(addCapabilities, EMPTY_CAPS);
this.dropCapabilities = firstNonNull(dropCapabilities, EMPTY_CAPS);
this.labels = Optional.fromNullable(labels).or(EMPTY_LABELS);
this.secondsToWaitBeforeKill = secondsToWaitBeforeKill;
this.ramdisks = firstNonNull(ramdisks, EMPTY_RAMDISKS);
代码示例来源:origin: HubSpot/Singularity
@Override
public SingularityLoadBalancerUpdate enqueue(LoadBalancerRequestId loadBalancerRequestId, SingularityRequest request, SingularityDeploy deploy, List<SingularityTask> add,
List<SingularityTask> remove) {
final List<String> serviceOwners = request.getOwners().or(Collections.<String> emptyList());
final Set<String> loadBalancerGroups = deploy.getLoadBalancerGroups().or(Collections.<String>emptySet());
final BaragonService lbService = new BaragonService(deploy.getLoadBalancerServiceIdOverride().or(request.getId()), serviceOwners, deploy.getServiceBasePath().get(),
deploy.getLoadBalancerAdditionalRoutes().or(Collections.<String>emptyList()), loadBalancerGroups, deploy.getLoadBalancerOptions().orNull(),
deploy.getLoadBalancerTemplate(), deploy.getLoadBalancerDomains().or(Collections.<String>emptySet()));
final List<UpstreamInfo> addUpstreams = tasksToUpstreams(add, loadBalancerRequestId.toString(), deploy.getLoadBalancerUpstreamGroup());
final List<UpstreamInfo> removeUpstreams = tasksToUpstreams(remove, loadBalancerRequestId.toString(), deploy.getLoadBalancerUpstreamGroup());
final BaragonRequest loadBalancerRequest = new BaragonRequest(loadBalancerRequestId.toString(), lbService, addUpstreams, removeUpstreams);
return sendBaragonRequest(loadBalancerRequestId, loadBalancerRequest, LoadBalancerMethod.ENQUEUE);
}
代码示例来源:origin: apache/incubator-gobblin
public KafkaEventReporter(Builder<?> builder) throws IOException {
super(builder);
this.serializer = this.closer.register(
createSerializer(new FixedSchemaVersionWriter()));
if(builder.kafkaPusher.isPresent()) {
this.kafkaPusher = builder.kafkaPusher.get();
} else {
String pusherClassName = builder.pusherClassName.or(PusherUtils.DEFAULT_KAFKA_PUSHER_CLASS_NAME);
this.kafkaPusher = PusherUtils.getPusher(pusherClassName, builder.brokers, builder.topic, builder.config);
}
this.closer.register(this.kafkaPusher);
}
代码示例来源:origin: apache/usergrid
Preconditions.checkArgument(collectionDeleteRequestBuilder.getCollectionName().isPresent(),
"You must specify a collection name");
String collectionName = collectionDeleteRequestBuilder.getCollectionName().get();
Preconditions.checkArgument( !(cursor.isPresent() && appId.isPresent()),
"You cannot specify an app id and a cursor. When resuming with cursor you must omit the appid." );
Preconditions.checkArgument( cursor.isPresent() || appId.isPresent(),
if (appId.isPresent()) {
applicationScope = appId.get();
} else { // cursor is present
applicationScope = cursor.get().getApplicationScope();
final long endTimestamp = collectionDeleteRequestBuilder.getEndTimestamp().or( System.currentTimeMillis() );
fromNullable(collectionName), cursorSeek.getSeekValue() )
.buffer( indexProcessorFig.getCollectionDeleteBufferSize())
.doOnNext( edgeScopes -> {
代码示例来源:origin: spotify/helios
private static String env(final String name, final String defaultValue) {
return Optional.fromNullable(getenv(name)).or(defaultValue);
}
代码示例来源:origin: google/truth
/** Returns {@code actual().get()}, or {@code expected().get()}, whichever is available. */
@Memoized
Object actualOrExpected() {
return actual().or(expected()).get();
}
代码示例来源:origin: PipelineAI/pipeline
private FallbackMethod doFind(Class<?> enclosingType, Method commandMethod, boolean extended) {
String name = getFallbackName(enclosingType, commandMethod);
Class<?>[] fallbackParameterTypes = null;
if (isDefault()) {
fallbackParameterTypes = new Class[0];
} else {
fallbackParameterTypes = commandMethod.getParameterTypes();
}
if (extended && fallbackParameterTypes[fallbackParameterTypes.length - 1] == Throwable.class) {
fallbackParameterTypes = ArrayUtils.remove(fallbackParameterTypes, fallbackParameterTypes.length - 1);
}
Class<?>[] extendedFallbackParameterTypes = Arrays.copyOf(fallbackParameterTypes,
fallbackParameterTypes.length + 1);
extendedFallbackParameterTypes[fallbackParameterTypes.length] = Throwable.class;
Optional<Method> exFallbackMethod = getMethod(enclosingType, name, extendedFallbackParameterTypes);
Optional<Method> fMethod = getMethod(enclosingType, name, fallbackParameterTypes);
Method method = exFallbackMethod.or(fMethod).orNull();
if (method == null) {
throw new FallbackDefinitionException("fallback method wasn't found: " + name + "(" + Arrays.toString(fallbackParameterTypes) + ")");
}
return new FallbackMethod(method, exFallbackMethod.isPresent(), isDefault());
}
代码示例来源:origin: spotify/helios
/**
* Stops this container.
*/
public void stop() throws InterruptedException {
// Tell docker to stop or eventually kill the container
final String container = containerId.or(containerName);
// Interrupt the thread blocking on waitContainer
stopAsync().awaitTerminated();
if (System.getenv("CONTAINER_STATS") != null) {
try {
log.info("container {} stats: {}", containerName, docker.stats(containerName));
} catch (DockerException e) {
log.warn("Could not log container stats. Exception was {}", e);
}
}
try {
docker.stopContainer(container, secondsToWaitBeforeKill);
} catch (DockerException e) {
if ((e instanceof ContainerNotFoundException) && !containerId.isPresent()) {
// we tried to stop the container by name but no container of the given name existed.
// this isn't surprising or exceptional, just means the container wasn't started yet.
} else {
log.warn("Stopping container {} failed", container, e);
}
}
}
代码示例来源:origin: embulk/embulk
@Deprecated
public TimestampFormatter(final Task task, final Optional<? extends TimestampColumnOption> columnOption) {
this(TimestampFormatterRuby.ofLegacy(
columnOption.isPresent()
? columnOption.get().getFormat().or(task.getDefaultTimestampFormat())
: task.getDefaultTimestampFormat(),
columnOption.isPresent()
? columnOption.get().getTimeZone().or(task.getDefaultTimeZone())
: task.getDefaultTimeZone()));
}
代码示例来源:origin: apache/incubator-gobblin
this.targetURI = Optional.fromNullable(this.dataset.getProperties().getProperty(TARGET_METASTORE_URI_KEY));
this.targetClientPool = HiveMetastoreClientPool.get(this.dataset.getProperties(), this.targetURI);
this.targetDatabase = Optional.fromNullable(this.dataset.getProperties().getProperty(TARGET_DATABASE_KEY))
.or(this.dataset.table.getDbName());
this.existingEntityPolicy = ExistingEntityPolicy.valueOf(this.dataset.getProperties()
.getProperty(EXISTING_ENTITY_POLICY_KEY, DEFAULT_EXISTING_ENTITY_POLICY).toUpperCase());
this.partitionFilter = Optional.of(generator.getFilter(this.dataset));
log.info(String.format("Dynamic partition filter for table %s: %s.", this.dataset.table.getCompleteName(),
this.partitionFilter.get()));
} catch (ReflectiveOperationException roe) {
throw new IOException(roe);
Optional.fromNullable(this.dataset.getProperties().getProperty(COPY_PARTITIONS_FILTER_CONSTANT));
this.tableRegistrationStep = Optional.of(tableRegistrationStep);
if (this.existingTargetTable.isPresent() && this.existingTargetTable.get().isPartitioned()) {
checkPartitionedTableCompatibility(this.targetTable, this.existingTargetTable.get());
this.existingTargetTable.isPresent() ? Maps.newHashMap(
HiveUtils.getPartitionsMap(multiClient.getClient(target_client),
this.existingTargetTable.get(), this.partitionFilter, this.hivePartitionExtendedFilter))
代码示例来源:origin: spotify/docker-client
public String getHost() {
return fromNullable(uri.getHost()).or("localhost");
}
代码示例来源:origin: google/error-prone
@Override
public List<JCStatement> inlineStatements(final Inliner inliner)
throws CouldNotResolveImportException {
try {
Optional<List<JCStatement>> binding = inliner.getOptionalBinding(placeholder().blockKey());
// If a placeholder was used as an expression binding in the @BeforeTemplate,
// and as a bare statement or as a return in the @AfterTemplate, we may need to convert.
Optional<JCExpression> exprBinding = inliner.getOptionalBinding(placeholder().exprKey());
binding =
binding.or(
exprBinding.transform(
(JCExpression expr) -> {
switch (implementationFlow()) {
case NEVER_EXITS:
return List.of((JCStatement) inliner.maker().Exec(expr));
case ALWAYS_RETURNS:
return List.of((JCStatement) inliner.maker().Return(expr));
default:
throw new AssertionError();
}
}));
return UPlaceholderExpression.copier(arguments(), inliner).copy(binding.get(), inliner);
} catch (UncheckedCouldNotResolveImportException e) {
throw e.getCause();
}
}
}
代码示例来源:origin: HubSpot/Singularity
public SingularityRequestParent skipHealthchecks(String requestId, SingularitySkipHealthchecksRequest skipHealthchecksRequest, SingularityUser user) {
SingularityRequestWithState oldRequestWithState = fetchRequestWithState(requestId, user);
SingularityRequest oldRequest = oldRequestWithState.getRequest();
SingularityRequest newRequest = oldRequest.toBuilder().setSkipHealthchecks(skipHealthchecksRequest.getSkipHealthchecks()).build();
submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.absent(), Optional.absent(), skipHealthchecksRequest.getMessage(), Optional.absent(), user);
if (skipHealthchecksRequest.getDurationMillis().isPresent()) {
requestManager.saveExpiringObject(new SingularityExpiringSkipHealthchecks(requestId, user.getEmail(),
System.currentTimeMillis(), skipHealthchecksRequest, oldRequest.getSkipHealthchecks(), skipHealthchecksRequest.getActionId().or(UUID.randomUUID().toString())));
}
return fillEntireRequest(fetchRequestWithState(requestId, user));
}
代码示例来源:origin: apache/incubator-gobblin
public JobCatalogBase(Optional<Logger> log, Optional<MetricContext> parentMetricContext,
boolean instrumentationEnabled, Optional<Config> sysConfig) {
this.log = log.isPresent() ? log.get() : LoggerFactory.getLogger(getClass());
this.listeners = new JobCatalogListenersList(log);
if (instrumentationEnabled) {
MetricContext realParentCtx =
parentMetricContext.or(Instrumented.getMetricContext(new org.apache.gobblin.configuration.State(), getClass()));
this.metricContext = realParentCtx.childBuilder(JobCatalog.class.getSimpleName()).build();
this.metrics = createStandardMetrics(sysConfig);
this.addListener(this.metrics);
}
else {
this.metricContext = null;
this.metrics = null;
}
}
内容来源于网络,如有侵权,请联系作者删除!