Created
October 21, 2015 08:31
-
-
Save fsarradin/bea95af73651a57c1112 to your computer and use it in GitHub Desktop.
Java 8 eXperiment
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 me.mixin; | |
import java.util.Map; | |
import java.util.WeakHashMap; | |
import static java.util.Collections.synchronizedMap; | |
public class MixinMain { | |
interface WithName { | |
Map<WithName, String> names = | |
synchronizedMap(new WeakHashMap<>()); | |
default String getName() { | |
return names.get(this); | |
} | |
default void setName(String name) { | |
names.put(this, name); | |
} | |
} | |
interface Mixture extends Runnable, WithName {} | |
public static void main(String[] args) throws Exception { | |
Mixture mixture1 = () -> System.out.println("hello !"); | |
Mixture mixture2 = () -> System.out.println("hello !"); | |
mixture1.setName("mixture1"); | |
mixture2.setName("mixture2"); | |
System.out.println(mixture1); | |
System.out.println(mixture2); | |
System.out.println(mixture1.getName()); | |
System.out.println(mixture2.getName()); | |
} | |
} |
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 me.pattern; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
public class PatternMain { | |
public static void main(String[] args) { | |
List<?> values = Arrays.asList("hello", 1, 1.5); | |
values.stream() | |
.map( | |
match() | |
.whenIs("hello").then(__ -> "hello world") | |
.whenTypeIs(Integer.class).then(i -> "int: " + i) | |
.otherwise(__ -> "what else!") | |
) | |
.forEach(System.out::println); | |
} | |
static <T> Matcher<T> match() { | |
return new Matcher<>(); | |
} | |
static class Matcher<T> { | |
<R> When<R> when(Predicate<? super T> predicate) { | |
return new When<>(predicate); | |
} | |
<R> When<R> whenIs(Object pattern) { | |
return when(pattern::equals); | |
} | |
class When<R> { | |
final Predicate<? super T> predicate; | |
final List<Case<R>> cases; | |
When(Predicate<? super T> predicate) { | |
this(predicate, Collections.emptyList()); | |
} | |
When(Predicate<? super T> predicate, List<Case<R>> cases) { | |
this.predicate = predicate; | |
this.cases = cases; | |
} | |
Then then(Function<T, ? extends R> function) { | |
List<Case<R>> cases = new ArrayList<>(); | |
cases.addAll(this.cases); | |
cases.add(new Case<>(When.this.predicate, function)); | |
return new Then(cases); | |
} | |
class Then implements Function<T, R> { | |
final List<Case<R>> cases; | |
Then(List<Case<R>> cases) { | |
this.cases = cases; | |
} | |
@Override | |
public R apply(T value) { | |
return cases.stream() | |
.filter(c -> c.isApplicable(value)) | |
.findFirst() | |
.map(c -> c.apply(value)) | |
.orElseThrow(IllegalArgumentException::new); | |
} | |
When<R> when(Predicate<? super T> predicate) { | |
return new When<R>(predicate, cases); | |
} | |
When<R> whenIs(Object pattern) { | |
return when(pattern::equals); | |
} | |
When<R> whenTypeIs(Class<?> cls) { | |
return when(v -> cls.isAssignableFrom(v.getClass())); | |
} | |
Otherwise<R> otherwise(Function<? super T, ? extends R> function) { | |
return new Otherwise<R>(function, cases); | |
} | |
} | |
} | |
class Otherwise<R> implements Function<T, R> { | |
final Function<? super T, ? extends R> function; | |
final List<Case<R>> cases; | |
Otherwise(Function<? super T, ? extends R> function, List<Case<R>> cases) { | |
this.function = function; | |
this.cases = cases; | |
} | |
@Override | |
public R apply(T value) { | |
return cases.stream() | |
.filter(c -> c.isApplicable(value)) | |
.findFirst() | |
.map(c -> c.apply(value)) | |
.orElseGet(() -> function.apply(value)); | |
} | |
} | |
class Case<R> implements Function<Object, R> { | |
final Predicate<Object> predicate; | |
final Function<Object, ? extends R> function; | |
@SuppressWarnings("unchecked") | |
Case(Predicate<? super T> predicate, Function<? super T, ? extends R> function) { | |
this.predicate = (Predicate<Object>) predicate; | |
this.function = (Function<Object, ? extends R>) function; | |
} | |
boolean isApplicable(Object object) { | |
return predicate.test(object); | |
} | |
@Override | |
public R apply(Object object) { | |
return function.apply(object); | |
} | |
} | |
} | |
} |
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 me.validation; | |
import java.util.Optional; | |
public class ValidationMain { | |
public static void main(String[] args) throws Exception { | |
Validation<Exception, Integer> result1 = | |
new Validation.Success<>(42); | |
Validation<Exception, Integer> result2 = | |
new Validation.Failure<>(new IllegalArgumentException()); | |
System.out.println(result1.toOptional()); | |
System.out.println(result2.swap().toOptional()); | |
} | |
} | |
abstract class Validation<E, T> { | |
private Validation() {} | |
public abstract Optional<T> toOptional(); | |
public abstract Validation<T, E> swap(); | |
static class Success<E, T> extends Validation<E, T> { | |
private final T value; | |
public Success(T value) { | |
this.value = value; | |
} | |
@Override | |
public Optional<T> toOptional() { | |
return Optional.of(value); | |
} | |
@Override | |
public Failure<T, E> swap() { | |
return new Failure<>(value); | |
} | |
} | |
static class Failure<E, T> extends Validation<E, T> { | |
private final E error; | |
public Failure(E error) { | |
this.error = error; | |
} | |
@Override | |
public Optional<T> toOptional() { | |
return Optional.empty(); | |
} | |
@Override | |
public Success<T, E> swap() { | |
return new Success<>(error); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment