package com.turlir.abakgists.templater.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.ViewGroup;

public abstract class DynamicForm<T> implements Form<T> {

    private final ViewGroup mGroup;
    private final Context mContext;

    private Template<T> mTemplate;

    private T value;

    public DynamicForm(@NonNull ViewGroup group) {
        mGroup = group;
        mContext = group.getContext();
    }

    @Override
    public final void create() {
        mGroup.removeAllViews();
        mTemplate = createTemplate();
    }

    @Override
    public /*final*/ void connect() {
        if (mTemplate == null) {
            throw new IllegalStateException("connect() before create()");
        }
        mTemplate.connect(mGroup);
    }

    @Override
    public final void bind(@NonNull T value) {
        if (mTemplate == null) {
            throw new IllegalStateException("bind() before create()");
        }
        if (mGroup.getChildCount() < 1) {
            throw new IllegalStateException("bind() before connect()");
        }
        this.value = value;
        mTemplate.bind();
        interact();
    }

    @Override
    public boolean verify() {
        if (mTemplate == null) {
            throw new IllegalStateException("verify() before create()");
        }
        return mTemplate.verify();
    }

    protected final T value() {
        if (value == null) {
            throw new IllegalStateException();
        }
        return value;
    }

    @NonNull
    @Override
    public T collect() {
        mTemplate.collect(value);
        return value;
    }

    protected abstract Template<T> createTemplate();

    protected abstract void interact();

    protected final Context getContext() {
        return mContext;
    }
}

@@ -586,219 +586,8 @@
  %7D%0A%0A
-    @Override%0A    public /*final*/ void connect() %7B%0A        if (mTemplate == null) %7B%0A            throw new IllegalStateException(%22connect() before create()%22);%0A        %7D%0A        mTemplate.connect(mGroup);%0A    %7D%0A%0A
     
@@ -991,22 +991,30 @@
 lic 
-boolean verify
+/*final*/ void connect
 () %7B
@@ -1092,22 +1092,23 @@
 eption(%22
-verify
+connect
 () befor
@@ -1139,23 +1139,16 @@
         
-return 
 mTemplat
@@ -1153,52 +1153,71 @@
 ate.
-verify();%0A    %7D%0A%0A    protected final T value
+connect(mGroup);%0A    %7D%0A%0A    @Override%0A    public boolean verify
 () %7B
@@ -1229,20 +1229,24 @@
     if (
-valu
+mTemplat
 e == nul
@@ -1294,16 +1294,42 @@
 ception(
+%22verify() before create()%22
 );%0A     
@@ -1340,37 +1340,50 @@
 %0A        return 
-value
+mTemplate.verify()
 ;%0A    %7D%0A%0A    @No
@@ -1486,24 +1486,171 @@
 lue;%0A    %7D%0A%0A
+    protected final T value() %7B%0A        if (value == null) %7B%0A            throw new IllegalStateException();%0A        %7D%0A        return value;%0A    %7D%0A%0A
     protecte

clean code