Skip to content

Commit 4fdc39f

Browse files
committed
refactor case insensitive enum parsing
1 parent cba1ef6 commit 4fdc39f

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

core/src/main/java/org/apache/spark/status/api/ApplicationStatus.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,8 @@ public enum ApplicationStatus {
2525
COMPLETED,
2626
RUNNING;
2727

28-
private static String VALID_VALUES = Joiner.on(", ").join(
29-
Arrays.asList(values()));
30-
3128
public static ApplicationStatus fromString(String str) {
32-
if (str == null) {
33-
return null;
34-
}
35-
for (ApplicationStatus status: values()) {
36-
if (status.name().equalsIgnoreCase(str))
37-
return status;
38-
}
39-
throw new IllegalArgumentException(
40-
String.format("Illegal type='%s'. Supported type values: %s",
41-
str, VALID_VALUES));
29+
return EnumUtil.parseIgnoreCase(ApplicationStatus.class, str);
4230
}
4331

4432
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.spark.status.api;
18+
19+
import com.google.common.base.Joiner;
20+
21+
import java.util.Arrays;
22+
23+
public class EnumUtil {
24+
public static <E extends Enum<E>> E parseIgnoreCase(Class<E> clz, String str) {
25+
E[] constants = clz.getEnumConstants();
26+
if (str == null) {
27+
return null;
28+
}
29+
for (E e: constants) {
30+
if (e.name().equalsIgnoreCase(str))
31+
return e;
32+
}
33+
throw new IllegalArgumentException(
34+
String.format("Illegal type='%s'. Supported type values: %s",
35+
str, Joiner.on(", ").join(
36+
Arrays.asList(constants))));
37+
}
38+
}

core/src/main/java/org/apache/spark/status/api/StageStatus.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,13 @@
1717

1818
package org.apache.spark.status.api;
1919

20-
import com.google.common.base.Joiner;
21-
22-
import java.util.Arrays;
23-
2420
public enum StageStatus {
2521
Active,
2622
Complete,
2723
Failed,
2824
Pending;
2925

30-
private static String VALID_VALUES = Joiner.on(", ").join(
31-
Arrays.asList(values()));
32-
3326
public static StageStatus fromString(String str) {
34-
if (str == null) {
35-
return null;
36-
}
37-
for (StageStatus status: values()) {
38-
if (status.name().equalsIgnoreCase(str))
39-
return status;
40-
}
41-
throw new IllegalArgumentException(
42-
String.format("Illegal type='%s'. Supported type values: %s",
43-
str, VALID_VALUES));
27+
return EnumUtil.parseIgnoreCase(StageStatus.class, str);
4428
}
4529
}

0 commit comments

Comments
 (0)