Last active
July 9, 2018 05:44
-
-
Save sungjk/7a55b2fff8673d92c4266378d950bccc to your computer and use it in GitHub Desktop.
functional programming style helpers for 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
public class Functional { | |
public interface Function3<A,B,C,R> { | |
R apply(A a, B b, C c) throws Throwable; | |
} | |
public static class Either<L, R> { | |
final public Optional<L> left; | |
final public Optional<R> right; | |
private Either(Optional<L> left, Optional<R> right) { | |
this.left = left; | |
this.right = right; | |
} | |
public static <L, R> Either<L, R> Left(L value) { | |
return new Either<L, R>(Optional.of(value), Optional.<R>empty()); | |
} | |
public static <L, R> Either<L, R> Right(R value) { | |
return new Either<L, R>(Optional.<L>empty(), Optional.of(value)); | |
} | |
@Override | |
public String toString() { | |
return "Either(left: " + this.left + ", right: " + this.right + ")"; | |
} | |
} | |
public static class Tuple<A, B> { | |
final public A _1; | |
final public B _2; | |
public Tuple(A _1, B _2) { | |
this._1 = _1; | |
this._2 = _2; | |
} | |
@Override | |
public String toString() { | |
return "Tuple2(_1: " + _1 + ", _2: " + _2 + ")"; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((_1 == null) ? 0 : _1.hashCode()); | |
result = prime * result + ((_2 == null) ? 0 : _2.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) return true; | |
if (obj == null) return false; | |
if (!(obj instanceof Tuple)) return false; | |
Tuple other = (Tuple) obj; | |
if (_1 == null) { if (other._1 != null) return false; } | |
else if (!_1.equals(other._1)) return false; | |
if (_2 == null) { if (other._2 != null) return false; } | |
else if (!_2.equals(other._2)) return false; | |
return true; | |
} | |
} | |
public static <A, B> Tuple<A, B> Tuple(A a, B b) { | |
return new Tuple<A, B>(a, b); | |
} | |
public static class Tuple3<A, B, C> { | |
final public A _1; | |
final public B _2; | |
final public C _3; | |
public Tuple3(A _1, B _2, C _3) { | |
this._1 = _1; | |
this._2 = _2; | |
this._3 = _3; | |
} | |
@Override | |
public String toString() { | |
return "Tuple3(_1: " + _1 + ", _2: " + _2 + ", _3:" + _3 + ")"; | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((_1 == null) ? 0 : _1.hashCode()); | |
result = prime * result + ((_2 == null) ? 0 : _2.hashCode()); | |
result = prime * result + ((_3 == null) ? 0 : _3.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) return true; | |
if (obj == null) return false; | |
if (!(obj instanceof Tuple3)) return false; | |
Tuple3 other = (Tuple3) obj; | |
if (_1 == null) { if (other._1 != null) return false; } | |
else if (!_1.equals(other._1)) return false; | |
if (_2 == null) { if (other._2 != null) return false; } | |
else if (!_2.equals(other._2)) return false; | |
if (_3 == null) { if (other._3 != null) return false; } | |
else if (!_3.equals(other._3)) return false; | |
return true; | |
} | |
} | |
public static <A, B, C> Tuple3<A, B, C> Tuple3(A a, B b, C c) { | |
return new Tuple3<A, B, C>(a, b, c); | |
} | |
public static class LazySupplier<T> implements Supplier<T> { | |
private T value; | |
private final Supplier<T> instantiator; | |
private LazySupplier(Supplier<T> instantiator) { | |
this.instantiator = instantiator; | |
} | |
@Override | |
public T get() { | |
if (this.value == null) { | |
this.value = instantiator.get(); | |
} | |
return this.value; | |
} | |
public static <T> Supplier<T> lazy(Supplier<T> creator) { | |
return new LazySupplier<>(creator); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment