Example usage for com.google.common.base Enums stringConverter

List of usage examples for com.google.common.base Enums stringConverter

Introduction

In this page you can find the example usage for com.google.common.base Enums stringConverter.

Prototype

public static <T extends Enum<T>> Converter<String, T> stringConverter(final Class<T> enumClass) 

Source Link

Document

Returns a converter that converts between strings and enum values of type enumClass using Enum#valueOf(Class,String) and Enum#name() .

Usage

From source file:uk.q3c.krail.i18n.jpa.PatternEntity.java

public PatternEntity(PatternCacheKey cacheKey, String value) {
    final Enum<?> enumKey = cacheKey.getKey();
    Converter<String, ? extends Enum> keyBase = Enums.stringConverter(enumKey.getClass());
    String keyString = keyBase + "." + enumKey.name();
    this.i18nkey = keyString;
    this.locale = cacheKey.getRequestedLocale().toLanguageTag();
    this.value = value;
}

From source file:google.registry.model.registrar.RegistrarContact.java

public static ImmutableSet<Type> typesFromStrings(Iterable<String> typeNames) {
    return FluentIterable.from(typeNames).transform(Enums.stringConverter(Type.class)).toSet();
}

From source file:com.github.rinde.rinsim.cli.ArgumentParser.java

/**
 * Create a parser for {@link Enum}s.//from w w  w.j a  v a  2  s.  c  o m
 * @param name The name for the value of the option (typically the enum name).
 * @param enumClass The class of the enum.
 * @param <T> The class of the enum.
 * @return A new {@link ArgumentParser} for instances of the specified enum.
 */
public static <T extends Enum<T>> ArgumentParser<T> enumParser(String name, Class<T> enumClass) {
    return asParser(name, Enums.stringConverter(enumClass));
}

From source file:com.github.rinde.rinsim.cli.ArgumentParser.java

/**
 * Create a parser for lists of {@link Enum}s.
 * @param name The name for the values of the option (typically the enum name
 *          with 'list' appended)./*from www  .  j  a v  a 2 s.  c om*/
 * @param enumClass The class of the enum.
 * @param <T> The class of the enum.
 * @return A new {@link ArgumentParser} for lists of instances of the
 *         specified enum.
 */
public static <T extends Enum<T>> ArgumentParser<List<T>> enumListParser(String name, Class<T> enumClass) {
    return asListParser(name, Enums.stringConverter(enumClass));
}

From source file:google.registry.tools.RegistrarContactCommand.java

@Override
protected void init() throws Exception {
    checkArgument(mainParameters.size() == 1, "Must specify exactly one client identifier: %s",
            ImmutableList.copyOf(mainParameters));
    String clientId = mainParameters.get(0);
    Registrar registrar = checkNotNull(Registrar.loadByClientId(clientId), "Registrar %s not found", clientId);
    contactTypes = newHashSet(/*  w  w  w.ja  va 2 s  . c  o m*/
            transform(nullToEmpty(contactTypeNames), Enums.stringConverter(RegistrarContact.Type.class)));
    ImmutableSet<RegistrarContact> contacts = registrar.getContacts();
    Map<String, RegistrarContact> contactsMap = new LinkedHashMap<>();
    for (RegistrarContact rc : contacts) {
        contactsMap.put(rc.getEmailAddress(), rc);
    }
    RegistrarContact oldContact;
    switch (mode) {
    case LIST:
        listContacts(contacts);
        break;
    case CREATE:
        stageEntityChange(null, createContact(registrar));
        break;
    case UPDATE:
        oldContact = checkNotNull(
                contactsMap.get(checkNotNull(email, "--email is required when --mode=UPDATE")),
                "No contact with the given email: %s", email);
        RegistrarContact newContact = updateContact(oldContact, registrar);
        stageEntityChange(oldContact, newContact);
        break;
    case DELETE:
        oldContact = checkNotNull(
                contactsMap.get(checkNotNull(email, "--email is required when --mode=DELETE")),
                "No contact with the given email: %s", email);
        stageEntityChange(oldContact, null);
        break;
    default:
        throw new AssertionError();
    }
    if (mode == Mode.CREATE || mode == Mode.UPDATE || mode == Mode.DELETE) {
        stageEntityChange(registrar, registrar.asBuilder().setContactsRequireSyncing(true).build());
    }
}