package de.bmoth.util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

public class Utils {

    public static <T> List<T> sortByTopologicalOrder(final Map<T, Set<T>> dependencies) {
        final Set<T> allValues = new HashSet<>(dependencies.keySet());
        ArrayList<T> sortedList = new ArrayList<T>();
        boolean newRun = true;
        while (newRun) {
            newRun = false;
            final ArrayList<T> todo = new ArrayList<>(allValues);
            todo.removeAll(sortedList);
            for (T element : todo) {
                Set<T> deps = new HashSet<>(dependencies.get(element));
                deps.removeAll(sortedList);
                if (deps.isEmpty()) {
                    sortedList.add(element);
                    newRun = true;
                }
            }
        }
        return sortedList;
    }

    public static <T> List<T> determineCycle(final Set<T> remaining, final Map<T, Set<T>> dependencies) {
        ArrayList<T> cycle = new ArrayList<T>();
        Set<T> set = new HashSet<T>(remaining);
        boolean newRun = true;
        while (newRun) {
            for (T next : set) {
                if (cycle.contains(next)) {
                    newRun = false;
                    cycle.add(next);
                    break;
                } else if (remaining.contains(next)) {
                    cycle.add(next);
                    set = new HashSet<>(dependencies.get(next));
                    break;
                }
            }
        }
        return cycle;
    }

    public static String getFileWithoutExtension(String f) {
        String res = null;
        int i = f.lastIndexOf('.');
        if (i > 0 && i < f.length() - 1) {
            res = f.substring(0, i);
        } else {
            // there is no file name extension
            res = f;
        }
        return res;
    }

    public static final String readFile(final File file) throws FileNotFoundException, IOException {
        final InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),
            Charset.forName("UTF-8"));

        final StringBuilder builder = new StringBuilder();
        final char[] buffer = new char[1024];
        int read;
        while ((read = inputStreamReader.read(buffer)) >= 0) {
            builder.append(String.valueOf(buffer, 0, read));
        }
        String content = builder.toString();

        inputStreamReader.close();

        // remove utf-8 byte order mark
        // replaceAll \uFEFF did not work for some reason
        // apparently, unix like systems report a single character with the code
        // below
        if (!content.isEmpty() && Character.codePointAt(content, 0) == 65279) {
            content = content.substring(1);
        }
        // while windows splits it up into three characters with the codes below
        if (!content.isEmpty() && Character.codePointAt(content, 0) == 239 && Character.codePointAt(content, 1) == 187
            && Character.codePointAt(content, 2) == 191) {
            content = content.substring(3);
        }

        return content.replaceAll("\r\n", "\n");
    }

}

@@ -1989,22 +1989,21 @@
         
-final 
+try (
 InputStr
@@ -2118,17 +2118,19 @@
 UTF-8%22))
-;
+) %7B
 %0A%0A      
@@ -2127,24 +2127,28 @@
  %7B%0A%0A        
+ 
+   
 final String
@@ -2182,24 +2182,28 @@
 gBuilder();%0A
+    
         fina
@@ -2240,24 +2240,28 @@
 4%5D;%0A        
+    
 int read;%0A  
@@ -2262,24 +2262,28 @@
 ad;%0A        
+  
+  
 while ((read
@@ -2337,16 +2337,20 @@
         
+    
 builder.
@@ -2390,34 +2390,42 @@
 read));%0A        
+ 
-%7D%0A
+   %7D%0A    
         String c
@@ -2462,16 +2462,20 @@
         
+    
 inputStr
@@ -2498,24 +2498,28 @@
 );%0A%0A        
+   
+ 
 // remove ut
@@ -2538,16 +2538,20 @@
 er mark%0A
+    
         
@@ -2608,16 +2608,20 @@
         
+ 
+   
 // appar
@@ -2685,16 +2685,20 @@
 he code%0A
+    
         
@@ -2706,32 +2706,36 @@
 / below%0A        
+    
 if (!content.isE
@@ -2794,32 +2794,36 @@
 ) %7B%0A            
+   
+ 
 content = conten
@@ -2838,34 +2838,42 @@
 ing(1);%0A        
-%7D%0A
+    %7D%0A    
         // while
@@ -2933,24 +2933,28 @@
 codes below%0A
+    
         if (
@@ -3072,16 +3072,20 @@
         
+    
 && Chara
@@ -3127,32 +3127,36 @@
 ) %7B%0A            
+    
 content = conten
@@ -3175,24 +3175,28 @@
 3);%0A        
+    
 %7D%0A%0A        r
@@ -3186,32 +3186,36 @@
      %7D%0A%0A        
+ 
+   
 return content.r
@@ -3239,16 +3239,26 @@
  %22%5Cn%22);%0A
+        %7D%0A
     %7D%0A%0A%7D

use try-with-resource