protected StopWhichAppsOnShutdown computeStopWhichAppsOnShutdown() { boolean isDefault = STOP_THESE_IF_NOT_PERSISTED.equals(stopWhichAppsOnShutdown); if (exitAndLeaveAppsRunningAfterStarting && isDefault) { return StopWhichAppsOnShutdown.NONE; } else { return Enums.valueOfIgnoreCase(StopWhichAppsOnShutdown.class, stopWhichAppsOnShutdown).get(); } }
@Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp = ".*leftover enums.*\\[aliceTheCamel\\].*leftover values.*alice_thecamel.*") public static void testAllValuesEnumeratedNoMatchBadCamel() { Enums.checkAllEnumeratedIgnoreCase(SomeENum.class, "e_300", "E_624", "WORD_UP", "alice_TheCamel"); }
/** as {@link #valueOfIgnoreCase(String, Enum[], String)} for all values of the given enum and using the enum type as the message */ public static <T extends Enum<?>> Maybe<T> valueOfIgnoreCase(Class<T> type, String givenValue) { return valueOfIgnoreCase(JavaClassNames.simpleClassName(type), values(type), givenValue); }
/** as {@link #checkAllEnumeratedIgnoreCase(String, Enum[], String...)} using the same default strategy * that {@link #valueOfIgnoreCase(Class, String)} applies */ public static void checkAllEnumeratedIgnoreCase(Class<? extends Enum<?>> type, String ...explicitValues) { checkAllEnumeratedIgnoreCase(JavaClassNames.simpleClassName(type), values(type), explicitValues); } /** checks that all accepted enum values are represented by the given set of explicit values */
@Override public ManagementNodeState call(ConfigBag parameters) { HighAvailabilityMode mode = parameters.get(MODE); Preconditions.checkNotNull(mode, MODE.getName() + " parameter is required"); EntityHttpClient httpClient = ((BrooklynNode)entity()).http(); HttpToolResponse resp = httpClient.post("/v1/server/ha/state", ImmutableMap.of("Brooklyn-Allow-Non-Master-Access", "true"), ImmutableMap.of("mode", mode.toString())); if (resp.getResponseCode() == HttpStatus.SC_OK) { Function<HttpToolResponse, ManagementNodeState> parseRespone = Functionals.chain( Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(String.class)), Enums.fromStringFunction(ManagementNodeState.class)); return parseRespone.apply(resp); } else { throw new IllegalStateException("Unexpected response code: " + resp.getResponseCode() + "\n" + resp.getContentAsString()); } } }
.poll(new HttpPollConfig<ManagementNodeState>(MANAGEMENT_NODE_STATE) .suburl("/v1/server/ha/state") .onSuccess(Functionals.chain(Functionals.chain(HttpValueFunctions.jsonContents(), JsonFunctions.cast(String.class)), Enums.fromStringFunction(ManagementNodeState.class))) .setOnFailureOrException(null))
@Override public T apply(String input) { return valueOfIgnoreCase(type, input).orNull(); } }
public static <E extends Enum<E>> Maybe<E> tryCoerce(String input, Class<E> targetType) { if (input==null) return null; if (targetType==null) return Maybe.absent("Null enum type"); if (!targetType.isEnum()) return Maybe.absent("Type '"+targetType+"' is not an enum"); List<String> options = ImmutableList.of( input, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, input), CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, input), CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, input), CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, input)); for (String value : options) { try { return Maybe.of(Enum.valueOf(targetType, value)); } catch (IllegalArgumentException iae) { continue; } } Maybe<E> result = Enums.valueOfIgnoreCase(targetType, input); if (result.isPresent()) return result; return Maybe.absent(new ClassCoercionException("Invalid value '"+input+"' for "+JavaClassNames.simpleClassName(targetType)+"; expected one of "+ Arrays.asList(Enums.values(targetType)))); }
@Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp = ".*leftover values.*vitamin.*") public static void testAllValuesEnumeratedExtra() { Enums.checkAllEnumeratedIgnoreCase(SomeENum.class, "e_300", "E_624", "Vitamin C", "wordUp", "alice_the_camel"); }
@Test public static void testValueOf() { Assert.assertEquals(Enums.valueOfIgnoreCase(SomeENum.class, "e_300").get(), SomeENum.E_300); Assert.assertEquals(Enums.valueOfIgnoreCase(SomeENum.class, "e_624").get(), SomeENum.E_624); Assert.assertEquals(Enums.valueOfIgnoreCase(SomeENum.class, "ALICE_THE_CAMEL").get(), SomeENum.aliceTheCamel); Assert.assertEquals(Enums.valueOfIgnoreCase(SomeENum.class, "alice_the_camel").get(), SomeENum.aliceTheCamel); Assert.assertEquals(Enums.valueOfIgnoreCase(SomeENum.class, "wordUp").get(), SomeENum.WORD_UP); Assert.assertEquals(Enums.valueOfIgnoreCase(SomeENum.class, "wordup").get(), SomeENum.WORD_UP); Assert.assertFalse(Enums.valueOfIgnoreCase(SomeENum.class, "MSG").isPresent()); Assert.assertFalse(Enums.valueOfIgnoreCase(SomeENum.class, "alice_thecamel").isPresent()); Assert.assertFalse(Enums.valueOfIgnoreCase(SomeENum.class, "_word_up").isPresent()); }
@Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp = ".*leftover enums.*E_624.*leftover values.*msg.*") public static void testAllValuesEnumeratedMissingAndExtra() { Enums.checkAllEnumeratedIgnoreCase(SomeENum.class, "e_300", "MSG", "WORD_UP", "aliceTheCamel"); }
protected PersistMode computePersistMode() { Maybe<PersistMode> persistMode = Enums.valueOfIgnoreCase(PersistMode.class, persist); if (!persistMode.isPresent()) { if (Strings.isBlank(persist)) { throw new FatalConfigurationRuntimeException("Persist mode must not be blank"); } else { throw new FatalConfigurationRuntimeException("Illegal persist setting: "+persist); } } if (persistMode.get() == PersistMode.DISABLED) { if (Strings.isNonBlank(persistenceDir)) throw new FatalConfigurationRuntimeException("Cannot specify persistenceDir when persist is disabled"); if (Strings.isNonBlank(persistenceLocation)) throw new FatalConfigurationRuntimeException("Cannot specify persistenceLocation when persist is disabled"); } return persistMode.get(); }
@Test public static void testAllValuesEnumerated() { Enums.checkAllEnumeratedIgnoreCase(SomeENum.class, "e_300", "E_624", "WORD_UP", "aliceTheCamel"); }
protected HighAvailabilityMode computeHighAvailabilityMode(PersistMode persistMode) { Maybe<HighAvailabilityMode> highAvailabilityMode = Enums.valueOfIgnoreCase(HighAvailabilityMode.class, highAvailability); if (!highAvailabilityMode.isPresent()) { if (Strings.isBlank(highAvailability)) { throw new FatalConfigurationRuntimeException("High availability mode must not be blank"); } else { throw new FatalConfigurationRuntimeException("Illegal highAvailability setting: "+highAvailability); } } if (highAvailabilityMode.get() != HighAvailabilityMode.DISABLED) { if (persistMode == PersistMode.DISABLED) { if (highAvailabilityMode.get() == HighAvailabilityMode.AUTO) return HighAvailabilityMode.DISABLED; throw new FatalConfigurationRuntimeException("Cannot specify highAvailability when persistence is disabled"); } else if (persistMode == PersistMode.CLEAN && (highAvailabilityMode.get() == HighAvailabilityMode.STANDBY || highAvailabilityMode.get() == HighAvailabilityMode.HOT_STANDBY || highAvailabilityMode.get() == HighAvailabilityMode.HOT_BACKUP)) { throw new FatalConfigurationRuntimeException("Cannot specify highAvailability "+highAvailabilityMode.get()+" when persistence is CLEAN"); } } return highAvailabilityMode.get(); }
@Test(expectedExceptions=IllegalStateException.class, expectedExceptionsMessageRegExp = ".*leftover enums.*E_624.*leftover values.*") public static void testAllValuesEnumeratedMissing() { Enums.checkAllEnumeratedIgnoreCase(SomeENum.class, "e_300", "word_UP", "aliceTheCamel"); }