summaryrefslogtreecommitdiff
path: root/src/semanticanalysis/Share.java
diff options
context:
space:
mode:
authorgeno <gabriele.genovese2@studio.unibo.it>2024-06-28 12:23:28 +0200
committergeno <gabriele.genovese2@studio.unibo.it>2024-06-28 12:23:28 +0200
commit37665fb6d0bc1eb29396ae949354cf7d6f9d54ca (patch)
tree058204d723fb31b3b30bf8d141cf621c6f42c0c6 /src/semanticanalysis/Share.java
parent1d5c4862e136419ab1ed3fcf8d8edaa0ee5fda10 (diff)
resolving all the comments santo made
Diffstat (limited to 'src/semanticanalysis/Share.java')
-rw-r--r--src/semanticanalysis/Share.java33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/semanticanalysis/Share.java b/src/semanticanalysis/Share.java
index c1f03c2..c98fc53 100644
--- a/src/semanticanalysis/Share.java
+++ b/src/semanticanalysis/Share.java
@@ -1,13 +1,18 @@
package semanticanalysis;
-import java.util.ArrayList;
+import java.util.*;
+import java.io.*;
public class Share {
- public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) {
- ArrayList<T> newList = new ArrayList<T>();
+ /**
+ * Removes the duplicate elements in a list of Semantic Errors. It's not
+ * generic because it's used a custom contains function.
+ */
+ public static ArrayList<SemanticError> removeDuplicates(ArrayList<SemanticError> list) {
+ ArrayList<SemanticError> newList = new ArrayList<SemanticError>();
- for (T element : list) {
+ for (SemanticError element : list) {
if (!customContains(newList, element)) {
newList.add(element);
}
@@ -15,9 +20,14 @@ public class Share {
return newList;
}
- public static <T> boolean customContains(ArrayList<T> list, T e) {
+ /**
+ * Normal contains did not work, so we made a custom contains function.
+ * Returns `true` if the String rappresentation of an object in the list is
+ * equal to the element given in input.
+ */
+ private static boolean customContains(ArrayList<SemanticError> list, SemanticError e) {
String e1 = e.toString();
- for (T element : list) {
+ for (SemanticError element : list) {
String e2 = element.toString();
if (e2.equals(e1)) {
return true;
@@ -34,4 +44,15 @@ public class Share {
return fileName.substring(extensionIndex + 1);
}
}
+
+ public static String readFile(String filePath) throws IOException {
+ StringBuilder content = new StringBuilder();
+ try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ content.append(line).append("\n");
+ }
+ }
+ return content.toString();
+ }
}