org.apache.reef.util.Optional类的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(137)

本文整理了Java中org.apache.reef.util.Optional类的一些代码示例,展示了Optional类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Optional类的具体详情如下:
包路径:org.apache.reef.util.Optional
类名称:Optional

Optional介绍

[英]Represents an optional value. Loosely based on The Java 8 version, but filtered for Java 7 compatibility.
[中]表示可选值。松散地基于The Java 8 version,但为Java 7兼容性进行了筛选。

代码示例

代码示例来源:origin: org.apache.reef/reef-utils

/**
 * @param other
 * @return the value of this Optional or other, if no value exists.
 */
public T orElse(final T other) {
 if (isPresent()) {
  return this.get();
 } else {
  return other;
 }
}

代码示例来源:origin: org.apache.reef/reef-runtime-local

/**
 * @return the path to "JAVA_HOME", if that is set. Optional.empty(), else.
 */
private static Optional<Path> getJavaHome() {
 final Optional<String> javaHome = getEnv("JAVA_HOME");
 if (javaHome.isPresent()) {
  final File javaHomeFile = new File(javaHome.get());
  if (javaHomeFile.exists()) {
   return Optional.of(javaHomeFile.toPath());
  }
 }
 return Optional.empty();
}

代码示例来源:origin: org.apache.reef/reef-common

CloseEventImpl(final byte[] theBytes) {
 this.value = Optional.ofNullable(theBytes);
}

代码示例来源:origin: org.apache.reef/reef-utils

/**
 * @return An optional representing the given value, or an empty Optional.
 */
public static <T> Optional<T> ofNullable(final T value) {
 if (null == value) {
  return Optional.empty();
 } else {
  return Optional.of(value);
 }
}

代码示例来源:origin: org.apache.reef/reef-common

/**
 * Called by the child context when it has been closed.
 */
private void resetChildContext() {
 synchronized (this.contextLifeCycle) {
  if (this.childContext.isPresent()) {
   this.childContext = Optional.empty();
  } else {
   throw new IllegalStateException("no child context set");
  }
 }
}

代码示例来源:origin: org.apache.reef/reef-runtime-local

/**
 * Allocates a container based on a request event. First it tries to match a
 * given node, if it cannot, it tries to get a spot in a rack.
 * @param requestEvent resource request event.
 * @return an optional with the container if allocated.
 */
Optional<Container> allocateContainer(final ResourceRequestEvent requestEvent) {
 Container container = null;
 final Optional<String> nodeName = getPreferredNode(requestEvent.getNodeNameList());
 if (nodeName.isPresent()) {
  container = allocateBasedOnNode(
    requestEvent.getMemorySize().orElse(this.defaultMemorySize),
    requestEvent.getVirtualCores().orElse(this.defaultNumberOfCores),
    nodeName.get());
 } else {
  final Optional<String> rackName = getPreferredRack(requestEvent.getRackNameList());
  if (rackName.isPresent()) {
   container = allocateBasedOnRack(
     requestEvent.getMemorySize().orElse(this.defaultMemorySize),
     requestEvent.getVirtualCores().orElse(this.defaultNumberOfCores),
     rackName.get());
  }
 }
 return Optional.ofNullable(container);
}

代码示例来源:origin: org.apache.reef/reef-common

@Override
public Optional<Throwable> fromBytes(final Optional<byte[]> bytes) {
 if (bytes.isPresent()) {
  return this.fromBytes(bytes.get());
 } else {
  return Optional.empty();
 }
}

代码示例来源:origin: org.apache.reef/reef-io

public static AvroFailure toAvroFailure(final Failure failure) {
 final String reason;
 if (failure.getReason().isPresent()) {
  reason = convertThrowableToString(failure.getReason().get());
 } else {
  reason = null;
 }
 return AvroFailure.newBuilder()
   .setAsError(convertThrowableToString(failure.asError()))
   .setData(unwrapOptionalByteArray(failure.getData()))
   .setDescription(failure.getDescription().orElse(null))
   .setId(failure.getId())
   .setMessage(failure.getMessage())
   .setReason(reason)
   .build();
}

代码示例来源:origin: org.apache.reef/reef-common

@SuppressWarnings("checkstyle:hiddenfield")
public synchronized void set(final T value) {
 if (this.value.isPresent()) {
  throw new IllegalStateException("Trying to set new value " + value +
    " while an old value was already present: " + this.value);
 }
 this.value = Optional.of(value);
}

代码示例来源:origin: org.apache.reef/reef-runtime-yarn

/**
 * @return the registered registration.
 */
synchronized RegisterApplicationMasterResponse getRegistration() {
 return registration.get();
}

代码示例来源:origin: org.apache.reef/reef-runtime-yarn

/**
 * Extract the number of maximum application attempts on the job.
 */
private Integer getMaxApplicationSubmissions(final JobSubmissionEvent jobSubmissionEvent) {
 return jobSubmissionEvent.getMaxApplicationSubmissions().orElse(1);
}

代码示例来源:origin: org.apache.reef/reef-common

@Override
public JavaLaunchCommandBuilder setConfigurationFilePaths(final List<String> configurationPaths) {
 this.evaluatorConfigurationPaths = Optional.of(configurationPaths);
 return this;
}

代码示例来源:origin: org.apache.reef/reef-common

@Override
public String toString() {
 // Replace with String.join() after migration to Java 1.8
 final StringBuilder allocatedContainers = new StringBuilder();
 for (String container : this.containerAllocationList) {
  if (allocatedContainers.length() > 0) {
   allocatedContainers.append(',');
  }
  allocatedContainers.append(container);
 }
 return String.format(
   "RuntimeStatusEventImpl:{name:%s, state:%s, allocated:[%s], outstanding:%d, error:%s}",
   this.name, this.state, allocatedContainers, this.outstandingContainerRequests.orElse(0),
   this.error.isPresent());
}

代码示例来源:origin: org.apache.reef/reef-runtime-yarn

/**
 * @return true, if a registration was set.
 */
synchronized boolean isPresent() {
 return this.registration.isPresent();
}

代码示例来源:origin: org.apache.reef/reef-common

@Override
 public Optional<ContextMessage> getMessage() {
  return Optional.empty();
 }
}

代码示例来源:origin: org.apache.reef/reef-runtime-yarn

private static Optional<String> getUserBoundJobSubmissionDirectory(final Configuration configuration) {
 try {
  return Optional.ofNullable(Tang.Factory.getTang().newInjector(configuration)
    .getNamedInstance(DriverJobSubmissionDirectory.class));
 } catch (final InjectionException ex) {
  return Optional.empty();
 }
}

代码示例来源:origin: apache/reef

/**
 * @return An optional representing the given value, or an empty Optional.
 */
public static <T> Optional<T> ofNullable(final T value) {
 if (null == value) {
  return Optional.empty();
 } else {
  return Optional.of(value);
 }
}

代码示例来源:origin: org.apache.reef/reef-common

/**
 * Set the remote ID used to communicate with this Evaluator.
 *
 * @param evaluatorRID
 * @throws java.lang.IllegalStateException if the remote ID has been set before.
 */
synchronized void setRemoteID(final String evaluatorRID) {
 if (this.wrapped.isPresent()) {
  throw new IllegalStateException("Trying to reset the evaluator ID. This isn't supported.");
 } else {
  LOG.log(Level.FINE, "Registering remoteId [{0}] for Evaluator [{1}]", new Object[]{evaluatorRID, evaluatorId});
  this.wrapped = Optional.of(remoteManager.getHandler(evaluatorRID,
    EvaluatorRuntimeProtocol.EvaluatorControlProto.class));
 }
}

代码示例来源:origin: org.apache.reef/reef-runtime-yarn

private synchronized Resource getResource(final ResourceRequestEvent resourceRequestEvent) {
 final Resource result = Records.newRecord(Resource.class);
 final int memory = getMemory(resourceRequestEvent.getMemorySize().get());
 final int core = resourceRequestEvent.getVirtualCores().get();
 LOG.log(Level.FINEST, "Resource requested: memory = {0}, virtual core count = {1}.", new Object[]{memory, core});
 result.setMemory(memory);
 result.setVirtualCores(core);
 return result;
}

代码示例来源:origin: org.apache.reef/reef-common

@Override
 public void onNext(final FailedTask failedTask) {
  throw new RuntimeException("No handler bound for FailedTask: " + failedTask, failedTask.getReason().orElse(null));
 }
}

相关文章