Merge "Merge branch 'stable-2.14'"

This commit is contained in:
David Pursehouse 2017-04-20 13:14:23 +00:00 committed by Gerrit Code Review
commit 7f14019e1a
2 changed files with 25 additions and 23 deletions

View File

@ -16,7 +16,7 @@ package com.google.gerrit.pgm.init.api;
import com.google.gerrit.common.Die;
import java.io.Console;
import java.lang.reflect.InvocationTargetException;
import java.util.EnumSet;
import java.util.Set;
/** Console based interaction with the invoking user. */
@ -37,20 +37,6 @@ public abstract class ConsoleUI {
return new Die("aborted by user");
}
/** Obtain all values from an enumeration. */
@SuppressWarnings("unchecked")
protected static <T extends Enum<?>> T[] all(final T value) {
try {
return (T[]) value.getDeclaringClass().getMethod("values").invoke(null);
} catch (IllegalArgumentException
| NoSuchMethodException
| InvocationTargetException
| IllegalAccessException
| SecurityException e) {
throw new IllegalArgumentException("Cannot obtain enumeration values", e);
}
}
/** @return true if this is a batch UI that has no user interaction. */
public abstract boolean isBatch();
@ -95,7 +81,8 @@ public abstract class ConsoleUI {
}
/** Prompt the user to make a choice from an enumeration's values. */
public abstract <T extends Enum<?>> T readEnum(T def, String fmt, Object... args);
public abstract <T extends Enum<?>, A extends EnumSet<? extends T>> T readEnum(
T def, A options, String fmt, Object... args);
private static class Interactive extends ConsoleUI {
private final Console console;
@ -208,9 +195,9 @@ public abstract class ConsoleUI {
}
@Override
public <T extends Enum<?>> T readEnum(T def, String fmt, Object... args) {
public <T extends Enum<?>, A extends EnumSet<? extends T>> T readEnum(
T def, A options, String fmt, Object... args) {
final String prompt = String.format(fmt, args);
final T[] options = all(def);
for (; ; ) {
String r = console.readLine("%-30s [%s/?]: ", prompt, def.toString());
if (r == null) {
@ -277,7 +264,8 @@ public abstract class ConsoleUI {
}
@Override
public <T extends Enum<?>> T readEnum(T def, String fmt, Object... args) {
public <T extends Enum<?>, A extends EnumSet<? extends T>> T readEnum(
T def, A options, String fmt, Object... args) {
return def;
}

View File

@ -22,6 +22,7 @@ import com.google.inject.assistedinject.Assisted;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;
/** Helper to edit a section of the configuration files. */
@ -110,15 +111,28 @@ public class Section {
return site.resolve(string(title, name, defValue));
}
public <T extends Enum<?>> T select(final String title, final String name, final T defValue) {
public <T extends Enum<?>, E extends EnumSet<? extends T>> T select(
String title, String name, T defValue) {
return select(title, name, defValue, false);
}
public <T extends Enum<?>> T select(
final String title, final String name, final T defValue, final boolean nullIfDefault) {
public <T extends Enum<?>, E extends EnumSet<? extends T>> T select(
String title, String name, T defValue, boolean nullIfDefault) {
@SuppressWarnings("unchecked")
E allowedValues = (E) EnumSet.allOf(defValue.getClass());
return select(title, name, defValue, allowedValues, nullIfDefault);
}
public <T extends Enum<?>, E extends EnumSet<? extends T>> T select(
String title, String name, T defValue, E allowedValues) {
return select(title, name, defValue, allowedValues, false);
}
public <T extends Enum<?>, A extends EnumSet<? extends T>> T select(
String title, String name, T defValue, A allowedValues, final boolean nullIfDefault) {
final boolean set = get(name) != null;
T oldValue = flags.cfg.getEnum(section, subsection, name, defValue);
T newValue = ui.readEnum(oldValue, "%s", title);
T newValue = ui.readEnum(oldValue, allowedValues, "%s", title);
if (nullIfDefault && newValue == defValue) {
newValue = null;
}