下面列出了怎么用com.google.common.base.Enums的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Try and parse `value` as Enum. Throws `QueryException` if invalid value.
*
* @param klass the Enum class.
* @param value the enum value.
*/
@SuppressWarnings( { "unchecked", "rawtypes" } )
public static <T> T getEnumValue( Class<T> klass, String value )
{
Optional<? extends Enum<?>> enumValue = Enums.getIfPresent( (Class<? extends Enum>) klass, value );
if ( enumValue.isPresent() )
{
return (T) enumValue.get();
}
else
{
Object[] possibleValues = klass.getEnumConstants();
throw new QueryParserException( "Unable to parse `" + value + "` as `" + klass + "`, available values are: " + Arrays.toString( possibleValues ) );
}
}
private <T extends Enum<T>> T getEnumWithDefault( Class<T> enumKlass, Map<String, List<String>> parameters, String key, T defaultValue )
{
if ( parameters == null || parameters.get( key ) == null || parameters.get( key ).isEmpty() )
{
return defaultValue;
}
if ( TrackerIdScheme.class.equals( enumKlass ) && IdScheme.isAttribute( parameters.get( key ).get( 0 ) ) )
{
return Enums.getIfPresent( enumKlass, "ATTRIBUTE" ).orNull();
}
String value = String.valueOf( parameters.get( key ).get( 0 ) );
return Enums.getIfPresent( enumKlass, value ).or( defaultValue );
}
/**
* Get an instance of {@link HiveSerDeManager}.
*
* @param props A {@link State} object. To get a specific implementation of {@link HiveSerDeManager}, specify either
* one of the values in {@link Implementation} (e.g., AVRO or ORC) or the name of a class that implements
* {@link HiveSerDeManager} in property {@link #HIVE_ROW_FORMAT}. The {@link State} object is also used to
* instantiate the {@link HiveSerDeManager}.
*/
public static HiveSerDeManager get(State props) {
String type = props.getProp(HIVE_ROW_FORMAT, Implementation.AVRO.name());
Optional<Implementation> implementation = Enums.getIfPresent(Implementation.class, type.toUpperCase());
try {
if (implementation.isPresent()) {
return (HiveSerDeManager) ConstructorUtils.invokeConstructor(Class.forName(implementation.get().toString()),
props);
}
return (HiveSerDeManager) ConstructorUtils.invokeConstructor(Class.forName(type), props);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(
"Unable to instantiate " + HiveSerDeManager.class.getSimpleName() + " with type " + type, e);
}
}
private void validate(String encryptionLevel, String encryptedFields) throws IOException {
if (!Enums.getIfPresent(EncryptionLevel.class, encryptionLevel.toUpperCase()).isPresent()) {
throw new IOException("Invalid encryption level " + encryptionLevel);
}
switch (EncryptionLevel.valueOf(encryptionLevel.toUpperCase())) {
case FIELD:
if ((encryptedFields.equalsIgnoreCase(DatasetDescriptorConfigKeys.DATASET_DESCRIPTOR_CONFIG_ANY)) ||
(encryptedFields.equalsIgnoreCase(DatasetDescriptorConfigKeys.DATASET_DESCRIPTOR_CONFIG_NONE))) {
log.error("Invalid input for encryptedFields {}", encryptedFields);
throw new IOException("Invalid encryptedFields");
}
break;
case NONE:
if (!encryptedFields.equalsIgnoreCase(DatasetDescriptorConfigKeys.DATASET_DESCRIPTOR_CONFIG_NONE)) {
log.error("Invalid input for encryptedFields {}", encryptedFields);
throw new IOException("Invalid encryptedFields");
}
break;
default:
break;
}
return;
}
/**
* Creates a new instance for a JobLauncher with a given type
* @param sysProps the system/environment properties
* @param jobProps the job properties
* @param launcherTypeValue the type of the launcher; either a {@link JobLauncherType} value or
* the name of the class that extends {@link AbstractJobLauncher} and has a constructor
* that has a single Properties parameter..
* @param metadataTags additional metadata to be added to timing events
* @return the JobLauncher instance
* @throws RuntimeException if the instantiation fails
*/
public static JobLauncher newJobLauncher(Properties sysProps, Properties jobProps,
String launcherTypeValue, SharedResourcesBroker<GobblinScopeTypes> instanceBroker, List<? extends Tag<?>> metadataTags) {
Optional<JobLauncherType> launcherType = Enums.getIfPresent(JobLauncherType.class, launcherTypeValue);
try {
if (launcherType.isPresent()) {
switch (launcherType.get()) {
case LOCAL:
return new LocalJobLauncher(JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps), instanceBroker, metadataTags);
case MAPREDUCE:
return new MRJobLauncher(JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps), instanceBroker, metadataTags);
default:
throw new RuntimeException("Unsupported job launcher type: " + launcherType.get().name());
}
}
@SuppressWarnings("unchecked")
Class<? extends AbstractJobLauncher> launcherClass =
(Class<? extends AbstractJobLauncher>) Class.forName(launcherTypeValue);
return launcherClass.getDeclaredConstructor(Properties.class)
.newInstance(JobConfigurationUtils.combineSysAndJobProperties(sysProps, jobProps));
} catch (Exception e) {
throw new RuntimeException("Failed to create job launcher: " + e, e);
}
}
private <T extends Enum<T>> Set<String> getRecommendationByBlacklist(Class<T> enumClass, boolean emptyServiceListBlacklisted) {
Map<String, Set<String>> componentsByHostGroup = getNonGatewayComponentsByHostGroup();
Set<String> recos = new HashSet<>();
for (Entry<String, Set<String>> hostGroupServices : componentsByHostGroup.entrySet()) {
if (emptyServiceListBlacklisted && hostGroupServices.getValue().isEmpty()) {
continue;
}
boolean foundBlackListItem = false;
for (String service : hostGroupServices.getValue()) {
if (Enums.getIfPresent(enumClass, service).isPresent()) {
foundBlackListItem = true;
break;
}
}
if (!foundBlackListItem) {
recos.add(hostGroupServices.getKey());
}
}
return recos;
}
@Test
public void transform_string_to_enum () {
List<String> days = Lists.newArrayList(
"WEDNESDAY",
"SUNDAY",
"MONDAY",
"TUESDAY",
"WEDNESDAY");
Function<String, Day> valueOfFunction = Enums.valueOfFunction(Day.class);
Iterable<Day> daysAsEnums = Iterables.transform(days, valueOfFunction);
assertThat(daysAsEnums, IsIterableWithSize.<Day>iterableWithSize(5));
assertThat(daysAsEnums, IsIterableContainingInOrder.
<Day>contains(
Day.WEDNESDAY,
Day.SUNDAY,
Day.MONDAY,
Day.TUESDAY,
Day.WEDNESDAY));
}
@Test
public void transform_string_to_enum_string_converter () {
List<String> days = Lists.newArrayList(
"WEDNESDAY",
"SUNDAY",
"MONDAY",
"TUESDAY",
"WEDNESDAY");
Function<String, Day> valueOfFunction = Enums.stringConverter(Day.class);
Iterable<Day> daysAsEnums = Iterables.transform(days, valueOfFunction);
assertThat(daysAsEnums, IsIterableWithSize.<Day>iterableWithSize(5));
assertThat(daysAsEnums, IsIterableContainingInOrder.
<Day>contains(
Day.WEDNESDAY,
Day.SUNDAY,
Day.MONDAY,
Day.TUESDAY,
Day.WEDNESDAY));
}
public static Entity spawn(Location location, ConfigurationSection config) {
String typeStr = config.getString("type");
if (typeStr == null) return null;
EntityType type = Enums.getIfPresent(EntityType.class, typeStr.toUpperCase(Locale.ENGLISH)).or(EntityType.ZOMBIE);
return edit(location.getWorld().spawnEntity(location, type), config);
}
/**
* Attempt to resolve an enum of the specified type by looking for a label with the
* given value, trying all case variations in the process.
*
* @return the matching enum label (if any)
* @throws IllegalArgumentException if no such label matching the given value is found.
*/
@Override
public Enum convert(String value) {
Set<String> candidates = Sets.newHashSet(value, value.toUpperCase(), value.toLowerCase());
for (String candidate : candidates) {
Optional<? extends Enum> result = Enums.getIfPresent(enumType, candidate);
if (result.isPresent())
return result.get();
}
throw new IllegalArgumentException(String.format(
"No enum constant %s.[%s]", enumType.getName(), collectionToDelimitedString(candidates, "|")));
}
/**
* If the substation has a more specific geographical information than just its country,
* returns the corresponding geographical code, otherwise null.
*/
private static EntsoeGeographicalCode getRegionalGeographicalCode(Substation substation) {
//Currently only DE has subregions
if (substation.getCountry().map(country -> country != Country.DE).orElse(true)) {
return null;
}
EntsoeGeographicalCode res = Enums.getIfPresent(EntsoeGeographicalCode.class, substation.getNameOrId().substring(0, 2)).orNull();
//handle case where a D-node would start with DE ...
return res == EntsoeGeographicalCode.DE ? null : res;
}
private static void fillWithDefault ( final List<ColumnLabelProviderInformation> columnInformation )
{
columnInformation.add ( new ColumnLabelProviderInformation ( "sourceTimestamp", "sourceTimestamp", true, DEFAULT_INITIAL_SIZE, Collections.<String, String> emptyMap () ) );
for ( final Fields field : Fields.values () )
{
if ( Enums.getField ( field ).getAnnotation ( Deprecated.class ) != null )
{
// ignore deprecated fields
continue;
}
final Map<String, String> properties = new HashMap<String, String> ();
properties.put ( "key", field.getName () );
switch ( field )
{
case ACTOR_NAME:
properties.put ( "decoration", Decoration.ACTOR.toString () );
break;
case EVENT_TYPE:
properties.put ( "decoration", Decoration.MONITOR.toString () );
break;
default:
break;
}
columnInformation.add ( new ColumnLabelProviderInformation ( field.getName (), "variant", false, DEFAULT_INITIAL_SIZE, properties ) );
}
columnInformation.add ( new ColumnLabelProviderInformation ( "entryTimestamp", "entryTimestamp", true, DEFAULT_INITIAL_SIZE, Collections.<String, String> emptyMap () ) );
}
private InclusionStrategy.Include getInclusionStrategy( String inclusionStrategy )
{
if ( inclusionStrategy != null )
{
Optional<InclusionStrategy.Include> optional = Enums.getIfPresent( InclusionStrategy.Include.class, inclusionStrategy );
if ( optional.isPresent() )
{
return optional.get();
}
}
return InclusionStrategy.Include.NON_NULL;
}
private InclusionStrategy.Include getInclusionStrategy( String inclusionStrategy )
{
if ( inclusionStrategy != null )
{
Optional<InclusionStrategy.Include> optional = Enums.getIfPresent( InclusionStrategy.Include.class, inclusionStrategy );
if ( optional.isPresent() )
{
return optional.get();
}
}
return InclusionStrategy.Include.NON_NULL;
}
private InclusionStrategy.Include getInclusionStrategy( String inclusionStrategy )
{
if ( inclusionStrategy != null )
{
Optional<InclusionStrategy.Include> optional = Enums.getIfPresent( InclusionStrategy.Include.class, inclusionStrategy );
if ( optional.isPresent() )
{
return optional.get();
}
}
return InclusionStrategy.Include.NON_NULL;
}
private <T extends Enum<T>> T getEnumWithDefault( Class<T> enumKlass, Map<String, List<String>> parameters, String key, T defaultValue )
{
if ( parameters == null || parameters.get( key ) == null || parameters.get( key ).isEmpty() )
{
return defaultValue;
}
String value = String.valueOf( parameters.get( key ).get( 0 ) );
return Enums.getIfPresent( enumKlass, value ).or( defaultValue );
}
private <T extends Enum<T>> T getEnumWithDefault( Class<T> enumKlass, Map<String, List<String>> parameters, String key, T defaultValue )
{
if ( parameters == null || parameters.get( key ) == null || parameters.get( key ).isEmpty() )
{
return defaultValue;
}
String value = String.valueOf( parameters.get( key ).get( 0 ) );
return Enums.getIfPresent( enumKlass, value ).or( defaultValue );
}
static CypherType parse(@NotNull String type) {
if (type.startsWith("LIST")) {
if (!type.contains("OF")) {
return CypherList.of(ANY);
}
String param = type.split("OF")[1]
.replaceAll("\\?", "")
.trim();
return CypherList.of(Enums.getIfPresent(CypherSimpleType.class, param).or(ANY));
}
return Enums.getIfPresent(CypherSimpleType.class, type.replaceAll("\\?", "").trim())
.or(ANY);
}
private ConsumerBuilder<byte[]> getConsumerConfiguration(PulsarClient client) {
ConsumerBuilder<byte[]> builder = client.newConsumer();
if (queryParams.containsKey("ackTimeoutMillis")) {
builder.ackTimeout(Integer.parseInt(queryParams.get("ackTimeoutMillis")), TimeUnit.MILLISECONDS);
}
if (queryParams.containsKey("subscriptionType")) {
checkArgument(Enums.getIfPresent(SubscriptionType.class, queryParams.get("subscriptionType")).isPresent(),
"Invalid subscriptionType %s", queryParams.get("subscriptionType"));
builder.subscriptionType(SubscriptionType.valueOf(queryParams.get("subscriptionType")));
}
if (queryParams.containsKey("receiverQueueSize")) {
builder.receiverQueueSize(Math.min(Integer.parseInt(queryParams.get("receiverQueueSize")), 1000));
}
if (queryParams.containsKey("consumerName")) {
builder.consumerName(queryParams.get("consumerName"));
}
if (queryParams.containsKey("priorityLevel")) {
builder.priorityLevel(Integer.parseInt(queryParams.get("priorityLevel")));
}
if (queryParams.containsKey("maxRedeliverCount") || queryParams.containsKey("deadLetterTopic")) {
DeadLetterPolicy.DeadLetterPolicyBuilder dlpBuilder = DeadLetterPolicy.builder();
if (queryParams.containsKey("maxRedeliverCount")) {
dlpBuilder.maxRedeliverCount(Integer.parseInt(queryParams.get("maxRedeliverCount")))
.deadLetterTopic(String.format("%s-%s-DLQ", topic, subscription));
}
if (queryParams.containsKey("deadLetterTopic")) {
dlpBuilder.deadLetterTopic(queryParams.get("deadLetterTopic"));
}
builder.deadLetterPolicy(dlpBuilder.build());
}
return builder;
}
/**
* Attempt to resolve an enum of the specified type by looking for a label with the
* given value, trying all case variations in the process.
*
* @return the matching enum label (if any)
* @throws ConfigException if no such label matching the given value is found.
*/
@Override
public Enum convert(String value) {
Set<String> candidates = Sets.newHashSet(value, value.toUpperCase(), value.toLowerCase());
for (String candidate : candidates) {
Optional<? extends Enum> result = Enums.getIfPresent(enumType, candidate);
if (result.isPresent())
return result.get();
}
throw new ConfigException("Enum label %s.{%s} does not exist",
enumType.getSimpleName(), String.join("|", candidates));
}
/**
* Get a Java enum from a Protobuf enum in a safe way.
*
* @param enumType the class of the enum, e.g: BlaEnum.class
* @param name the name of the enum entry, e.g: proto.getWinner().name()
* @param <E> the enum Type
* @return an enum
*/
@Nullable
public static <E extends Enum<E>> E enumFromProto(Class<E> enumType, String name) {
E result = Enums.getIfPresent(enumType, name).orNull();
if (result == null) {
log.error("Invalid value for enum " + enumType.getSimpleName() + ": " + name);
result = Enums.getIfPresent(enumType, "UNDEFINED").orNull();
log.error("We try to lookup for an enum entry with name 'UNDEFINED' and use that if available, " +
"otherwise the enum is null. enum={}", result);
return result;
}
return result;
}
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;
}
/**
* Refer to 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;
}
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);
}
public static KafkaWorkUnitPacker getInstance(AbstractSource<?, ?> source, SourceState state,
Optional<MetricContext> metricContext) {
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, metricContext);
}
throw new IllegalArgumentException("WorkUnit packer type " + packerTypeStr + " not found");
}
return getInstance(DEFAULT_PACKER_TYPE, source, state, metricContext);
}
/**
* Constructs a {@link KafkaSchemaRegistry} using the value of {@link #KAFKA_DESERIALIZER_TYPE}, if not set it
* defaults to {@link SimpleKafkaSchemaRegistry}.
*/
private static KafkaSchemaRegistry<?, ?> getKafkaSchemaRegistry(Properties props)
throws ReflectiveOperationException {
Optional<Deserializers> deserializerType =
Enums.getIfPresent(Deserializers.class, props.getProperty(KAFKA_DESERIALIZER_TYPE).toUpperCase());
if (deserializerType.isPresent()) {
return ConstructorUtils.invokeConstructor(deserializerType.get().getSchemaRegistryClass(), props);
}
if (props.containsKey(KafkaSchemaRegistry.KAFKA_SCHEMA_REGISTRY_CLASS)) {
return KafkaSchemaRegistry.get(props);
}
return new SimpleKafkaSchemaRegistry(props);
}
/**
* Get an instance of {@link HiveSerDeWrapper}.
*
* @param serDeType The SerDe type. If serDeType is one of the available {@link HiveSerDeWrapper.BuiltInHiveSerDe},
* the other three parameters are not used. Otherwise, serDeType should be the class name of a {@link SerDe},
* and the other three parameters must be present.
*/
public static HiveSerDeWrapper get(String serDeType, Optional<String> inputFormatClassName,
Optional<String> outputFormatClassName) {
Optional<BuiltInHiveSerDe> hiveSerDe = Enums.getIfPresent(BuiltInHiveSerDe.class, serDeType.toUpperCase());
if (hiveSerDe.isPresent()) {
return new HiveSerDeWrapper(hiveSerDe.get());
}
Preconditions.checkArgument(inputFormatClassName.isPresent(),
"Missing input format class name for SerDe " + serDeType);
Preconditions.checkArgument(outputFormatClassName.isPresent(),
"Missing output format class name for SerDe " + serDeType);
return new HiveSerDeWrapper(serDeType, inputFormatClassName.get(), outputFormatClassName.get());
}
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();
}
private void initDatePartitionFromGranularity(State state) {
String granularityProp = state.getProp(PartitionedFileSourceBase.DATE_PARTITIONED_SOURCE_PARTITION_GRANULARITY);
DatePartitionType partitionType = null;
if (granularityProp == null) {
partitionType = PartitionedFileSourceBase.DEFAULT_DATE_PARTITIONED_SOURCE_PARTITION_GRANULARITY;
} else {
Optional<DatePartitionType> partitionTypeOpt =
Enums.getIfPresent(DatePartitionType.class, granularityProp.toUpperCase());
Preconditions
.checkState(partitionTypeOpt.isPresent(), "Invalid source partition granularity: " + granularityProp);
partitionType = partitionTypeOpt.get();
}
this.partitionPatternFormatter = DateTimeFormat.forPattern(partitionType.getDateTimePattern());
this.incrementalUnit = partitionType.getDateTimeFieldType().getDurationType();
}
/**
* Get the output format of the writer of type {@link WriterOutputFormat}.
*
* @param branches number of forked branches
* @param index branch index
* @return output format of the writer
*/
public WriterOutputFormat getWriterOutputFormat(int branches, int index) {
String writerOutputFormatValue = this.taskState.getProp(
ForkOperatorUtils.getPropertyNameForBranch(ConfigurationKeys.WRITER_OUTPUT_FORMAT_KEY, branches, index),
WriterOutputFormat.OTHER.name());
log.debug("Found writer output format value = {}", writerOutputFormatValue);
WriterOutputFormat wof = Enums.getIfPresent(WriterOutputFormat.class, writerOutputFormatValue.toUpperCase())
.or(WriterOutputFormat.OTHER);
log.debug("Returning writer output format = {}", wof);
return wof;
}