From 4fdc39f06c9620c7b2652ceb2bef25b0d92bfc2a Mon Sep 17 00:00:00 2001 From: Imran Rashid Date: Tue, 17 Feb 2015 13:25:05 -0600 Subject: [PATCH] refactor case insensitive enum parsing --- .../spark/status/api/ApplicationStatus.java | 14 +------ .../org/apache/spark/status/api/EnumUtil.java | 38 +++++++++++++++++++ .../apache/spark/status/api/StageStatus.java | 18 +-------- 3 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 core/src/main/java/org/apache/spark/status/api/EnumUtil.java diff --git a/core/src/main/java/org/apache/spark/status/api/ApplicationStatus.java b/core/src/main/java/org/apache/spark/status/api/ApplicationStatus.java index 60830f1886813..8d9874b94fa4d 100644 --- a/core/src/main/java/org/apache/spark/status/api/ApplicationStatus.java +++ b/core/src/main/java/org/apache/spark/status/api/ApplicationStatus.java @@ -25,20 +25,8 @@ public enum ApplicationStatus { COMPLETED, RUNNING; - private static String VALID_VALUES = Joiner.on(", ").join( - Arrays.asList(values())); - public static ApplicationStatus fromString(String str) { - if (str == null) { - return null; - } - for (ApplicationStatus status: values()) { - if (status.name().equalsIgnoreCase(str)) - return status; - } - throw new IllegalArgumentException( - String.format("Illegal type='%s'. Supported type values: %s", - str, VALID_VALUES)); + return EnumUtil.parseIgnoreCase(ApplicationStatus.class, str); } } diff --git a/core/src/main/java/org/apache/spark/status/api/EnumUtil.java b/core/src/main/java/org/apache/spark/status/api/EnumUtil.java new file mode 100644 index 0000000000000..55c0d7d710bfe --- /dev/null +++ b/core/src/main/java/org/apache/spark/status/api/EnumUtil.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.spark.status.api; + +import com.google.common.base.Joiner; + +import java.util.Arrays; + +public class EnumUtil { + public static > E parseIgnoreCase(Class clz, String str) { + E[] constants = clz.getEnumConstants(); + if (str == null) { + return null; + } + for (E e: constants) { + if (e.name().equalsIgnoreCase(str)) + return e; + } + throw new IllegalArgumentException( + String.format("Illegal type='%s'. Supported type values: %s", + str, Joiner.on(", ").join( + Arrays.asList(constants)))); + } +} diff --git a/core/src/main/java/org/apache/spark/status/api/StageStatus.java b/core/src/main/java/org/apache/spark/status/api/StageStatus.java index cc83a375cd975..e752e6241963e 100644 --- a/core/src/main/java/org/apache/spark/status/api/StageStatus.java +++ b/core/src/main/java/org/apache/spark/status/api/StageStatus.java @@ -17,29 +17,13 @@ package org.apache.spark.status.api; -import com.google.common.base.Joiner; - -import java.util.Arrays; - public enum StageStatus { Active, Complete, Failed, Pending; - private static String VALID_VALUES = Joiner.on(", ").join( - Arrays.asList(values())); - public static StageStatus fromString(String str) { - if (str == null) { - return null; - } - for (StageStatus status: values()) { - if (status.name().equalsIgnoreCase(str)) - return status; - } - throw new IllegalArgumentException( - String.format("Illegal type='%s'. Supported type values: %s", - str, VALID_VALUES)); + return EnumUtil.parseIgnoreCase(StageStatus.class, str); } }