Skip to content

Commit

Permalink
Cleaner validation
Browse files Browse the repository at this point in the history
  • Loading branch information
carterkozak committed Jan 10, 2019
1 parent a0a6d9a commit 1309385
Showing 1 changed file with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,12 @@

import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.util.BitSet;

/**
* Internal utility functions for conjure enum types.
*/
public final class ConjureEnums {

private static final BitSet allowedCharacters = new BitSet();

static {
allowedCharacters.set('A', 'Z' + 1);
allowedCharacters.set('_');
}

private ConjureEnums() {
// cannot instantiate
}
Expand All @@ -41,12 +33,16 @@ public static void validate(String value) {
throw new SafeIllegalArgumentException("Enum values must not be empty");
}

final int length = value.length();
int length = value.length();
for (int index = 0; index < length; index++) {
if (!allowedCharacters.get(value.charAt(index))) {
if (!isAllowedCharacter(value.charAt(index))) {
throw new SafeIllegalArgumentException("Enum values must use SCREAMING_SNAKE_CASE",
UnsafeArg.of("value", value));
}
}
}

private static boolean isAllowedCharacter(char character) {
return (character >= 'A' && character <= 'Z') || character == '_';
}
}

0 comments on commit 1309385

Please sign in to comment.