Drew reminds that humans like sorted drop downs #4339

This commit is contained in:
rusefillc 2022-07-12 05:01:36 -04:00
parent f3f6fe288c
commit cede67c5e5
3 changed files with 48 additions and 13 deletions

Binary file not shown.

View File

@ -3,6 +3,9 @@ package com.rusefi.test;
import com.rusefi.VariableRegistry; import com.rusefi.VariableRegistry;
import org.junit.Test; import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import static com.rusefi.VariableRegistry.*; import static com.rusefi.VariableRegistry.*;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -55,4 +58,14 @@ public class VariableRegistryTest {
assertEquals("1", registry.get("key_int")); assertEquals("1", registry.get("key_int"));
assertEquals("1", registry.get("key_int" + _HEX_SUFFIX)); assertEquals("1", registry.get("key_int" + _HEX_SUFFIX));
} }
@Test
public void testHumanSorted() {
Map<Integer, String> input = new HashMap<>();
input.put(0, "NONE");
input.put(1, "A");
input.put(2, "Z");
input.put(3, "N");
assertEquals("0=\"NONE\",1=\"A\",3=\"N\",2=\"Z\"", VariableRegistry.getString(input));
}
} }

View File

@ -2,12 +2,14 @@ package com.rusefi;
import com.devexperts.logging.Logging; import com.devexperts.logging.Logging;
import com.rusefi.enum_reader.Value; import com.rusefi.enum_reader.Value;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -90,12 +92,9 @@ public class VariableRegistry {
* @return value>name map for specified enum name. * @return value>name map for specified enum name.
*/ */
@Nullable @Nullable
public TreeMap<Integer, String> resolveEnumValues(EnumsReader enumsReader, String enumName) { private TreeMap<Integer, String> resolveEnumValues(@NotNull EnumsReader.EnumState stringValueMap) {
TreeMap<Integer, String> valueNameById = new TreeMap<>(); TreeMap<Integer, String> valueNameById = new TreeMap<>();
EnumsReader.EnumState stringValueMap = enumsReader.getEnums().get(enumName);
if (stringValueMap == null)
return null;
for (Value value : stringValueMap.values()) { for (Value value : stringValueMap.values()) {
if (value.isForceSize()) if (value.isForceSize())
continue; continue;
@ -113,21 +112,44 @@ public class VariableRegistry {
return valueNameById; return valueNameById;
} }
private static void appendCommaIfNeeded(StringBuilder sb) {
if (sb.length() > 0)
sb.append(",");
}
public String getEnumOptionsForTunerStudio(EnumsReader enumsReader, String enumName) { public String getEnumOptionsForTunerStudio(EnumsReader enumsReader, String enumName) {
TreeMap<Integer, String> valueNameById = resolveEnumValues(enumsReader, enumName); EnumsReader.EnumState stringValueMap = enumsReader.getEnums().get(enumName);
if (stringValueMap == null)
return null;
TreeMap<Integer, String> valueNameById = resolveEnumValues(stringValueMap);
if (valueNameById == null) if (valueNameById == null)
return null; return null;
int maxValue = valueNameById.lastKey(); return getString(valueNameById);
}
private static String quote(String string) {
return "\"" + string + "\"";
}
@NotNull
public static String getString(Map<Integer, String> valueNameById) {
TreeMap<Integer, String> humanDropDownSorted = new TreeMap<>((o1, o2) -> {
if (o1.intValue() == o2)
return 0;
if (o1.intValue() == 0)
return -1; // "None" always go first
if (o2.intValue()==0)
return 1;
return valueNameById.get(o1).compareTo(valueNameById.get(o2));
});
humanDropDownSorted.putAll(valueNameById);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// todo: TS enum key-value form #4232 for (Map.Entry<Integer, String> e : humanDropDownSorted.entrySet()) {
for (int i = 0; i <= maxValue; i++) { appendCommaIfNeeded(sb);
if (sb.length() > 0) sb.append(e.getKey() + "=" + quote(e.getValue()));
sb.append(", ");
String value = valueNameById.getOrDefault(i, INVALID);
sb.append("\"" + value + "\"");
} }
return sb.toString(); return sb.toString();
} }