Update number type mapper

Dieser Commit ist enthalten in:
yoyosource 2022-12-18 13:31:33 +01:00
Ursprung 441661a8f5
Commit 8d1b5c5f66

Datei anzeigen

@ -58,10 +58,10 @@ public class SWCommandUtils {
if (s.equalsIgnoreCase("false")) return false; if (s.equalsIgnoreCase("false")) return false;
return null; return null;
}, s -> Arrays.asList("true", "false"))); }, s -> Arrays.asList("true", "false")));
addMapper(float.class, Float.class, createMapper(numberMapper(Float::parseFloat), numberCompleter(Float::parseFloat))); addMapper(float.class, Float.class, createMapper(numberMapper(Float::parseFloat), numberCompleter(Float::parseFloat, true)));
addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble))); addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble, true)));
addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt))); addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt, false)));
addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong))); addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong, false)));
MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList)); MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList));
} }
@ -255,10 +255,25 @@ public class SWCommandUtils {
}; };
} }
private static Function<String, Collection<String>> numberCompleter(Function<String, ?> mapper) { private static Function<String, Collection<String>> numberCompleter(Function<String, ?> mapper, boolean comma) {
return s -> numberMapper(mapper).apply(s) != null return s -> {
? Collections.singletonList(s) if (numberMapper(mapper).apply(s) == null) {
: Collections.emptyList(); return Collections.emptyList();
}
List<String> strings = new ArrayList<>();
if (s.length() == 0) {
strings.add("-");
} else {
strings.add(s);
}
for (int i = 0; i < 10; i++) {
strings.add(s + i);
}
if (comma && (!s.contains(".") || !s.contains(","))) {
strings.add(s + ".");
}
return strings;
};
} }
static <T extends Annotation> T[] getAnnotation(Method method, Class<T> annotation) { static <T extends Annotation> T[] getAnnotation(Method method, Class<T> annotation) {