// Path: core/src/main/java/org/codeandmagic/promise/Either.java
// public static <T1> Either<Throwable, T1> trying(F0<T1> block) {
//     try {
//         return new Right<>(block.apply());
//     } catch (Throwable e) {
//         return new Left<>(e);
//     }
// }


import org.codeandmagic.promise.sample.R;
import org.codeandmagic.promise.volley.BitmapLruCache;
import org.codeandmagic.promise.volley.ImageResult;
import org.codeandmagic.promise.volley.VolleyImagePromise;
import org.codeandmagic.promise.volley.VolleyJsonPromise;
import static org.codeandmagic.promise.Either.trying;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.patterns_fragment, container, false);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            if (mAdapter == null) {
                mRequestQueue = Volley.newRequestQueue(getActivity());
                mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
                mAdapter = new ImageAdapter(getActivity());
                ((GridView) getView().findViewById(R.id.grid)).setAdapter(mAdapter);
                doMagic();
            } else {
                ((GridView) getView().findViewById(R.id.grid)).setAdapter(mAdapter);
            }
        }

        /**
         * The ENTIRE code needed to:
         * 1. Retrieve a JSON
         * 2. Parse the JSON and for each element which contains an URL
         * 2.1. Download an image and add it to the GridView adapter
         */
        private void doMagic() {
            VolleyJsonPromise
                .jsonArrayPromise(mRequestQueue, URL)
// Path: core/src/main/java/org/codeandmagic/promise/Either.java
// public static <T1> Either<Throwable, T1> trying(F0<T1> block) {
//     try {
//         return new Right<>(block.apply());
//     } catch (Throwable e) {
//         return new Left<>(e);
//     }
// }


// Path: sample/src/main/java/org/codeandmagic/promise/sample/patterns/PatternsActivity.java
import org.codeandmagic.promise.sample.R;
import org.codeandmagic.promise.volley.BitmapLruCache;
import org.codeandmagic.promise.volley.ImageResult;
import org.codeandmagic.promise.volley.VolleyImagePromise;
import org.codeandmagic.promise.volley.VolleyJsonPromise;
import static org.codeandmagic.promise.Either.trying;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.patterns_fragment, container, false);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            if (mAdapter == null) {
                mRequestQueue = Volley.newRequestQueue(getActivity());
                mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache());
                mAdapter = new ImageAdapter(getActivity());
                ((GridView) getView().findViewById(R.id.grid)).setAdapter(mAdapter);
                doMagic();
            } else {
                ((GridView) getView().findViewById(R.id.grid)).setAdapter(mAdapter);
            }
        }

        /**
         * The ENTIRE code needed to:
         * 1. Retrieve a JSON
         * 2. Parse the JSON and for each element which contains an URL
         * 2.1. Download an image and add it to the GridView adapter
         */
        private void doMagic() {
            VolleyJsonPromise
                .jsonArrayPromise(mRequestQueue, URL)
                .flatMap(value -> trying(() -> PatternDeserializer.getInstance().deserialize(value)))