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 {
        try (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");
        }
    }

}

@@ -867,1025 +867,8 @@
  %7D%0A%0A
-    public static %3CT%3E List%3CT%3E determineCycle(final Set%3CT%3E remaining, final Map%3CT, Set%3CT%3E%3E dependencies) %7B%0A        ArrayList%3CT%3E cycle = new ArrayList%3CT%3E();%0A        Set%3CT%3E set = new HashSet%3CT%3E(remaining);%0A        boolean newRun = true;%0A        while (newRun) %7B%0A            for (T next : set) %7B%0A                if (cycle.contains(next)) %7B%0A                    newRun = false;%0A                    cycle.add(next);%0A                    break;%0A                %7D else if (remaining.contains(next)) %7B%0A                    cycle.add(next);%0A                    set = new HashSet%3C%3E(dependencies.get(next));%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        return cycle;%0A    %7D%0A%0A    public static String getFileWithoutExtension(String f) %7B%0A        String res = null;%0A        int i = f.lastIndexOf('.');%0A        if (i %3E 0 && i %3C f.length() - 1) %7B%0A            res = f.substring(0, i);%0A        %7D else %7B%0A            // there is no file name extension%0A            res = f;%0A        %7D%0A        return res;%0A    %7D%0A%0A
     

remove unused code