Skip to content

Commit

Permalink
Replace Guava with standard Java where easily possible (#892)
Browse files Browse the repository at this point in the history
Co-authored-by: Hannes Wellmann <[email protected]>
  • Loading branch information
HannesWell and iils-hwellmann authored Jan 10, 2024
1 parent b133b8f commit 2169f5b
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -60,7 +59,6 @@
import org.matheclipse.parser.client.ast.ASTNode;
import org.matheclipse.parser.client.ast.FunctionNode;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;

public class FileFunctions {
private static final Logger LOGGER = LogManager.getLogger();
Expand Down Expand Up @@ -640,15 +638,15 @@ protected static IExpr loadPackage(final EvalEngine engine, final BufferedReader
return S.Null;
}

private static IExpr getFile(File file, IAST ast, String arg1Str, EvalEngine engine) {
private static IExpr getFile(Path file, IAST ast, String arg1Str, EvalEngine engine) {
boolean packageMode = engine.isPackageMode();
String input = engine.get$Input();
String inputFileName = engine.get$InputFileName();
try {
engine.setPackageMode(true);
engine.set$Input(arg1Str);
engine.set$InputFileName(file.getAbsolutePath());
String str = com.google.common.io.Files.asCharSource(file, Charset.defaultCharset()).read();
engine.set$InputFileName(file.toAbsolutePath().toString());
String str = Files.readString(file, Charset.defaultCharset());
return Get.loadPackage(engine, str);
} catch (IOException e) {
// LOGGER.debug("Get.getFile() failed", e);
Expand All @@ -665,11 +663,11 @@ private static IExpr getURL(URL url, IAST ast, String arg1Str, EvalEngine engine
boolean packageMode = engine.isPackageMode();
String input = engine.get$Input();
String inputFileName = engine.get$InputFileName();
try {
try (java.io.InputStream in = url.openStream()) {
engine.setPackageMode(true);
engine.set$Input(arg1Str);
engine.set$InputFileName(url.getPath());
String str = Resources.toString(url, StandardCharsets.UTF_8);
String str = new String(in.readAllBytes(), StandardCharsets.UTF_8);
return loadPackage(engine, str);
} catch (IOException e) {
// LOGGER.debug("FileFunctions.Get.getURL() failed", e);
Expand All @@ -695,12 +693,12 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
URL url = new URL(arg1Str);
return getURL(url, ast, arg1Str, engine);
}
File file = new File(arg1Str);
if (file.exists()) {
Path file = Path.of(arg1Str);
if (Files.isRegularFile(file)) {
return getFile(file, ast, arg1Str, engine);
} else {
file = FileSystems.getDefault().getPath(arg1Str).toAbsolutePath().toFile();
if (file.exists()) {
file = file.toAbsolutePath();
if (Files.isRegularFile(file)) {
return getFile(file, ast, arg1Str, engine);
}
}
Expand Down Expand Up @@ -1160,22 +1158,18 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}
String arg1 = ((IStringX) ast.arg1()).toString();
if (arg1.startsWith("https://") || arg1.startsWith("http://")) {
URL url;
try {
url = new URL(arg1);
String str;
str = Resources.toString(url, StandardCharsets.UTF_8);
try (java.io.InputStream in = new URL(arg1).openStream()) {
String str = new String(in.readAllBytes(), StandardCharsets.UTF_8);
return F.stringx(str);
} catch (IOException ioe) {
LOGGER.log(engine.getLogLevel(), ast.topHead(), ioe);
return F.NIL;
}
}
File file = new File(arg1);
if (file.exists()) {
Path file = Path.of(arg1);
if (Files.isRegularFile(file)) {
try {
String str =
com.google.common.io.Files.asCharSource(file, Charset.defaultCharset()).read();
String str = Files.readString(file, Charset.defaultCharset());
return F.stringx(str);
} catch (IOException e) {
LOGGER.log(engine.getLogLevel(), "ReadString exception", e);
Expand Down Expand Up @@ -1227,10 +1221,8 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}
String arg1Str = ((IStringX) ast.arg1()).toString();
if (arg1Str.startsWith("https://") || arg1Str.startsWith("http://")) {
URL url;
try {
url = new URL(arg1Str);
String str = Resources.toString(url, StandardCharsets.UTF_8);
try (java.io.InputStream in = new URL(arg1Str).openStream()) {
String str = new String(in.readAllBytes(), StandardCharsets.UTF_8);
return F.$s(str);
} catch (IOException e) {
LOGGER.debug("URLFetch.evaluate() failed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import org.matheclipse.core.interfaces.INum;
import org.matheclipse.core.interfaces.ISymbol;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

/** Functions for graph theory algorithms. */
public class GraphFunctions {
Expand Down Expand Up @@ -184,7 +183,7 @@ protected void setOperation(Graph<IExpr, ? extends IExprEdge> graph1,
for (IExpr v : Sets.intersection(graph1.vertexSet(), graph2.vertexSet())) {
resultGraph.addVertex(v);
}
SetView<? extends IExprEdge> graphSet = Sets.intersection(graph1.edgeSet(), graph2.edgeSet());
Set<? extends IExprEdge> graphSet = Sets.intersection(graph1.edgeSet(), graph2.edgeSet());
for (IExprEdge e : graphSet) {
IExpr v1 = e.lhs();
IExpr v2 = e.rhs();
Expand Down Expand Up @@ -283,7 +282,7 @@ protected IExpr setOperation(Graph<IExpr, ? extends IExprEdge> graph1,
for (IExpr v : Sets.union(graph1.vertexSet(), graph2.vertexSet())) {
resultGraph.addVertex(v);
}
SetView<? extends IExprEdge> graphSet = Sets.difference(graph1.edgeSet(), graph2.edgeSet());
Set<? extends IExprEdge> graphSet = Sets.difference(graph1.edgeSet(), graph2.edgeSet());
for (IExprEdge e : graphSet) {
IExpr v1 = e.lhs();
IExpr v2 = e.rhs();
Expand Down Expand Up @@ -362,7 +361,7 @@ protected void setOperation(Graph<IExpr, ? extends IExprEdge> graph1,
for (IExpr v : Sets.union(graph1.vertexSet(), graph2.vertexSet())) {
resultGraph.addVertex(v);
}
SetView<? extends IExprEdge> graphSet = Sets.union(graph1.edgeSet(), graph2.edgeSet());
Set<? extends IExprEdge> graphSet = Sets.union(graph1.edgeSet(), graph2.edgeSet());
for (IExprEdge e : graphSet) {
IExpr v1 = e.lhs();
IExpr v2 = e.rhs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.List;
import java.util.function.Consumer;
import org.apache.logging.log4j.LogManager;
Expand All @@ -27,8 +29,6 @@
import org.matheclipse.core.interfaces.IStringX;
import org.matheclipse.core.interfaces.ISymbol;
import org.matheclipse.parser.client.ast.ASTNode;
import com.google.common.io.Files;
import com.google.common.io.Resources;

public class UnitTestingFunctions {
private static final Logger LOGGER = LogManager.getLogger();
Expand Down Expand Up @@ -104,7 +104,7 @@ private static IExpr getFile(File file, IAST ast, EvalEngine engine) {
// boolean packageMode = engine.isPackageMode();
try {
// engine.setPackageMode(true);
String str = Files.asCharSource(file, Charset.defaultCharset()).read();
String str = Files.readString(file.toPath(), Charset.defaultCharset());
return runTests(engine, str);
} catch (IOException e) {
LOGGER.debug("TestReport.getFile() failed", e);
Expand All @@ -117,9 +117,9 @@ private static IExpr getFile(File file, IAST ast, EvalEngine engine) {

private static IExpr getURL(URL url, IAST ast, EvalEngine engine) {
// boolean packageMode = engine.isPackageMode();
try {
try (InputStream in = url.openStream()) {
// engine.setPackageMode(true);
String str = Resources.toString(url, StandardCharsets.UTF_8);
String str = new String(in.readAllBytes(), StandardCharsets.UTF_8);
return runTests(engine, str);
} catch (IOException e) {
LOGGER.debug("TestReport.getURL() failed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.matheclipse.core.interfaces.IStringX;
import org.matheclipse.core.interfaces.ISymbol;
import org.matheclipse.core.tensor.qty.IQuantity;
import com.google.common.io.ByteStreams;

/**
* Methods for handling the WXF serialization format.
Expand Down Expand Up @@ -1089,7 +1088,7 @@ public static IExpr deserializeResource(String resourceName, boolean internal) {
if (resourceAsStream == null) {
return F.NIL;
} else {
byte[] byteArray = ByteStreams.toByteArray(resourceAsStream);
byte[] byteArray = resourceAsStream.readAllBytes();
return deserializeInternal(byteArray);
}
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.expression.DataExpr;
import org.matheclipse.core.expression.S;
import org.matheclipse.core.interfaces.IExpr;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;

public class InputStreamExpr extends DataExpr<InputStream> implements Externalizable {
Expand Down Expand Up @@ -63,7 +62,7 @@ public DataInput getDataInput() throws IOException {
*/
public Reader getReader() throws IOException {
if (reader == null) {
String str = CharStreams.toString(new InputStreamReader(fData, Charsets.UTF_8));
String str = new String(fData.readAllBytes(), StandardCharsets.UTF_8);
if (dataIn != null) {
dataIn = null;
}
Expand Down Expand Up @@ -108,10 +107,9 @@ public static InputStreamExpr newInstance(final File file, String streamName, fi
return new InputStreamExpr(new FileInputStream(file), streamName, reader);
}

public static InputStreamExpr newInstance(final Reader reader)
throws IOException, FileNotFoundException {
public static InputStreamExpr newInstance(final Reader reader) throws IOException {
InputStream targetStream =
new ByteArrayInputStream(CharStreams.toString(reader).getBytes(Charsets.UTF_8));
new ByteArrayInputStream(CharStreams.toString(reader).getBytes(StandardCharsets.UTF_8));
reader.reset();
return new InputStreamExpr(targetStream, "String", reader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.matheclipse.parser.trie.TrieMatch;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.google.common.base.CharMatcher;
import uk.ac.ed.ph.snuggletex.InputError;
import uk.ac.ed.ph.snuggletex.SnuggleEngine;
import uk.ac.ed.ph.snuggletex.SnuggleInput;
Expand Down Expand Up @@ -610,7 +609,7 @@ private ISymbol identifier(NodeList list, int[] position) {
}

protected static ISymbol createFunction(String str) {
if (CharMatcher.javaLetterOrDigit().matchesAllOf(str)) {
if (str.chars().allMatch(Character::isLetterOrDigit)) {
return F.symbol(str);
}
return F.$s(str);
Expand All @@ -630,7 +629,7 @@ protected ISymbol createSymbol(String str) {
return S.E;
}
}
if (CharMatcher.javaLetterOrDigit().matchesAllOf(str)) {
if (str.chars().allMatch(Character::isLetterOrDigit)) {
return F.symbol(str);
}
return F.$s(str);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.matheclipse.core.reflection.system;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -116,13 +117,11 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}
}
} else if (format.equals(Extension.DAT)) {
File file = new File(filename);
com.google.common.io.Files.write(arg2.toString(), file, Charset.defaultCharset());
Files.writeString(Path.of(filename), arg2.toString(), Charset.defaultCharset());
return arg1;
} else if (format.equals(Extension.WXF)) {
File file = new File(filename);
byte[] bArray = WL.serialize(arg2);
com.google.common.io.Files.write(bArray, file);
Files.write(Path.of(filename), bArray);
return arg1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
}
}
// } else if (format.equals(Extension.DAT)) {
// File file = new File(arg1.toString());
// com.google.common.io.Files.write(arg2.toString(), file, Charset.defaultCharset());
// Path file = Path.of(arg1.toString());
// Files.writeString(file, arg2.toString(), Charset.defaultCharset());
// return arg1;
// } else if (format.equals(Extension.WXF)) {
// File file = new File(arg1.toString());
// Path file = Path.of(arg1.toString());
// byte[] bArray = WL.serialize(arg2);
// com.google.common.io.Files.write(bArray, file);
// Files.write(file, bArray);
// return arg1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.matheclipse.core.interfaces.INumber;
import org.matheclipse.core.sympy.utilities.Iterables;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

public class Expr {

Expand Down Expand Up @@ -87,12 +86,12 @@ public static Pair argsCnc(IExpr that, boolean warn, boolean split_1) {

private static boolean asIndependentHas(IExpr e, IASTAppendable other, Set<IExpr> sym) {
boolean hasOther = e.has(x -> other.indexOf(x) >= 0, true);
if (sym.size() == 0) {
if (sym.isEmpty()) {
return hasOther;
}
VariablesSet vs = new VariablesSet(e);
SetView<IExpr> intersection = Sets.intersection(sym, vs.getVariablesSet());
return hasOther || e.has(x -> intersection.contains(x), true);
Set<IExpr> intersection = Sets.intersection(sym, vs.getVariablesSet());
return hasOther || e.has(intersection::contains, true);
}

public static Pair asIndependent(IExpr self, IAST deps) {
Expand Down

0 comments on commit 2169f5b

Please sign in to comment.