本文整理了Java中com.google.common.base.Enums
类的一些代码示例,展示了Enums
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Enums
类的具体详情如下:
包路径:com.google.common.base.Enums
类名称:Enums
[英]Utility methods for working with Enum instances.
[中]用于处理枚举实例的实用程序方法。
代码示例来源: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/guava
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get()));
}
代码示例来源:origin: google/guava
@GwtIncompatible // java.lang.ref.WeakReference
static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnumConstants(
Class<T> enumClass) {
synchronized (enumConstantCache) {
Map<String, WeakReference<? extends Enum<?>>> constants = enumConstantCache.get(enumClass);
if (constants == null) {
constants = populateCache(enumClass);
}
return constants;
}
}
代码示例来源:origin: google/guava
public void testStringConverter_serialization() {
SerializableTester.reserializeAndAssert(Enums.stringConverter(TestEnum.class));
}
代码示例来源:origin: google/guava
@GwtIncompatible // Class.getName()
public void testStringConverter_toString() {
assertEquals(
"Enums.stringConverter(com.google.common.base.EnumsTest$TestEnum.class)",
Enums.stringConverter(TestEnum.class).toString());
}
代码示例来源: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: google/j2objc
@GwtIncompatible // java.lang.ref.WeakReference
static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnumConstants(
Class<T> enumClass) {
synchronized (enumConstantCache) {
Map<String, WeakReference<? extends Enum<?>>> constants = enumConstantCache.get(enumClass);
if (constants == null) {
constants = populateCache(enumClass);
}
return constants;
}
}
代码示例来源:origin: google/j2objc
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get()));
}
代码示例来源:origin: google/guava
public void testStringConverter_convertError() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
try {
converter.convert("xxx");
fail();
} catch (IllegalArgumentException expected) {
}
}
代码示例来源: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: wildfly/wildfly
@GwtIncompatible // java.lang.ref.WeakReference
static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnumConstants(
Class<T> enumClass) {
synchronized (enumConstantCache) {
Map<String, WeakReference<? extends Enum<?>>> constants = enumConstantCache.get(enumClass);
if (constants == null) {
constants = populateCache(enumClass);
}
return constants;
}
}
代码示例来源:origin: wildfly/wildfly
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get()));
}
代码示例来源:origin: google/guava
@GwtIncompatible // NullPointerTester
public void testStringConverter_nullPointerTester() throws Exception {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
NullPointerTester tester = new NullPointerTester();
tester.testAllPublicInstanceMethods(converter);
}
代码示例来源: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: com.google.guava/guava-jdk5
@GwtIncompatible("java.lang.ref.WeakReference")
static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnumConstants(
Class<T> enumClass) {
synchronized (enumConstantCache) {
Map<String, WeakReference<? extends Enum<?>>> constants =
enumConstantCache.get(enumClass);
if (constants == null) {
constants = populateCache(enumClass);
}
return constants;
}
}
代码示例来源:origin: com.diffplug.guava/guava-core
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
return ref == null
? Optional.<T> absent()
: Optional.of(enumClass.cast(ref.get()));
}
}
代码示例来源:origin: google/guava
public void testStringConverter_nullConversions() {
Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
assertNull(converter.convert(null));
assertNull(converter.reverse().convert(null));
}
代码示例来源: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: com.diffplug.guava/guava-core
@GwtIncompatible("java.lang.ref.WeakReference")
static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnumConstants(
Class<T> enumClass) {
synchronized (enumConstantCache) {
Map<String, WeakReference<? extends Enum<?>>> constants = enumConstantCache.get(enumClass);
if (constants == null) {
constants = populateCache(enumClass);
}
return constants;
}
}
代码示例来源:origin: com.google.guava/guava-jdk5
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
return ref == null
? Optional.<T>absent()
: Optional.of(enumClass.cast(ref.get()));
}
}
内容来源于网络,如有侵权,请联系作者删除!