Example #1
0
 public void testValueOfFunction_equals() {
   new EqualsTester()
       .addEqualityGroup(
           Enums.valueOfFunction(TestEnum.class), Enums.valueOfFunction(TestEnum.class))
       .addEqualityGroup(Enums.valueOfFunction(OtherEnum.class))
       .testEquals();
 }
Example #2
0
  @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));
  }
Example #3
0
  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);
  }
Example #4
0
 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"));
 }
Example #5
0
  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());
  }
Example #6
0
 public void testStringConverter_convertError() {
   Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
   try {
     converter.convert("xxx");
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Example #7
0
 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));
 }
Example #8
0
 @GwtIncompatible("NullPointerTester")
 public void testStringConverter_nullPointerTester() throws Exception {
   Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
   NullPointerTester tester = new NullPointerTester();
   tester.testAllPublicInstanceMethods(converter);
 }
Example #9
0
 public void testValueOfFunction_nullWhenNoMatchingConstant() {
   Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
   assertNull(function.apply("WOMBAT"));
 }
Example #10
0
 public void testValueOfFunction_caseSensitive() {
   Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
   assertNull(function.apply("cHEETO"));
   assertNull(function.apply("Honda"));
   assertNull(function.apply("poodlE"));
 }
Example #11
0
 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"));
 }
Example #12
0
 public <A extends Enum<A>> A getEnumValue(final String name, final Class<A> type) {
   return Enums.stringConverter(type).convert(getString(name));
 }
Example #13
0
 public void testStringConverter_serialization() {
   SerializableTester.reserializeAndAssert(Enums.stringConverter(TestEnum.class));
 }
Example #14
0
 public static CpuFamily fromValue(String v) {
   return Enums.getIfPresent(CpuFamily.class, v).or(UNRECOGNIZED);
 }
Example #15
0
 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());
 }
Example #16
0
 @GwtIncompatible("SerializableTester")
 public void testValueOfFunction_serialization() {
   Function<String, TestEnum> function = Enums.valueOfFunction(TestEnum.class);
   SerializableTester.reserializeAndAssert(function);
 }
Example #17
0
 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));
 }
Example #18
0
 public void testStringConverter_nullConversions() {
   Converter<String, TestEnum> converter = Enums.stringConverter(TestEnum.class);
   assertNull(converter.convert(null));
   assertNull(converter.reverse().convert(null));
 }
Example #19
0
 @GwtIncompatible("Class.getName()")
 public void testStringConverter_toString() {
   assertEquals(
       "Enums.stringConverter(com.google.common.base.EnumsTest$TestEnum.class)",
       Enums.stringConverter(TestEnum.class).toString());
 }
Example #20
0
 public void testGetIfPresent_whenNoMatchingConstant() {
   assertEquals(Optional.absent(), Enums.getIfPresent(TestEnum.class, "WOMBAT"));
 }
Example #21
0
 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()));
 }