com.google.common.base.Enums.getIfPresent()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(430)

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

Enums.getIfPresent介绍

[英]Returns an optional enum constant for the given type, using Enum#valueOf. If the constant does not exist, Optional#absent is returned. A common use case is for parsing user input or falling back to a default enum constant. For example, Enums.getIfPresent(Country.class, countryInput).or(Country.DEFAULT);
[中]使用enum#valueOf返回给定类型的可选枚举常量。如果常量不存在,则返回可选的#缺席。一个常见的用例是解析用户输入或返回默认枚举常量。例如,枚举。getIfPresent(Country.class,countryInput)。或(国家违约);

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

/**
  * [PDI-15575] Ensuring that this enum can return with some default value. Necessary for when being used on the
  * GUI side if a user leaves the field empty, it will enforce a default value. This allows the object to be saved,
  * loaded, and cloned as necessary.
  * @param contentType
  * @return ContentType
  */
 public static ContentType getIfPresent( String contentType ) {
  return Enums.getIfPresent( ContentType.class, contentType ).or( Element );
 }
}

代码示例来源:origin: google/error-prone

private static Optional<TimeUnit> getTimeUnit(ExpressionTree timeUnit) {
  if (timeUnit instanceof IdentifierTree) { // e.g., SECONDS
   return Enums.getIfPresent(TimeUnit.class, ((IdentifierTree) timeUnit).getName().toString());
  }
  if (timeUnit instanceof MemberSelectTree) { // e.g., TimeUnit.SECONDS
   return Enums.getIfPresent(
     TimeUnit.class, ((MemberSelectTree) timeUnit).getIdentifier().toString());
  }
  return Optional.absent();
 }
}

代码示例来源:origin: apache/incubator-gobblin

private static Optional<Deserializers> getDeserializerType(Properties props) {
 Preconditions.checkArgument(props.containsKey(KAFKA_DESERIALIZER_TYPE),
   "Missing required property " + KAFKA_DESERIALIZER_TYPE);
 return Enums.getIfPresent(Deserializers.class, props.getProperty(KAFKA_DESERIALIZER_TYPE).toUpperCase());
}

代码示例来源:origin: apache/incubator-gobblin

/**
  * Return the {@link TaskSchedulerType} with the specified name. If the specified name
  * does not map to a {@link TaskSchedulerType}, then {@link #SCHEDULEDEXECUTORSERVICE}
  * will be returned.
  *
  * @param name the name of the {@link TaskSchedulerType}
  * @return the specified {@link TaskSchedulerType} or {@link #SCHEDULEDEXECUTORSERVICE}
  */
 public static TaskSchedulerType parse(String name) {
  if (StringUtils.isEmpty(name)) {
   return SCHEDULEDEXECUTORSERVICE;
  }
  return Enums.getIfPresent(TaskSchedulerType.class, name.toUpperCase()).or(SCHEDULEDEXECUTORSERVICE);
 }
}

代码示例来源:origin: apache/incubator-gobblin

/**
  * Get the devliery semantics type from {@link ConfigurationKeys#DELIVERY_SEMANTICS}.
  * The default value is {@link Type#AT_LEAST_ONCE}.
  */
 public static DeliverySemantics parse(State state) {
  String value =
    state.getProp(ConfigurationKeys.GOBBLIN_RUNTIME_DELIVERY_SEMANTICS, AT_LEAST_ONCE.toString()).toUpperCase();
  Optional<DeliverySemantics> semantics = Enums.getIfPresent(DeliverySemantics.class, value);
  Preconditions.checkState(semantics.isPresent(), value + " is not a valid delivery semantics");
  return semantics.get();
 }
}

代码示例来源:origin: google/guava

public void testGetIfPresent_caseSensitive() {
 assertThat(Enums.getIfPresent(TestEnum.class, "cHEETO")).isAbsent();
 assertThat(Enums.getIfPresent(TestEnum.class, "Honda")).isAbsent();
 assertThat(Enums.getIfPresent(TestEnum.class, "poodlE")).isAbsent();
}

代码示例来源:origin: google/guava

public void testGetIfPresent() {
 assertThat(Enums.getIfPresent(TestEnum.class, "CHEETO")).hasValue(TestEnum.CHEETO);
 assertThat(Enums.getIfPresent(TestEnum.class, "HONDA")).hasValue(TestEnum.HONDA);
 assertThat(Enums.getIfPresent(TestEnum.class, "POODLE")).hasValue(TestEnum.POODLE);
 assertThat(Enums.getIfPresent(TestEnum.class, "CHEETO")).isPresent();
 assertThat(Enums.getIfPresent(TestEnum.class, "HONDA")).isPresent();
 assertThat(Enums.getIfPresent(TestEnum.class, "POODLE")).isPresent();
 assertThat(Enums.getIfPresent(TestEnum.class, "CHEETO")).hasValue(TestEnum.CHEETO);
 assertThat(Enums.getIfPresent(TestEnum.class, "HONDA")).hasValue(TestEnum.HONDA);
 assertThat(Enums.getIfPresent(TestEnum.class, "POODLE")).hasValue(TestEnum.POODLE);
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * Refer to {@link MRCompactorAvroKeyDedupJobRunner#getDedupKeyOption()}
 */
private MRCompactorAvroKeyDedupJobRunner.DedupKeyOption getDedupKeyOption() {
 if (!this.state.contains(MRCompactorAvroKeyDedupJobRunner.COMPACTION_JOB_DEDUP_KEY)) {
  return MRCompactorAvroKeyDedupJobRunner.DEFAULT_DEDUP_KEY_OPTION;
 }
 Optional<MRCompactorAvroKeyDedupJobRunner.DedupKeyOption> option = Enums.getIfPresent(MRCompactorAvroKeyDedupJobRunner.DedupKeyOption.class,
     this.state.getProp(MRCompactorAvroKeyDedupJobRunner.COMPACTION_JOB_DEDUP_KEY).toUpperCase());
 return option.isPresent() ? option.get() : MRCompactorAvroKeyDedupJobRunner.DEFAULT_DEDUP_KEY_OPTION;
}

代码示例来源:origin: google/guava

public void testGetIfPresent_whenNoMatchingConstant() {
 assertThat(Enums.getIfPresent(TestEnum.class, "WOMBAT")).isAbsent();
}

代码示例来源:origin: ben-manes/caffeine

/** Implementation variation to use, or all if unset. */
Optional<Implementation> implementation() {
 return Optional.ofNullable(Enums.getIfPresent(Implementation.class,
   System.getProperty("implementation", "")).orNull());
}

代码示例来源:origin: apache/incubator-gobblin

private static DatePartitionType getGranularity(State state, int numBranches, int branchId) {
 String propName = ForkOperatorUtils.getPropertyNameForBranch(WRITER_PARTITION_GRANULARITY, numBranches, branchId);
 String granularityValue = state.getProp(propName, DEFAULT_WRITER_PARTITION_GRANULARITY.toString());
 Optional<DatePartitionType> granularity =
   Enums.getIfPresent(DatePartitionType.class, granularityValue.toUpperCase());
 Preconditions.checkState(granularity.isPresent(),
   granularityValue + " is not a valid writer partition granularity");
 return granularity.get();
}

代码示例来源:origin: ben-manes/caffeine

/** The key reference combination to use, or all if unset. */
Optional<ReferenceType> keys() {
 return Optional.ofNullable(Enums.getIfPresent(ReferenceType.class,
   System.getProperty("keys", "").toUpperCase()).orNull());
}

代码示例来源:origin: ben-manes/caffeine

/** Indicates if statistics should be used, both if unset */
Optional<Stats> stats() {
 return Optional.ofNullable(Enums.getIfPresent(Stats.class,
   System.getProperty("stats", "").toUpperCase()).orNull());
}

代码示例来源:origin: ben-manes/caffeine

/** The value reference combination to use, or all if unset. */
 Optional<ReferenceType> values() {
  return Optional.ofNullable(Enums.getIfPresent(ReferenceType.class,
    System.getProperty("values", "").toUpperCase()).orNull());
 }
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * Get the {@link LauncherTypeEnum} for this {@link JobState}.
 */
public LauncherTypeEnum getLauncherType() {
 return Enums.getIfPresent(LauncherTypeEnum.class,
   this.getProp(ConfigurationKeys.JOB_LAUNCHER_TYPE_KEY, JobLauncherFactory.JobLauncherType.LOCAL.name()))
   .or(LauncherTypeEnum.LOCAL);
}

代码示例来源:origin: ben-manes/caffeine

/** Compute indicates if an async or sync cache variation should be use, or both if unset. */
Optional<Compute> compute() {
 return Optional.ofNullable(Enums.getIfPresent(Compute.class,
   System.getProperty("compute", "").toUpperCase()).orNull());
}

代码示例来源:origin: apache/incubator-gobblin

public static KafkaWorkUnitPacker getInstance(AbstractSource<?, ?> source, SourceState state) {
 if (state.contains(KAFKA_WORKUNIT_PACKER_TYPE)) {
  String packerTypeStr = state.getProp(KAFKA_WORKUNIT_PACKER_TYPE);
  Optional<PackerType> packerType = Enums.getIfPresent(PackerType.class, packerTypeStr);
  if (packerType.isPresent()) {
   return getInstance(packerType.get(), source, state);
  }
  throw new IllegalArgumentException("WorkUnit packer type " + packerTypeStr + " not found");
 }
 return getInstance(DEFAULT_PACKER_TYPE, source, state);
}

代码示例来源:origin: apache/incubator-gobblin

private DedupKeyOption getDedupKeyOption() {
 if (!this.dataset.jobProps().contains(COMPACTION_JOB_DEDUP_KEY)) {
  return DEFAULT_DEDUP_KEY_OPTION;
 }
 Optional<DedupKeyOption> option = Enums.getIfPresent(DedupKeyOption.class,
   this.dataset.jobProps().getProp(COMPACTION_JOB_DEDUP_KEY).toUpperCase());
 return option.isPresent() ? option.get() : DEFAULT_DEDUP_KEY_OPTION;
}

代码示例来源:origin: apache/incubator-gobblin

private KafkaWorkUnitSizeEstimator getWorkUnitSizeEstimator() {
 if (this.state.contains(KAFKA_WORKUNIT_SIZE_ESTIMATOR_TYPE)) {
  String sizeEstimatorTypeString = this.state.getProp(KAFKA_WORKUNIT_SIZE_ESTIMATOR_TYPE);
  Optional<SizeEstimatorType> sizeEstimatorType =
    Enums.getIfPresent(SizeEstimatorType.class, sizeEstimatorTypeString);
  if (sizeEstimatorType.isPresent()) {
   return getWorkUnitSizeEstimator(sizeEstimatorType.get());
  }
  throw new IllegalArgumentException("WorkUnit size estimator type " + sizeEstimatorType + " not found");
 }
 return getWorkUnitSizeEstimator(DEFAULT_SIZE_ESTIMATOR_TYPE);
}

代码示例来源:origin: google/guava

@GwtIncompatible // weak references
private WeakReference<?> doTestClassUnloading() throws Exception {
 URLClassLoader shadowLoader = new URLClassLoader(getClassPathUrls(), null);
 @SuppressWarnings("unchecked")
 Class<TestEnum> shadowTestEnum =
   (Class<TestEnum>) Class.forName(TestEnum.class.getName(), false, shadowLoader);
 assertNotSame(shadowTestEnum, TestEnum.class);
 // We can't write Set<TestEnum> because that is a Set of the TestEnum from the original
 // ClassLoader.
 Set<Object> shadowConstants = new HashSet<>();
 for (TestEnum constant : TestEnum.values()) {
  Optional<TestEnum> result = Enums.getIfPresent(shadowTestEnum, constant.name());
  assertThat(result).isPresent();
  shadowConstants.add(result.get());
 }
 assertEquals(ImmutableSet.<Object>copyOf(shadowTestEnum.getEnumConstants()), shadowConstants);
 Optional<TestEnum> result = Enums.getIfPresent(shadowTestEnum, "blibby");
 assertThat(result).isAbsent();
 return new WeakReference<>(shadowLoader);
}

相关文章