public void testValueOfFunction_equals() { new EqualsTester() .addEqualityGroup( Enums.valueOfFunction(TestEnum.class), Enums.valueOfFunction(TestEnum.class)) .addEqualityGroup(Enums.valueOfFunction(OtherEnum.class)) .testEquals(); }
@GwtIncompatible("reflection") public void testGetField() { Field foo = Enums.getField(AnEnum.FOO); assertEquals("FOO", foo.getName()); assertTrue(foo.isAnnotationPresent(ExampleAnnotation.class)); Field bar = Enums.getField(AnEnum.BAR); assertEquals("BAR", bar.getName()); assertFalse(bar.isAnnotationPresent(ExampleAnnotation.class)); }
public IndicatorStyle seenIndicatorStyle() { String value = preferences.getString("pref_seen_indicator_style", IndicatorStyle.NONE.toString()); assert value != null; return Enums.getIfPresent(IndicatorStyle.class, value).or(IndicatorStyle.NONE); }
public NetworkGroupFilterSupport() { super( builderFor(NetworkGroup.class) .withTagFiltering(NetworkGroupTag.class, "networkGroup") .withStringProperty("description", FilterFunctions.DESCRIPTION) .withStringProperty("group-id", FilterFunctions.GROUP_ID) .withStringProperty("group-name", CloudMetadatas.toDisplayName()) .withStringSetProperty("ip-permission.cidr", FilterSetFunctions.PERMISSION_CIDR) .withStringSetProperty( "ip-permission.from-port", FilterSetFunctions.PERMISSION_FROM_PORT) .withStringSetProperty( "ip-permission.group-name", FilterSetFunctions.PERMISSION_GROUP) .withStringSetProperty( "ip-permission.protocol", FilterSetFunctions.PERMISSION_PROTOCOL) .withStringSetProperty("ip-permission.to-port", FilterSetFunctions.PERMISSION_TO_PORT) .withStringSetProperty( "ip-permission.user-id", FilterSetFunctions.PERMISSION_ACCOUNT_ID) .withStringProperty("owner-id", FilterFunctions.ACCOUNT_ID) .withPersistenceAlias("networkRules", "networkRules") .withPersistenceFilter("description") .withPersistenceFilter("group-id", "groupId") .withPersistenceFilter("group-name", "displayName") .withPersistenceFilter( "ip-permission.from-port", "networkRules.lowPort", PersistenceFilter.Type.Integer) .withPersistenceFilter( "ip-permission.protocol", "networkRules.protocol", Enums.valueOfFunction(NetworkRule.Protocol.class)) .withPersistenceFilter( "ip-permission.to-port", "networkRules.highPort", PersistenceFilter.Type.Integer) .withPersistenceFilter("owner-id", "ownerAccountNumber")); }
public void testGetIfPresent() { assertEquals(Optional.of(TestEnum.CHEETO), Enums.getIfPresent(TestEnum.class, "CHEETO")); assertEquals(Optional.of(TestEnum.HONDA), Enums.getIfPresent(TestEnum.class, "HONDA")); assertEquals(Optional.of(TestEnum.POODLE), Enums.getIfPresent(TestEnum.class, "POODLE")); assertTrue(Enums.getIfPresent(TestEnum.class, "CHEETO").isPresent()); assertTrue(Enums.getIfPresent(TestEnum.class, "HONDA").isPresent()); assertTrue(Enums.getIfPresent(TestEnum.class, "POODLE").isPresent()); assertEquals(TestEnum.CHEETO, Enums.getIfPresent(TestEnum.class, "CHEETO").get()); assertEquals(TestEnum.HONDA, Enums.getIfPresent(TestEnum.class, "HONDA").get()); assertEquals(TestEnum.POODLE, Enums.getIfPresent(TestEnum.class, "POODLE").get()); }
public void testStringConverter_convertError() { Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); try { converter.convert("xxx"); fail(); } catch (IllegalArgumentException expected) { } }
public void testStringConverter_convert() { Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); assertEquals(TestEnum.CHEETO, converter.convert("CHEETO")); assertEquals(TestEnum.HONDA, converter.convert("HONDA")); assertEquals(TestEnum.POODLE, converter.convert("POODLE")); assertNull(converter.convert(null)); assertNull(converter.reverse().convert(null)); }
@GwtIncompatible("NullPointerTester") public void testStringConverter_nullPointerTester() throws Exception { Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); NullPointerTester tester = new NullPointerTester(); tester.testAllPublicInstanceMethods(converter); }
public void testValueOfFunction_nullWhenNoMatchingConstant() { Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class); assertNull(function.apply("WOMBAT")); }
public void testValueOfFunction_caseSensitive() { Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class); assertNull(function.apply("cHEETO")); assertNull(function.apply("Honda")); assertNull(function.apply("poodlE")); }
public void testValueOfFunction() { Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class); assertEquals(TestEnum.CHEETO, function.apply("CHEETO")); assertEquals(TestEnum.HONDA, function.apply("HONDA")); assertEquals(TestEnum.POODLE, function.apply("POODLE")); }
public <A extends Enum<A>> A getEnumValue(final String name, final Class<A> type) { return Enums.stringConverter(type).convert(getString(name)); }
public void testStringConverter_serialization() { SerializableTester.reserializeAndAssert(Enums.stringConverter(TestEnum.class)); }
public static CpuFamily fromValue(String v) { return Enums.getIfPresent(CpuFamily.class, v).or(UNRECOGNIZED); }
public void testGetIfPresent_caseSensitive() { assertFalse(Enums.getIfPresent(TestEnum.class, "cHEETO").isPresent()); assertFalse(Enums.getIfPresent(TestEnum.class, "Honda").isPresent()); assertFalse(Enums.getIfPresent(TestEnum.class, "poodlE").isPresent()); }
@GwtIncompatible("SerializableTester") public void testValueOfFunction_serialization() { Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class); SerializableTester.reserializeAndAssert(function); }
public void testStringConverter_reverse() { Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); assertEquals("CHEETO", converter.reverse().convert(TestEnum.CHEETO)); assertEquals("HONDA", converter.reverse().convert(TestEnum.HONDA)); assertEquals("POODLE", converter.reverse().convert(TestEnum.POODLE)); }
public void testStringConverter_nullConversions() { Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class); assertNull(converter.convert(null)); assertNull(converter.reverse().convert(null)); }
@GwtIncompatible("Class.getName()") public void testStringConverter_toString() { assertEquals( "Enums.stringConverter(com.google.common.base.EnumsTest$TestEnum.class)", Enums.stringConverter(TestEnum.class).toString()); }
public void testGetIfPresent_whenNoMatchingConstant() { assertEquals(Optional.absent(), Enums.getIfPresent(TestEnum.class, "WOMBAT")); }
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) { WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value); return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get())); }