Created
March 29, 2014 18:03
-
-
Save passy/9859181 to your computer and use it in GitHub Desktop.
Options in Java. Probably a terrible idea.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rdrei.android.scdl2.ui; | |
import android.view.InflateException; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.crashlytics.android.Crashlytics; | |
import com.google.android.gms.ads.AdRequest; | |
import com.google.android.gms.ads.AdView; | |
import com.google.inject.Inject; | |
import com.google.inject.Provider; | |
import net.rdrei.android.option.Option; | |
import net.rdrei.android.scdl2.ApplicationPreferences; | |
import net.rdrei.android.scdl2.R; | |
import net.rdrei.android.scdl2.guice.ActivityLayoutInflater; | |
import roboguice.util.Ln; | |
/** | |
* Loads and injects an AdView if required into an existing layout. | |
* | |
* @author pascal | |
*/ | |
public class AdViewManager { | |
private Provider<ApplicationPreferences> mPreferencesProvider; | |
private ActivityLayoutInflater mInflater; | |
@Inject | |
public AdViewManager(Provider<ApplicationPreferences> preferencesProvider, | |
ActivityLayoutInflater inflater) { | |
this.mPreferencesProvider = preferencesProvider; | |
this.mInflater = inflater; | |
} | |
private Option<View> getAdView(final ViewGroup baseView) { | |
try { | |
return Option.some(mInflater.inflate(R.layout.adview, baseView, false)); | |
} catch (InflateException exc) { | |
Ln.e(exc); | |
Crashlytics.logException(exc); | |
} | |
return Option.none(); | |
} | |
private Option<View> setupView(final View view) { | |
AdView adView; | |
try { | |
adView = (AdView) view; | |
} catch (ClassCastException err) { | |
adView = null; | |
Crashlytics.logException(err); | |
} | |
if (adView != null) { | |
final AdRequest adRequest = new AdRequest.Builder() | |
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) | |
.build(); | |
adView.loadAd(adRequest); | |
return Option.some(adView); | |
} | |
return Option.none(); | |
} | |
/** | |
* Add the AdView to the given baseView. | |
* | |
* @param baseView The layout element to add the view to. | |
*/ | |
public void addToView(final ViewGroup baseView) { | |
final Option<View> adView = getAdView(baseView); | |
adView | |
.flatMap(this::setupView) | |
.call(baseView::addView); | |
} | |
/** | |
* Add to the given baseView, but only if the application is non-adfree. | |
* | |
* @param baseView The view you want to see your ads in. | |
* @return True if added to layout, false if not. | |
*/ | |
public boolean addToViewIfRequired(final ViewGroup baseView) { | |
final boolean showAds = !mPreferencesProvider.get().isAdFree(); | |
if (showAds) { | |
addToView(baseView); | |
} | |
return showAds; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rdrei.android.option; | |
public interface Consumer1<A> { | |
void accept(A a); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rdrei.android.option; | |
public interface Func1<A1, R> { | |
R apply(A1 arg1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rdrei.android.option; | |
import java.util.NoSuchElementException; | |
public class None<A> extends Option<A> { | |
None() { | |
} | |
@Override | |
public <B> Option<B> map(Func1<A, B> f) { | |
return NONE; | |
} | |
@Override | |
public Option<A> call(Consumer1<A> f) { | |
return this; | |
} | |
@Override | |
public <B> Option<B> flatMap(Func1<A, Option<B>> f) { | |
return NONE; | |
} | |
@Override | |
public Option<A> filter(Predicate<? super A> predicate) { | |
return NONE; | |
} | |
@Override | |
public A getOrElse(A def) { | |
return def; | |
} | |
@Override | |
public boolean isEmpty() { | |
return true; | |
} | |
@Override | |
public A get() { | |
throw new NoSuchElementException(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rdrei.android.option; | |
public abstract class Option<A> { | |
public static final None NONE = new None(); | |
public abstract <B> Option<B> map(Func1<A, B> f); | |
public abstract Option<A> call(Consumer1<A> f); | |
public abstract <B> Option<B> flatMap(Func1<A, Option<B>> f); | |
public abstract Option<A> filter(Predicate<? super A> predicate); | |
public abstract A getOrElse(A def); | |
public abstract boolean isEmpty(); | |
public abstract A get(); | |
@SuppressWarnings("unchecked") | |
public static <A> Some<A> some(A value) { | |
return new Some(value); | |
} | |
@SuppressWarnings("unchecked") | |
public static <A> None<A> none() { | |
return NONE; | |
} | |
public static <A> Option<A> asOption(A value) { | |
if (value == null) return none(); | |
else return some(value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rdrei.android.option; | |
public interface Predicate<T> { | |
boolean test(T t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.rdrei.android.option; | |
public class Some<A> extends Option<A> { | |
private final A value; | |
Some(A value) { | |
this.value = value; | |
} | |
@Override | |
public <B> Option<B> map(Func1<A, B> f) { | |
return some(f.apply(value)); | |
} | |
@Override | |
public Option<A> call(Consumer1<A> f) { | |
f.accept(value); | |
return this; | |
} | |
@Override | |
public <B> Option<B> flatMap(Func1<A, Option<B>> f) { | |
return f.apply(value); | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public Option<A> filter(Predicate<? super A> predicate) { | |
if (predicate.test(value)) { | |
return this; | |
} else { | |
return None.NONE; | |
} | |
} | |
@Override | |
public A getOrElse(A def) { | |
return value; | |
} | |
@Override | |
public boolean isEmpty() { | |
return false; | |
} | |
@Override | |
public A get() { | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment