diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2024-06-30 13:45:57 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-30 13:45:57 +0200 | 
| commit | 8aa8b5834953cab15c0687608f4fafea2e6c61be (patch) | |
| tree | 1835ae7c789136b4a6c44c12efd4247a0b96d893 /src/semanticanalysis/Share.java | |
| parent | 7125bb27fedaafd5a56b5122e4251b182448ddac (diff) | |
| parent | 06d7c8dee25c065b1a569337f34d3cd5e892a31d (diff) | |
Merge pull request #10 from boozec/check-semantics
Co-Authored-By: geno <gabriele.genovese2@studio.unibo.it>
Co-Authored-By: L0P0P <grassoemanuele@live.com> 
Diffstat (limited to 'src/semanticanalysis/Share.java')
| -rw-r--r-- | src/semanticanalysis/Share.java | 58 | 
1 files changed, 58 insertions, 0 deletions
diff --git a/src/semanticanalysis/Share.java b/src/semanticanalysis/Share.java new file mode 100644 index 0000000..7542cf7 --- /dev/null +++ b/src/semanticanalysis/Share.java @@ -0,0 +1,58 @@ +package semanticanalysis; + +import java.io.*; +import java.util.*; + +public class Share { + +    /** +     * 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(); + +        for (SemanticError element : list) { +            if (!customContains(newList, element)) { +                newList.add(element); +            } +        } +        return newList; +    } + +    /** +     * 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 (SemanticError element : list) { +            String e2 = element.toString(); +            if (e2.equals(e1)) { +                return true; +            } +        } +        return false; +    } + +    public static String getExtension(String fileName) { +        int extensionIndex = fileName.lastIndexOf('.'); +        if (extensionIndex == -1) { +            return fileName; +        } else { +            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(); +    } +}  |