All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ioc.commons.impl.android.ui.HasContextWrapper Maven / Gradle / Ivy

package org.ioc.commons.impl.android.ui;

import org.ioc.commons.ui.HasStorage;

import android.app.Application;
import android.content.Context;
import android.view.HasContext;
import android.widget.TextView;

public class HasContextWrapper {

	private HasContextWrapper() {
		/* No const */
	}

	/**
	 * Get a new wrapper from a {@link TextView} instance.
	 * 
	 * @param widget
	 *            The instance to wrap
	 * @return A new wrapper
	 */
	public static HasContext from(final Application app) {
		return new HasContext() {

			@Override
			public Context getContext() {
				return app.getApplicationContext();
			}

		};
	}

	public static HasContext from(final Application app, HasStorage cache) {
		Object cached = cache.retrieve(app);
		HasContext hasContext = (cached instanceof HasContext ? (HasContext) cached : null);
		if (hasContext == null) {
			hasContext = from(app);
			cache.store(app, hasContext);
		}
		return hasContext;
	}

	/**
	 * Get a new wrapper from a {@link TextView} instance.
	 * 
	 * @param widget
	 *            The instance to wrap
	 * @return A new wrapper
	 */
	public static HasContext from(final Context context) {
		return new HasContext() {

			@Override
			public Context getContext() {
				return context;
			}

		};
	}

	public static HasContext from(final Context context, HasStorage cache) {
		Object cached = cache.retrieve(context);
		HasContext hasContext = (cached instanceof HasContext ? (HasContext) cached : null);
		if (hasContext == null) {
			hasContext = from(context);
			cache.store(context, hasContext);
		}
		return hasContext;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy