package org.xblackcat.sjpu.builder;

import org.xblackcat.sjpu.storage.IFunctionalAH;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * Functional builder allows only one abstract method per class/interface.
 *
 * @author xBlackCat
 */
public class FunctionalClassBuilder<Base> extends ClassBuilder<Base> {
    public FunctionalClassBuilder(IDefiner definerF, IMethodBuilder... builders) {
        super(definerF, builders);
    }

    @Override
    public <T extends Base> Class<? extends T> build(
            Class<T> target
    ) throws GeneratorException {
        int abstractCount = 0;
        for (Method m : target.getMethods()) {
            if (Modifier.isAbstract(m.getModifiers())) {
                abstractCount++;
            }
        }
        for (Method m : target.getDeclaredMethods()) {
            if (!Modifier.isPublic(m.getModifiers()) && Modifier.isAbstract(m.getModifiers())) {
                abstractCount++;
            }
        }

        if (abstractCount != 1) {
            throw new GeneratorException(
                    "Only single abstract method is allowed for implementation of " +
                            BuilderUtils.getName(IFunctionalAH.class) + ". " + BuilderUtils.getName(target) + " has " +
                            abstractCount
            );
        }

        return super.build(target);
    }
}

@@ -34,58 +34,8 @@
 r;%0A%0A
-import org.xblackcat.sjpu.storage.IFunctionalAH;%0A%0A
 impo
@@ -1096,88 +1096,65 @@
  of 
-%22 +%0A                            BuilderUtils.getName(IFunctionalAH.class) + %22. %22
+functional interface/class. %22%0A                           
  + B
@@ -1196,36 +1196,8 @@
  %22 +
-%0A                           
  abs

Remove extra dependency for class builder