From 258c8cb46318282bdf83ccbb731daedd4bcbac4f Mon Sep 17 00:00:00 2001 From: Benjamin von Berg Date: Fri, 18 Feb 2022 19:46:26 +0100 Subject: [PATCH] purged UniqueString from the community modules --- modules/tlc2/overrides/CSV.java | 15 +++++----- modules/tlc2/overrides/IOUtils.java | 43 ++++++++++++++--------------- modules/tlc2/overrides/Json.java | 27 +++++++++--------- modules/tlc2/overrides/SVG.java | 24 ++++++++-------- 4 files changed, 51 insertions(+), 58 deletions(-) diff --git a/modules/tlc2/overrides/CSV.java b/modules/tlc2/overrides/CSV.java index 67db509..b595151 100644 --- a/modules/tlc2/overrides/CSV.java +++ b/modules/tlc2/overrides/CSV.java @@ -45,7 +45,6 @@ import tlc2.value.impl.StringValue; import tlc2.value.impl.TupleValue; import tlc2.value.impl.Value; -import util.UniqueString; public class CSV { @@ -59,8 +58,8 @@ public static Value write(final StringValue template, final Value parameter, fin } final Object[] params = Arrays.asList(tv.getElems()).stream().map(v -> v.toString()) .toArray(size -> new Object[size]); - Files.write(Paths.get(absolutePath.val.toString()), - (String.format(template.val.toString(), params) + System.lineSeparator()).getBytes("UTF-8"), + Files.write(Paths.get(absolutePath.val), + (String.format(template.val, params) + System.lineSeparator()).getBytes("UTF-8"), StandardOpenOption.CREATE, StandardOpenOption.APPEND); return BoolValue.ValTrue; } @@ -75,19 +74,19 @@ public static Value read(final Value columns, final StringValue delim, final Str new String[] { "CSVRead", "sequence", Values.ppr(columns.toString()) }); } - final Path path = Paths.get(absolutePath.val.toString()); + final Path path = Paths.get(absolutePath.val); if (!path.toFile().exists()) { // There are zero records in a file that doesn't exist. return TupleValue.EmptyTuple; } - final UniqueString[] names = Arrays.asList(tv.elems).stream().map(v -> ((StringValue) v).val) - .collect(Collectors.toList()).toArray(UniqueString[]::new); + final String[] names = Arrays.asList(tv.elems).stream().map(v -> ((StringValue) v).val) + .collect(Collectors.toList()).toArray(String[]::new); final List lines = Files.readAllLines(path); final Value[] records = new Value[lines.size()]; for (int i = 0; i < lines.size(); i++) { - StringValue[] values = Arrays.asList(lines.get(i).split(delim.val.toString())).stream() + StringValue[] values = Arrays.asList(lines.get(i).split(delim.val)).stream() .map(s -> new StringValue(s)).collect(Collectors.toList()).toArray(StringValue[]::new); records[i] = new RecordValue(names, values, false); } @@ -98,7 +97,7 @@ public static Value read(final Value columns, final StringValue delim, final Str @TLAPlusOperator(identifier = "CSVRecords", module = "CSV", minLevel = 1, warn = false) public static Value records(final StringValue absolutePath) throws IOException { - final Path path = Paths.get(absolutePath.val.toString()); + final Path path = Paths.get(absolutePath.val); if (!path.toFile().exists()) { // There are zero records in a file that doesn't exist. return IntValue.ValZero; diff --git a/modules/tlc2/overrides/IOUtils.java b/modules/tlc2/overrides/IOUtils.java index 62a224f..c497786 100644 --- a/modules/tlc2/overrides/IOUtils.java +++ b/modules/tlc2/overrides/IOUtils.java @@ -57,16 +57,15 @@ import tlc2.value.impl.StringValue; import tlc2.value.impl.TupleValue; import tlc2.value.impl.Value; -import util.UniqueString; public class IOUtils { @TLAPlusOperator(identifier = "IODeserialize", module = "IOUtils", warn = false) public static final IValue ioDeserialize(final StringValue absolutePath, final BoolValue compress) throws IOException { - final ValueInputStream vis = new ValueInputStream(new File(absolutePath.val.toString()), compress.val); + final ValueInputStream vis = new ValueInputStream(new File(absolutePath.val), compress.val); try { - return vis.read(UniqueString.internTbl.toMap()); + return vis.read(); } finally { vis.close(); } @@ -75,7 +74,7 @@ public static final IValue ioDeserialize(final StringValue absolutePath, final B @TLAPlusOperator(identifier = "IOSerialize", module = "IOUtils", warn = false) public static final IValue ioSerialize(final IValue value, final StringValue absolutePath, final BoolValue compress) throws IOException { - final ValueOutputStream vos = new ValueOutputStream(new File(absolutePath.val.toString()), compress.val); + final ValueOutputStream vos = new ValueOutputStream(new File(absolutePath.val), compress.val); try { value.write(vos); } finally { @@ -110,7 +109,7 @@ public synchronized static Value textSerialize(final Tool tool, final ExprOrOpAr } final StringValue serializer = (StringValue) opts.apply(new StringValue("format"), EvalControl.Clear); - if("TXT".equals(serializer.getVal().toString())) { + if("TXT".equals(serializer.getVal())) { final StringValue payload; final StringValue filepath; @@ -132,11 +131,11 @@ public synchronized static Value textSerialize(final Tool tool, final ExprOrOpAr try { Files.writeString( - Paths.get(filepath.getVal().toString()), - payload.getVal().toString(), - Charset.forName(charset.getVal().toString()), + Paths.get(filepath.getVal()), + payload.getVal(), + Charset.forName(charset.getVal()), Arrays.asList(openOptions).stream() - .map(e -> StandardOpenOption.valueOf(e.getVal().toString()) ) + .map(e -> StandardOpenOption.valueOf(e.getVal()) ) .toArray(size -> new StandardOpenOption[size])); return new RecordValue(EXEC_NAMES, new Value[] { IntValue.ValZero, new StringValue(successmsg), new StringValue("") }, false); @@ -173,7 +172,7 @@ public synchronized static Value textDeserialize(final Tool tool, final ExprOrOp } final StringValue serializer = (StringValue) opts.apply(new StringValue("format"), EvalControl.Clear); - if("TXT".equals(serializer.getVal().toString())) { + if("TXT".equals(serializer.getVal())) { final StringValue filepath; final StringValue charset; @@ -186,9 +185,7 @@ public synchronized static Value textDeserialize(final Tool tool, final ExprOrOp } try { - final String result = Files.readString( - Paths.get(filepath.getVal().toString()), - Charset.forName(charset.getVal().toString())); + final String result = Files.readString(Paths.get(filepath.getVal()), Charset.forName(charset.getVal())); return new RecordValue(EXEC_NAMES, new Value[] { IntValue.ValZero, new StringValue(result), new StringValue("") }, false); @@ -206,12 +203,12 @@ public synchronized static Value textDeserialize(final Tool tool, final ExprOrOp // process executes. final Map env = System.getenv(); - final UniqueString[] names = new UniqueString[env.size()]; + final String[] names = new String[env.size()]; final StringValue[] values = new StringValue[env.size()]; final List> entries = new ArrayList<>(env.entrySet()); for (int i = 0; i < entries.size(); i++) { - names[i] = UniqueString.of(entries.get(i).getKey()); + names[i] = entries.get(i).getKey(); values[i] = new StringValue(entries.get(i).getValue()); } @@ -223,7 +220,7 @@ public static Value atoi(final Value v) { if (v instanceof StringValue) { final StringValue sv = (StringValue) v; try { - final int i = Integer.parseInt(sv.val.toString()); + final int i = Integer.parseInt(sv.val); return IntValue.gen(i); } catch (Exception e) { // "fall-through" to eval exception below. @@ -350,9 +347,9 @@ private static Map getEnv(final RecordValue environment) { // Convert record of environment variables to what ProcessBuilder works with. final Map penv = new HashMap<>(); for (int i = 0; i < environment.size(); i++) { - final UniqueString name = environment.names[i]; + final String name = environment.names[i]; final Value value = environment.values[i]; - penv.put(name.toString(), value.toUnquotedString()); + penv.put(name, value.toUnquotedString()); } return penv; } @@ -398,11 +395,11 @@ private static String convert(IValue v) { } final StringValue sv = (StringValue) v; - return sv.val.toString(); + return sv.val; } - private static final UniqueString EXITVALUE = UniqueString.uniqueStringOf("exitValue"); - private static final UniqueString STDOUT = UniqueString.uniqueStringOf("stdout"); - private static final UniqueString STDERR = UniqueString.uniqueStringOf("stderr"); - private static final UniqueString[] EXEC_NAMES = new UniqueString[] { EXITVALUE, STDOUT, STDERR }; + private static final String EXITVALUE = ("exitValue"); + private static final String STDOUT = ("stdout"); + private static final String STDERR = ("stderr"); + private static final String[] EXEC_NAMES = new String[] { EXITVALUE, STDOUT, STDERR }; } diff --git a/modules/tlc2/overrides/Json.java b/modules/tlc2/overrides/Json.java index 2970173..168d654 100644 --- a/modules/tlc2/overrides/Json.java +++ b/modules/tlc2/overrides/Json.java @@ -58,7 +58,6 @@ import tlc2.value.impl.SubsetValue; import tlc2.value.impl.TupleValue; import tlc2.value.impl.Value; -import util.UniqueString; /** * Module overrides for operators to read and write JSON. @@ -107,7 +106,7 @@ public static StringValue toJsonObject(final IValue value) throws IOException { @TLAPlusOperator(identifier = "ndJsonDeserialize", module = "Json", warn = false) public static IValue ndDeserialize(final StringValue path) throws IOException { List values = new ArrayList<>(); - try (BufferedReader reader = new BufferedReader(new FileReader(new File(path.val.toString())))) { + try (BufferedReader reader = new BufferedReader(new FileReader(new File(path.val)))) { String line = reader.readLine(); while (line != null) { JsonElement node = JsonParser.parseString(line); @@ -126,7 +125,7 @@ public static IValue ndDeserialize(final StringValue path) throws IOException { */ @TLAPlusOperator(identifier = "JsonDeserialize", module = "Json", warn = false) public static IValue deserialize(final StringValue path) throws IOException { - JsonElement node = JsonParser.parseReader(new FileReader(new File(path.val.toString()))); + JsonElement node = JsonParser.parseReader(new FileReader(new File(path.val))); return getValue(node); } @@ -139,9 +138,9 @@ public static IValue deserialize(final StringValue path) throws IOException { */ @TLAPlusOperator(identifier = "ndJsonSerialize", module = "Json", warn = false) public synchronized static BoolValue ndSerialize(final StringValue path, final TupleValue value) throws IOException { - File file = new File(path.val.toString()); + File file = new File(path.val); if (file.getParentFile() != null) {file.getParentFile().mkdirs();} // Cannot create parent dir for relative path. - try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val.toString())))) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val)))) { for (int i = 0; i < value.elems.length; i++) { writer.write(getNode(value.elems[i]).toString() + "\n"); } @@ -158,9 +157,9 @@ public synchronized static BoolValue ndSerialize(final StringValue path, final T */ @TLAPlusOperator(identifier = "JsonSerialize", module = "Json", warn = false) public synchronized static BoolValue serialize(final StringValue path, final TupleValue value) throws IOException { - File file = new File(path.val.toString()); + File file = new File(path.val); if (file.getParentFile() != null) {file.getParentFile().mkdirs();} // Cannot create parent dir for relative path. - try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val.toString())))) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path.val)))) { writer.write("[\n"); for (int i = 0; i < value.elems.length; i++) { writer.write(getNode(value.elems[i]).toString()); @@ -186,9 +185,9 @@ private static JsonElement getNode(IValue value) throws IOException { } else if (value instanceof TupleValue) { return getArrayNode((TupleValue) value); } else if (value instanceof StringValue) { - return new JsonPrimitive(((StringValue) value).val.toString()); + return new JsonPrimitive(((StringValue) value).val); } else if (value instanceof ModelValue) { - return new JsonPrimitive(((ModelValue) value).val.toString()); + return new JsonPrimitive(((ModelValue) value).val); } else if (value instanceof IntValue) { return new JsonPrimitive(((IntValue) value).val); } else if (value instanceof BoolValue) { @@ -272,7 +271,7 @@ private static JsonElement getObjectNode(FcnRcdValue value) throws IOException { for (int i = 0; i < domain.length; i++) { Value domainValue = domain[i]; if (domainValue instanceof StringValue) { - jsonObject.add(((StringValue) domainValue).val.toString(), getNode(value.values[i])); + jsonObject.add(((StringValue) domainValue).val, getNode(value.values[i])); } else { jsonObject.add(domainValue.toString(), getNode(value.values[i])); } @@ -289,7 +288,7 @@ private static JsonElement getObjectNode(FcnRcdValue value) throws IOException { private static JsonElement getObjectNode(RecordValue value) throws IOException { JsonObject jsonObject = new JsonObject(); for (int i = 0; i < value.names.length; i++) { - jsonObject.add(value.names[i].toString(), getNode(value.values[i])); + jsonObject.add(value.names[i], getNode(value.values[i])); } return jsonObject; } @@ -440,15 +439,15 @@ private static TupleValue getTupleValue(JsonElement node) throws IOException { * @return the record value */ private static RecordValue getRecordValue(JsonElement node) throws IOException { - List keys = new ArrayList<>(); + List keys = new ArrayList<>(); List values = new ArrayList<>(); Iterator> iterator = node.getAsJsonObject().entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = iterator.next(); - keys.add(UniqueString.uniqueStringOf(entry.getKey())); + keys.add(entry.getKey()); values.add(getValue(entry.getValue())); } - return new RecordValue(keys.toArray(new UniqueString[keys.size()]), values.toArray(new Value[values.size()]), + return new RecordValue(keys.toArray(new String[keys.size()]), values.toArray(new Value[values.size()]), false); } diff --git a/modules/tlc2/overrides/SVG.java b/modules/tlc2/overrides/SVG.java index 984ec05..7363fd9 100644 --- a/modules/tlc2/overrides/SVG.java +++ b/modules/tlc2/overrides/SVG.java @@ -50,7 +50,6 @@ import tlc2.value.impl.TupleValue; import tlc2.value.impl.Value; import tlc2.value.impl.ValueVec; -import util.UniqueString; public final class SVG { @@ -78,8 +77,7 @@ public static Value SVGElemToString(Value elem) throws Exception { RecordValue frv = (RecordValue) elem.toRcd(); // Get 'name'. - StringValue nameVal = (StringValue) frv.apply(new StringValue("name"), 0); - String name = nameVal.getVal().toString(); + String name = ((StringValue) frv.apply(new StringValue("name"), 0)).getVal(); // Get 'attrs'. We convert it to 'RecordValue' type, which we expect should always be possible. Value attrsVal = frv.apply(new StringValue("attrs"), 0); @@ -88,11 +86,11 @@ public static Value SVGElemToString(Value elem) throws Exception { throw new Exception("Was unable to convert element to a record: " + attrsVal.toString()); } String attrStr = ""; - for (UniqueString us : attrs.names) { + for (String us : attrs.names) { attrStr += " "; - attrStr += us.toString().replaceAll("_", "-"); + attrStr += us.replaceAll("_", "-"); attrStr += "="; - String v = ((StringValue) attrs.apply(new StringValue(us), 0)).getVal().toString(); + String v = ((StringValue) attrs.apply(new StringValue(us), 0)).getVal(); // Quote all SVG attribute values. Technically, attribute values in HTML // don't always need to be quoted, but we use quotes so we can handle all // possible values. We single quote them to play nice with TLC string formatting. @@ -132,7 +130,7 @@ public static Value SVGElemToString(Value elem) throws Exception { // to be empty for all other element types, but since it's not rendered, we don't // explicitly disallow it. StringValue innerTextVal = (StringValue) frv.apply(new StringValue("innerText"), 0); - String innerText = innerTextVal.getVal().toString(); + String innerText = innerTextVal.getVal(); // Make sure TLA+ tuples such as <<1,2,3>> get properly rendered. innerText = innerText.replaceAll("<<", "<<").replaceAll(">>", ">>"); @@ -154,7 +152,7 @@ public static Value ringNetwork(IntValue cx, IntValue cy, IntValue r, IntValue n // Polar to Cartesian coordinates offset by (cx,cy). final int x = (int) (cx.val + r.val * Math.cos(angle)); final int y = (int) (cy.val + r.val * Math.sin(angle)); - return new RecordValue(new UniqueString[] {UniqueString.of("x"), UniqueString.of("y")}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false); + return new RecordValue(new String[] {"x", "y"}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false); } @TLAPlusOperator(identifier = "NodesOfDirectedMultiGraph", module = "SVG", warn = false) @@ -183,7 +181,7 @@ public static Value directedMultiGraph(final SetEnumValue nodes, final SetEnumVa // Algorithm final StringValue algo = (StringValue) opts.apply(new StringValue("algo"), EvalControl.Clear); - getAlgo(algo.val.toString(), opts).visit(layoutModel); + getAlgo(algo.val, opts).visit(layoutModel); // Get the node's coordinates from the algorithm. final Map locations = layoutModel.getLocations(); @@ -196,7 +194,7 @@ public static Value directedMultiGraph(final SetEnumValue nodes, final SetEnumVa private static Value point2Value(final Point p) { final int x = ((Double) p.x).intValue(); final int y = ((Double) p.y).intValue(); - return new RecordValue(new UniqueString[] { UniqueString.of("x"), UniqueString.of("y") }, + return new RecordValue(new String[] { "x", "y" }, new Value[] { IntValue.gen(x), IntValue.gen(y) }, false); } @@ -207,12 +205,12 @@ private static LayoutAlgorithm getAlgo(final String algo, final RecordVal switch (algo) { case "Sugiyama": // https://github.com/tomnelson/jungrapht-visualization/blob/afd155bf0246e5185f054ba1429bbcfbd429292a/jungrapht-layout/src/main/java/org/jungrapht/visualization/layout/algorithms/sugiyama/Layering.java#L4-L7 - String l = ((StringValue) opts.apply(new StringValue("layering"), EvalControl.Clear)).val.toString(); + String l = ((StringValue) opts.apply(new StringValue("layering"), EvalControl.Clear)).val; return SugiyamaLayoutAlgorithm.edgeAwareBuilder() .layering(Layering.valueOf(l)).vertexBoundsFunction(v -> Rectangle.of(-5, -5, nodeW.val, nodeH.val)).threaded(false) .build(); case "Eiglsperger": - l = ((StringValue) opts.apply(new StringValue("layering"), EvalControl.Clear)).val.toString(); + l = ((StringValue) opts.apply(new StringValue("layering"), EvalControl.Clear)).val; return EiglspergerLayoutAlgorithm.edgeAwareBuilder() .layering(Layering.valueOf(l)).vertexBoundsFunction(v -> Rectangle.of(-5, -5, nodeW.val, nodeH.val)).threaded(false) .build(); @@ -233,6 +231,6 @@ public static Value pointOnLine(final RecordValue from, final RecordValue to, fi int x = (int) (fx + ((tx - fx) / (idx.val * 1d))); int y = (int) (fy + ((ty - fy) / (idx.val * 1d))); - return new RecordValue(new UniqueString[] {UniqueString.of("x"), UniqueString.of("y")}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false); + return new RecordValue(new String[] {"x", "y"}, new Value[] {IntValue.gen(x), IntValue.gen(y)}, false); } }