package minestra.collection;

import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;

final class TestUtils {

    private TestUtils() {
        //
    }

    private static void error(String msg) {
        throw new RuntimeException(msg);
    }

    static boolean hasMethod(Class<?> targetClass, String methodName) {
        // System.out.println("----------");
        if (isBlank(methodName))
            error("method name is empty");
        Predicate<? super Method> f = x -> x.getName().equals(methodName);
        for (Class<?> c : getAllRelatedClassesOf(targetClass)) {
            // System.out.printf(">>>? %s %s %n", c, methodName);
            // System.out.println(">>> " +
            // Stream.of(c.getDeclaredMethods()).filter(f).collect(Collectors.toList()));
            Optional<Method> r = Stream.of(c.getDeclaredMethods()).filter(f).findFirst();
            if (r.isPresent())
                return true;
        }
        return false;
    }

    private static Set<Class<?>> getAllRelatedClassesOf(Class<?> c) {
        Set<Class<?>> a = new LinkedHashSet<>();
        a.add(c);
        for (Class<?> class1 : c.getInterfaces())
            a.addAll(getAllRelatedClassesOf(class1));
        Class<?> superClass = c.getSuperclass();
        if (superClass != null)
            a.add(superClass);
        // System.out.println( a.stream().map(x ->
        // x.getSimpleName()).collect(Collectors.toList()));
        return a;
    }

    static boolean isBlank(CharSequence s) {
        return s == null || s.toString().trim().length() == 0;
    }

    @SafeVarargs
    static <T> T[] arr(T... a) {
        return a;
    }

    static int[] iarr(int... a) {
        return a;
    }

    static long[] larr(long... a) {
        return a;
    }

    static double[] darr(double... a) {
        return a;
    }

}

@@ -444,53 +444,8 @@
 ) %7B%0A
-        // System.out.println(%22----------%22);%0A
     
@@ -472,16 +472,18 @@
 odName))
+ %7B
 %0A       
@@ -518,16 +518,26 @@
 mpty%22);%0A
+        %7D%0A
         
@@ -672,207 +672,8 @@
 ) %7B%0A
-            // System.out.printf(%22%3E%3E%3E? %25s %25s %25n%22, c, methodName);%0A            // System.out.println(%22%3E%3E%3E %22 +%0A            // Stream.of(c.getDeclaredMethods()).filter(f).collect(Collectors.toList()));%0A
     
@@ -788,16 +788,18 @@
 esent())
+ %7B
 %0A       
@@ -820,16 +820,30 @@
 n true;%0A
+            %7D%0A
         
@@ -1059,16 +1059,18 @@
 faces())
+ %7B
 %0A       
@@ -1112,24 +1112,34 @@
 f(class1));%0A
+        %7D%0A
         Clas
@@ -1206,16 +1206,18 @@
 != null)
+ %7B
 %0A       
@@ -1252,111 +1252,9 @@
     
-// System.out.println( a.stream().map(x -%3E%0A        // x.getSimpleName()).collect(Collectors.toList()));
+%7D
 %0A   

format and reduce comment-out codes in TestUtils