Last active
October 19, 2016 09:23
-
-
Save kingster/df6097f1357b709d7c207d77a88047cb to your computer and use it in GitHub Desktop.
Try & Option in Java
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 com.hivefive.ciphernod.scalaz; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
import java8.util.function.Function; | |
/** | |
* Created by kinshuk.bairagi on 20/02/16. | |
*/ | |
public abstract class Option<A> implements Iterable<A> { | |
private Option() { | |
} | |
public abstract A get(); | |
public abstract A getOrElse(A defaultResult); | |
public abstract Iterator<A> iterator(); | |
public abstract boolean match(Option<A> other); | |
@SuppressWarnings("unchecked") | |
public static <A> Option<A> Option(final A a) { | |
return a == null ? (Option<A>) None : Some(a); | |
} | |
public static <A> Option<A> Some(final A a) { | |
return new _Some<A>(a); | |
} | |
@SuppressWarnings("unchecked") | |
public static <A> Option<A> None() { | |
return (Option<A>) None; | |
} | |
@SuppressWarnings("rawtypes") | |
public static final Option None = new _None(); | |
public abstract void foreach(VoidFunction<A> i); | |
public abstract <B> Option<B> map(Function<A,B> i); | |
public abstract boolean isDefined(); | |
private static final class _Some<A> extends Option<A> { | |
private final A value; | |
private _Some(A a) { | |
if (a == null) throw new IllegalArgumentException("argument to Some may not be null"); | |
this.value = a; | |
} | |
@Override | |
public A get() { | |
return this.value; | |
} | |
@Override | |
public A getOrElse(final A ignored) { | |
return this.value; | |
} | |
@Override | |
public Iterator<A> iterator() { | |
return Collections.<A>singleton(this.value).iterator(); | |
} | |
@Override | |
public boolean match(final Option<A> other) { | |
return other == None ? false : value.equals(((_Some<A>) other).value); | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public boolean equals(final Object obj) { | |
return obj instanceof Option ? match((Option<A>) obj) : false; | |
} | |
@Override | |
public int hashCode() { | |
return this.value.hashCode(); | |
} | |
@Override | |
public String toString() { | |
return "Some(" + this.value + ")"; | |
} | |
public void foreach(VoidFunction<A> i){ | |
i.apply(value); | |
} | |
public <B> Option<B> map(Function<A,B> i){ | |
return Option.Option(i.apply(value)); | |
} | |
@Override | |
public boolean isDefined() { | |
return true; | |
} | |
} | |
private static final class _None<A> extends Option<A> { | |
private _None() { | |
} | |
@Override | |
public A get() { | |
throw new NoSuchElementException("None.get() called"); | |
} | |
@Override | |
public A getOrElse(final A result) { | |
return result; | |
} | |
@Override | |
public Iterator<A> iterator() { | |
return Collections.<A>emptyList().iterator(); | |
} | |
@Override | |
@SuppressWarnings("rawtypes") | |
public boolean match(final Option other) { | |
return other == None; | |
} | |
@Override | |
public boolean equals(final Object obj) { | |
return obj == this; | |
} | |
@Override | |
public int hashCode() { | |
return 0; | |
} | |
@Override | |
public String toString() { | |
return "None"; | |
} | |
public void foreach(VoidFunction i){ | |
} | |
public <B> Option<B> map(Function<A,B> i){ | |
return Option.None; | |
} | |
@Override | |
public boolean isDefined() { | |
return false; | |
} | |
} | |
} |
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 com.hivefive.ciphernod.scalaz; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java8.util.function.Function; | |
/** | |
* Created by kinshuk.bairagi on 20/02/16. | |
*/ | |
public abstract class Try<A> implements Iterable<A> { | |
private Try() { | |
} | |
public abstract A get() throws Throwable; | |
public abstract Iterator<A> iterator(); | |
public abstract boolean match(Try<A> other); | |
public abstract void foreach(VoidFunction<A> i); | |
public abstract <B> Try<B> map(Function<A, B> i); | |
public abstract boolean isSuccess(); | |
public static <A> Try<A> Success(final A a) { | |
return new Success<A>(a); | |
} | |
public static <A> Try<A> Failure(Throwable throwable) { | |
return new Failure<A>(throwable); | |
} | |
private static final class Success<A> extends Try<A> { | |
private final A value; | |
private Success(A a) { | |
this.value = a; | |
} | |
@Override | |
public A get() { | |
return this.value; | |
} | |
@Override | |
public Iterator<A> iterator() { | |
return Collections.<A>singleton(this.value).iterator(); | |
} | |
@Override | |
public boolean match(final Try<A> other) { | |
return !(other instanceof Failure) && value.equals(((Success<A>) other).value); | |
} | |
@Override | |
@SuppressWarnings("unchecked") | |
public boolean equals(final Object obj) { | |
return obj instanceof Try && match((Try<A>) obj); | |
} | |
@Override | |
public int hashCode() { | |
return this.value.hashCode(); | |
} | |
@Override | |
public String toString() { | |
return "Some(" + this.value + ")"; | |
} | |
public void foreach(VoidFunction<A> i){ | |
i.apply(value); | |
} | |
public <B> Try<B> map(Function<A,B> i){ | |
return Try.Success(i.apply(value)); | |
} | |
@Override | |
public boolean isSuccess() { | |
return true; | |
} | |
} | |
private static final class Failure<A> extends Try<A> { | |
Throwable error; | |
private Failure(Throwable error) { | |
this.error = error; | |
} | |
@Override | |
public A get() throws Throwable { | |
throw error; | |
} | |
@Override | |
public Iterator<A> iterator() { | |
return Collections.<A>emptyList().iterator(); | |
} | |
@Override | |
@SuppressWarnings("rawtypes") | |
public boolean match(final Try other) { | |
return other instanceof Failure; | |
} | |
@Override | |
public boolean equals( Object other) { | |
return !(other instanceof Success) && error.equals(((Failure<A>) other).error); | |
} | |
@Override | |
public int hashCode() { | |
return error.hashCode(); | |
} | |
@Override | |
public String toString() { | |
return "None"; | |
} | |
public void foreach(VoidFunction i){ | |
} | |
public <B> Try<B> map(Function<A,B> i){ | |
return Try.Failure(error); | |
} | |
@Override | |
public boolean isSuccess() { | |
return false; | |
} | |
} | |
} | |
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 com.hivefive.ciphernod.scalaz; | |
/** | |
* Created by kinshuk.bairagi on 21/02/16. | |
*/ | |
public interface VoidFunction<T> { | |
void apply(T var1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment