package org.ccci.gto.android.common.support.v4.util;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;

public final class FragmentUtils {
    private FragmentUtils() {
    }

    public static <T> T findView(final Fragment fragment, final Class<T> clazz, final int id) {
        final View root = fragment.getView();
        if (root != null) {
            final View view = root.findViewById(id);
            if (clazz.isInstance(view)) {
                return clazz.cast(view);
            }
        }
        return null;
    }

    public static <T> T getAncestorFragment(final Fragment fragment, final Class<T> clazz) {
        Fragment parent = fragment.getParentFragment();
        while (parent != null) {
            if (clazz.isInstance(parent)) {
                return clazz.cast(parent);
            }
            parent = parent.getParentFragment();
        }
        return null;
    }

    public static <T> T getListener(final Fragment fragment, final Class<T> clazz) {
        final Fragment frag = fragment.getParentFragment();
        if (clazz.isInstance(frag)) {
            return clazz.cast(frag);
        }

        final FragmentActivity activity = fragment.getActivity();
        if (clazz.isInstance(activity)) {
            return clazz.cast(activity);
        }

        return null;
    }
}

@@ -47,16 +47,103 @@
 .util;%0A%0A
+import android.support.annotation.NonNull;%0Aimport android.support.annotation.Nullable;%0A
 import a
@@ -313,23 +313,32 @@
 tils() %7B
+%7D%0A
 %0A    
-%7D%0A
+@Nullable
 %0A    pub
@@ -363,16 +363,25 @@
 indView(
+@NonNull 
 final Fr
@@ -388,32 +388,41 @@
 agment fragment,
+ @NonNull
  final Class%3CT%3E 
@@ -698,32 +698,46 @@
 rn null;%0A    %7D%0A%0A
+    @Nullable%0A
     public stati
@@ -764,16 +764,25 @@
 ragment(
+@NonNull 
 final Fr
@@ -789,32 +789,41 @@
 agment fragment,
+ @NonNull
  final Class%3CT%3E 
@@ -1104,24 +1104,38 @@
 ull;%0A    %7D%0A%0A
+    @Nullable%0A
     public s
@@ -1158,16 +1158,25 @@
 istener(
+@NonNull 
 final Fr
@@ -1191,16 +1191,25 @@
 ragment,
+ @NonNull
  final C

add several support annotations to FragmentUtils