Skip to content

Commit

Permalink
refactor case insensitive enum parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
squito committed Feb 17, 2015
1 parent cba1ef6 commit 4fdc39f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
38 changes: 38 additions & 0 deletions core/src/main/java/org/apache/spark/status/api/EnumUtil.java
Original file line number Diff line number Diff line change
@@ -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 extends Enum<E>> E parseIgnoreCase(Class<E> 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))));
}
}
18 changes: 1 addition & 17 deletions core/src/main/java/org/apache/spark/status/api/StageStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4fdc39f

Please sign in to comment.