Chorus-bdd/Chorus · ConfigPropertyUtils.java
java logo
public static <T extends Enum<T>> Pattern createValidationPatternFromEnumType(Class<T> enumType) {

        String regEx = Stream.of(enumType.getEnumConstants())
                .map(Enum::name)
                .collect(Collectors.joining("|", "(?i)", ""));

        //Enum constants may contain $ which needs to be escaped
        regEx = regEx.replace("$", "\\$");
        return Pattern.compile(regEx);
    }
Similar code snippets
1.
cucumber/cucumber-expressions-java · ParameterType.java
Match rating: 59.8% · See similar code snippets
java logo
public static <E extends Enum> ParameterType<E> fromEnum(final Class<E> enumClass) {
        Enum[] enumConstants = enumClass.getEnumConstants();
        StringBuilder regexpBuilder = new StringBuilder();
        for (int i = 0; i < enumConstants.length; i++) {
            if (i > 0) regexpBuilder.append("|");
            regexpBuilder.append(enumConstants[i].name());
        }
        return new ParameterType<>(
                enumClass.getSimpleName(),
                regexpBuilder.toString(),
                enumClass,
                new Transformer<E>() {
                    @Override
                    public E transform(String arg) {
                        return (E) Enum.valueOf(enumClass, arg);
                    }
                }
        );
    }
2.
line/armeria · EnumInfo.java
Match rating: 54.23% · See similar code snippets
java logo
private static Iterable<EnumValueInfo> toEnumValues(Class<? extends Enum<?>> enumType) {
        final Class<?> rawEnumType = requireNonNull(enumType, "enumType");
        @SuppressWarnings({ "unchecked", "rawtypes" })
        final Set<Enum> values = EnumSet.allOf((Class<Enum>) rawEnumType);
        return values.stream().map(e -> new EnumValueInfo(e.name()))::iterator;
    }
3.
lightbend/config · SimpleConfig.java
Match rating: 53.67% · See similar code snippets
java logo
private <T extends Enum<T>> T getEnumValue(String path, Class<T> enumClass, ConfigValue enumConfigValue) {
        String enumName = (String) enumConfigValue.unwrapped();
        try {
            return Enum.valueOf(enumClass, enumName);
        } catch (IllegalArgumentException e) {
            List<String> enumNames = new ArrayList<String>();
            Enum[] enumConstants = enumClass.getEnumConstants();
            if (enumConstants != null) {
                for (Enum enumConstant : enumConstants) {
                    enumNames.add(enumConstant.name());
                }
            }
            throw new ConfigException.BadValue(
              enumConfigValue.origin(), path,
              String.format("The enum class %s has no constant of the name '%s' (should be one of %s.)",
                enumClass.getSimpleName(), enumName, enumNames));
        }
    }
4.
cdapio/tigon · Schema.java
Match rating: 53.33% · See similar code snippets
java logo
public static Schema enumWith(Class<Enum<?>> enumClass) {
    Enum<?>[] enumConstants = enumClass.getEnumConstants();
    String[] names = new String[enumConstants.length];
    for (int i = 0; i < enumConstants.length; i++) {
      names[i] = enumConstants[i].name();
    }
    return enumWith(names);
  }
5.
jqno/equalsverifier · FallbackFactory.java
Match rating: 53.18% · See similar code snippets
java logo
private Tuple<T> giveEnumInstances(TypeTag tag) {
        Class<T> type = tag.getType();
        T[] enumConstants = type.getEnumConstants();

        switch (enumConstants.length) {
            case 0:
                return new Tuple<>(null, null, null);
            case 1:
                return new Tuple<>(enumConstants[0], enumConstants[0], enumConstants[0]);
            default:
                return new Tuple<>(enumConstants[0], enumConstants[1], enumConstants[0]);
        }
    }
6.
vojtechhabarta/typescript-generator · ModelParser.java
Match rating: 53.03% · See similar code snippets
java logo
protected static DeclarationModel parseEnum(SourceType<Class<?>> sourceClass) {
        final List<EnumMemberModel> values = new ArrayList<>();
        if (sourceClass.type.isEnum()) {
            @SuppressWarnings("unchecked")
            final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) sourceClass.type;
            for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
                values.add(new EnumMemberModel(enumConstant.name(), enumConstant.name(), null));
            }
        }
        return new EnumModel(sourceClass.type, EnumKind.StringBased, values, null);
    }
7.
osglworks/java-tool · Lang.java
Match rating: 53.01% · See similar code snippets
java logo
public static <ENUM extends Enum<ENUM>> TypeConverter<String, ENUM> stringToEnum(final Class<ENUM> enumClass) {
            return new TypeConverter<String, ENUM>(String.class, enumClass) {
                @Override
                public ENUM convert(String s) {
                    return convert(s, null);
                }

                @Override
                public ENUM convert(String s, Object hint) {
                    if (HINT_STRICT == hint) {
                        return Enum.valueOf(enumClass, s);
                    }
                    Enum[] enums = enumClass.getEnumConstants();
                    for (Enum e : enums) {
                        if (Keyword.of(e.name()).equals(Keyword.of(s))) {
                            return (ENUM) e;
                        }
                    }
                    throw new IllegalArgumentException("No enum constant " + enumClass.getCanonicalName() + "." + s);
                }
            };
        }
8.
mapfish/mapfish-print · MapfishParser.java
Match rating: 52.81% · See similar code snippets
java logo
private static Object parseEnum(final Class<?> type, final String path, final String enumString) {
        // not the name, maybe the ordinal
        try {
            int ordinal = Integer.parseInt(enumString);
            final Object[] enumConstants = type.getEnumConstants();
            if (ordinal < enumConstants.length) {
                return enumConstants[ordinal];
            } else {
                throw enumError(enumConstants, path, enumString);
            }
        } catch (NumberFormatException ne) {
            final Object[] enumConstants = type.getEnumConstants();

            for (Object enumConstant: enumConstants) {
                if (enumConstant.toString().equalsIgnoreCase(enumString) ||
                        ((Enum) enumConstant).name().equalsIgnoreCase(enumString)) {
                    return enumConstant;
                }
            }
            throw enumError(type.getEnumConstants(), path, enumString);
        }
    }
9.
satoshi-kimura/samurai-dao · Row.java
Match rating: 52.66% · See similar code snippets
java logo
public <ENUM extends Enum<ENUM>> ENUM getEnum(String key, Class<ENUM> enumClass) {
		return typeConverter.getEnum(key, enumClass);
	}
10.
lesaint/damapping · DASourceClassImpl.java
Match rating: 52.66% · See similar code snippets
java logo
public static EnumBuilder enumBuilder(@Nonnull DAType type, @Nullable List<DAEnumValue> enumValues) {
    return new EnumBuilder(type, enumValues);
  }