Skip to content

Commit

Permalink
Update primitive matching tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlcox committed Jul 5, 2015
1 parent f60952e commit a1ec860
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 48 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ A pattern matching library for Java 8. Motif provides scala-like pattern matchin
IntStream.range(0, 101).forEach(
n -> System.out.println(
match(Tuple2.of(n % 3, n % 5))
.when(tuple2(0, 0)).get(() -> "FizzBuzz")
.when(tuple2(0, any())).get(y -> "Fizz")
.when(tuple2(any(), 0)).get(x -> "Buzz")
.when(tuple2(eq(0), eq(0))).get(() -> "FizzBuzz")
.when(tuple2(eq(0), any())).get(y -> "Fizz")
.when(tuple2(any(), eq(0))).get(x -> "Buzz")
.orElse(String.valueOf(n))
.getMatch()
)
Expand All @@ -35,8 +35,8 @@ match(personOpt)
```java
public long factorial(long i) {
return match(i)
.when(caseLong(0)).get(() -> 1l)
.when(caseLong(any())).get(x -> x * factorial(x - 1))
.when(eq(0)).get(() -> 1l)
.when(any()).get(x -> x * factorial(x - 1))
.getMatch();
}
```
Expand All @@ -46,8 +46,8 @@ public long factorial(long i) {
```java
Optional<Tuple2<String, String>> opt = Optional.of(Tuple2.of("first", "second"));
match(opt)
.when(some(tuple2("third", any()))).then(b -> doStuff(b))
.when(some(tuple2(any(), "second"))).then(a -> doStuff(a))
.when(some(tuple2(eq("third"), any()))).then(b -> doStuff(b))
.when(some(tuple2(any(), eq("second")))).then(a -> doStuff(a))
.when(none()).then(() -> System.out.println("Tuple not found"))
.doMatch();
```
Expand All @@ -58,7 +58,7 @@ match(opt)
List<String> list = Arrays.asList("a", "b", "c", "d");
match(list)
.when(nil()).then(() -> System.out.println("Empty List"))
.when(headNil("b")).then(() -> System.out.println("Singleton List of 'b'"))
.when(headNil(eq("b"))).then(() -> System.out.println("Singleton List of 'b'"))
.when(headNil(any())).then(head -> System.out.println("Singleton List of " + head))
.when(headTail(any(), any())).then(
(head, tail) -> System.out.println("head: " + head + " Remaining: " + tail))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Benchmark {
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(FactorialBenchmark.class.getSimpleName())
//.include(FizzBuzzBenchmark.class.getSimpleName())
.include(FizzBuzzBenchmark.class.getSimpleName())
.forks(1)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.leacox.motif.benchmarks;

import static com.leacox.motif.MatchesExact.eq;
import static com.leacox.motif.Motif.match;
import static com.leacox.motif.cases.PrimitiveCases.caseLong;
import static com.leacox.motif.MatchesAny.any;
Expand Down Expand Up @@ -59,8 +60,8 @@ public long factorialPatternMatching() {

private long factMatching(long i) {
return match(i)
.when(caseLong(0)).get(() -> 1l)
.when(caseLong(any())).get(x -> x * factMatching(x - 1))
.when(eq(0l)).get(() -> 1l)
.when(any()).get(x -> x * factMatching(x - 1))
.getMatch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static DecomposableMatchBuilder0<Long> caseLong(long l) {
return new DecomposableMatchBuilder0<>(matchers, new PrimitiveFieldExtractor<>(Long.class));
}

public static DecomposableMatchBuilder1<Long, Long> caseLong(MatchesAny b) {
public static DecomposableMatchBuilder1<Long, Long> caseLong(MatchesAny<Long> l) {
List<Matcher<Object>> matchers = new ArrayList<>();
matchers.add(any());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
public interface Extractor0<T> {
boolean unapply(T t);

Class getExtractorClass();
Class<?> getExtractorClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
public interface Extractor1<T, A> {
Optional<A> unapply(T t);

Class getExtractorClass();
Class<?> getExtractorClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
public interface Extractor2<T, A, B> {
Optional<Tuple2<A, B>> unapply(T t);

Class getExtractorClass();
Class<?> getExtractorClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
public interface Extractor3<T, A, B, C> {
Optional<Tuple3<A, B, C>> unapply(T t);

Class getExtractorClass();
Class<?> getExtractorClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.leacox.motif.matching;

import com.leacox.motif.MatchesAny;
import com.leacox.motif.MatchesExact;
import com.leacox.motif.extract.DecomposableMatchBuilder0;
import com.leacox.motif.extract.DecomposableMatchBuilder1;
Expand All @@ -39,9 +40,14 @@ public FluentMatching(T value) {
this.value = value;
}

public <U extends T> InitialMatching0<T, U> when(Object o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.eq(o));
return new InitialMatching0<>(new DecomposableMatchBuilder0<>(matchers, new IdentityFieldExtractor()).build(), value);
public <U extends T> InitialMatching0<T, U> when(MatchesExact<U> o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.eq(o.t));
return new InitialMatching0<>(new DecomposableMatchBuilder0<>(matchers, new IdentityFieldExtractor<U>()).build(), value);
}

public <U extends T> InitialMatching1<T, U, U> when(MatchesAny<U> o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.any());
return new InitialMatching1<>(new DecomposableMatchBuilder1<U, U>(matchers, 0, new IdentityFieldExtractor<>()).build(), value);
}

public <U extends T> InitialMatching0<T, U> when(
Expand Down
13 changes: 10 additions & 3 deletions motif/src/main/java/com/leacox/motif/matching/FluentMatchingC.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.leacox.motif.matching;

import com.leacox.motif.MatchException;
import com.leacox.motif.MatchesAny;
import com.leacox.motif.MatchesExact;
import com.leacox.motif.extract.DecomposableMatchBuilder0;
import com.leacox.motif.extract.DecomposableMatchBuilder1;
import com.leacox.motif.extract.DecomposableMatchBuilder2;
Expand Down Expand Up @@ -44,9 +46,14 @@ void addPattern(ConsumablePattern<T> pattern) {
patterns.add(pattern);
}

public <U extends T> OngoingMatchingC0<T, U> when(Object o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.eq(o));
return new OngoingMatchingC0<>(this, new DecomposableMatchBuilder0<>(matchers, new IdentityFieldExtractor()).build());
public <U extends T> OngoingMatchingC0<T, U> when(MatchesExact<U> o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.eq(o.t));
return new OngoingMatchingC0<>(this, new DecomposableMatchBuilder0<>(matchers, new IdentityFieldExtractor<U>()).build());
}

public <U extends T> OngoingMatchingC1<T, U, U> when(MatchesAny<U> o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.any());
return new OngoingMatchingC1<>(this, new DecomposableMatchBuilder1<U, U>(matchers, 0, new IdentityFieldExtractor<>()).build());
}

public <U extends T> OngoingMatchingC0<T, U> when(
Expand Down
13 changes: 10 additions & 3 deletions motif/src/main/java/com/leacox/motif/matching/FluentMatchingR.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.leacox.motif.matching;

import com.leacox.motif.MatchException;
import com.leacox.motif.MatchesAny;
import com.leacox.motif.MatchesExact;
import com.leacox.motif.extract.DecomposableMatchBuilder0;
import com.leacox.motif.extract.DecomposableMatchBuilder1;
import com.leacox.motif.extract.DecomposableMatchBuilder2;
Expand Down Expand Up @@ -44,9 +46,14 @@ void addPattern(Pattern<T, R> pattern) {
patterns.add(pattern);
}

public <U extends T> OngoingMatchingR0<T, U, R> when(Object o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.eq(o));
return new OngoingMatchingR0<>(this, new DecomposableMatchBuilder0<>(matchers, new IdentityFieldExtractor()).build());
public <U extends T> OngoingMatchingR0<T, U, R> when(MatchesExact<U> o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.eq(o.t));
return new OngoingMatchingR0<>(this, new DecomposableMatchBuilder0<>(matchers, new IdentityFieldExtractor<U>()).build());
}

public <U extends T> OngoingMatchingR1<T, U, U, R> when(MatchesAny<U> o) {
List<Matcher<Object>> matchers = Lists.of(ArgumentMatchers.any());
return new OngoingMatchingR1<>(this, new DecomposableMatchBuilder1<U, U>(matchers, 0, new IdentityFieldExtractor<>()).build());
}

public <U extends T> OngoingMatchingR0<T, U, R> when(
Expand Down
Loading

0 comments on commit a1ec860

Please sign in to comment.