Skip to content

Commit

Permalink
Add when(Object o) method matching interface
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlcox committed Jul 4, 2015
1 parent 64a622c commit f60952e
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2015 John Leacox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.leacox.motif.cases;

import com.leacox.motif.generate.CasesGenerator;
import com.leacox.motif.generate.Match1MethodSpec;
import com.leacox.motif.tuple.Tuple1;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeVariableName;

import java.io.IOException;

/**
* @author John Leacox
*/
final class PrimitiveCasesGenerator{
private PrimitiveCasesGenerator() {
}

public static void main(String[] args) {
TypeName A = TypeVariableName.get("A");
TypeName t = ParameterizedTypeName.get(ClassName.get(Tuple1.class), A);

Match1MethodSpec byteMatch = Match1MethodSpec.builder()
.withName("aByte").withSummaryJavadoc("Matches a byte.\n")
.withMatchExtractor(PrimitiveFieldExtractor.class, Byte.class).withParamA(TypeName.get(byte.class), "b").build();

JavaFile primitiveCasesFile = CasesGenerator.newBuilder(
"com.leacox.motif.cases", "PrimitiveCases", t)
.addFileComment(Copyright.COPYRIGHT_NOTICE)
.addJavadoc("Motif cases for matching a primitive types.\n")
.addMatch1Method(byteMatch)
.build().generate();

try {
primitiveCasesFile.writeTo(System.out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public final class PrimitiveCases {
private PrimitiveCases() {
}

// TODO: Are these cases actually useful or is the when(Object o) method good enough?
public static DecomposableMatchBuilder0<Byte> caseByte(byte b) {
List<Matcher<Object>> matchers = new ArrayList<>();
matchers.add(eq(b));
Expand Down
14 changes: 14 additions & 0 deletions motif/src/main/java/com/leacox/motif/matching/FluentMatching.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@
*/
package com.leacox.motif.matching;

import com.leacox.motif.MatchesExact;
import com.leacox.motif.extract.DecomposableMatchBuilder0;
import com.leacox.motif.extract.DecomposableMatchBuilder1;
import com.leacox.motif.extract.DecomposableMatchBuilder2;
import com.leacox.motif.extract.DecomposableMatchBuilder3;
import com.leacox.motif.extract.FieldExtractor;
import com.leacox.motif.extract.matchers.ArgumentMatchers;
import com.leacox.motif.extract.matchers.Matcher;
import com.leacox.motif.extract.util.Lists;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* @author John Leacox
Expand All @@ -30,6 +39,11 @@ 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(
DecomposableMatchBuilder0<U> decomposableMatchBuilder) {
return new InitialMatching0<>(decomposableMatchBuilder.build(), value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.leacox.motif.extract.DecomposableMatchBuilder1;
import com.leacox.motif.extract.DecomposableMatchBuilder2;
import com.leacox.motif.extract.DecomposableMatchBuilder3;
import com.leacox.motif.extract.matchers.ArgumentMatchers;
import com.leacox.motif.extract.matchers.Matcher;
import com.leacox.motif.extract.util.Lists;
import com.leacox.motif.function.Consumer0;

import java.util.ArrayList;
Expand All @@ -37,10 +40,15 @@ public final class FluentMatchingC<T> {
this.value = value;
}

public void addPattern(ConsumablePattern<T> pattern) {
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(
DecomposableMatchBuilder0<U> decomposableMatchBuilder) {
return new OngoingMatchingC0<>(this, decomposableMatchBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.leacox.motif.extract.DecomposableMatchBuilder1;
import com.leacox.motif.extract.DecomposableMatchBuilder2;
import com.leacox.motif.extract.DecomposableMatchBuilder3;
import com.leacox.motif.extract.matchers.ArgumentMatchers;
import com.leacox.motif.extract.matchers.Matcher;
import com.leacox.motif.extract.util.Lists;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,10 +40,15 @@ public final class FluentMatchingR<T, R> {
this.value = value;
}

public void addPattern(Pattern<T, R> pattern) {
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(
DecomposableMatchBuilder0<U> decomposableMatchBuilder) {
return new OngoingMatchingR0<>(this, decomposableMatchBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 John Leacox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.leacox.motif.matching;

import com.leacox.motif.extract.FieldExtractor;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* @author John Leacox
*/
final class IdentityFieldExtractor<T> implements FieldExtractor<T> {
@Override
public Optional<List<Object>> unapply(T value) {
return Optional.of(Collections.singletonList(value));
}

@Override
public Class<?> getExtractorClass() {
return Object.class;
}
}
20 changes: 17 additions & 3 deletions motif/src/test/java/com/leacox/motif/cases/NestedCasesSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import static com.leacox.motif.cases.ListConsCases.headTail;
import static com.leacox.motif.cases.OptionalCases.some;
import static com.leacox.motif.cases.Tuple2Cases.tuple2;
import static com.leacox.motif.cases.TypeOfCases.typeOf;

import com.leacox.motif.extract.util.Lists;
import com.leacox.motif.tuple.Tuple;
import com.leacox.motif.tuple.Tuple2;

import com.insightfullogic.lambdabehave.JunitSuiteRunner;
Expand Down Expand Up @@ -55,7 +55,8 @@ public class NestedCasesSpec {

it.should(
"match nested Optionals", expect -> {
Optional<Optional<Optional<String>>> opt = Optional.of(Optional.of(Optional.of("a")));
Optional<Optional<Optional<String>>> opt =
Optional.of(Optional.of(Optional.of("a")));

String result = match(opt)
.when(some(some(some(any())))).get(a -> a)
Expand Down Expand Up @@ -92,7 +93,8 @@ public class NestedCasesSpec {

it.should(
"match List of Tuple2", expect -> {
List<Tuple2<String, String>> list = Lists.of(Tuple2.of("a", "b"), Tuple2.of("c", "d"));
List<Tuple2<String, String>> list =
Lists.of(Tuple2.of("a", "b"), Tuple2.of("c", "d"));

String result = match(list)
.when(headTail(tuple2(any(), any()), any())).get(
Expand All @@ -113,6 +115,18 @@ public class NestedCasesSpec {

expect.that(result).is("(a, b, c)");
});

it.should(
"match nested typeOf", expect -> {
Tuple2<Object, Object> tuple2 = Tuple2.of((Object) 2, (Object) 4);

String result = match(tuple2)
.when(tuple2(typeOf(Integer.class), typeOf(Integer.class))).get(
(a, b) -> String.valueOf(a))
.getMatch();

expect.that(result).is("2");
});
});
}
}
41 changes: 21 additions & 20 deletions motif/src/test/java/com/leacox/motif/cases/PrimitiveCasesSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.leacox.motif.cases;

import static com.insightfullogic.lambdabehave.Suite.describe;
import static com.leacox.motif.MatchesExact.eq;
import static com.leacox.motif.Motif.match;
import static com.leacox.motif.cases.PrimitiveCases.caseBoolean;
import static com.leacox.motif.cases.PrimitiveCases.caseByte;
Expand Down Expand Up @@ -45,8 +46,8 @@ public class PrimitiveCasesSpec {

// No byte literals in Java so you have to cast
String result = match(b)
.when(caseByte((byte) 4)).get(() -> "Nope")
.when(caseByte((byte) 27)).get(() -> "27")
.when((byte) 4).get(() -> "Nope")
.when((byte) 27).get(() -> "27")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -59,8 +60,8 @@ public class PrimitiveCasesSpec {

// No short literals in Java so you have to cast
String result = match(s)
.when(caseShort((short) 4)).get(() -> "Nope")
.when(caseShort((short) 382)).get(() -> "382")
.when((short) 4).get(() -> "Nope")
.when((short) 382).get(() -> "382")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -72,8 +73,8 @@ public class PrimitiveCasesSpec {
int i = 2875283;

String result = match(i)
.when(caseInt(4)).get(() -> "Nope")
.when(caseInt(2875283)).get(() -> "2875283")
.when(4).get(() -> "Nope")
.when(2875283).get(() -> "2875283")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -85,8 +86,8 @@ public class PrimitiveCasesSpec {
long l = 82874927472927l;

String result = match(l)
.when(caseLong(4l)).get(() -> "Nope")
.when(caseLong(82874927472927l)).get(() -> "82874927472927")
.when(4l).get(() -> "Nope")
.when(82874927472927l).get(() -> "82874927472927")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -98,8 +99,8 @@ public class PrimitiveCasesSpec {
float f = 382.84782f;

String result = match(f)
.when(caseFloat(4f)).get(() -> "Nope")
.when(caseFloat(382.84782f)).get(() -> "Yep")
.when(4f).get(() -> "Nope")
.when(382.84782f).get(() -> "Yep")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -111,8 +112,8 @@ public class PrimitiveCasesSpec {
double d = 89378393384832.3847849394;

String result = match(d)
.when(caseDouble(4d)).get(() -> "Nope")
.when(caseDouble(89378393384832.3847849394)).get(() -> "Yep")
.when(4d).get(() -> "Nope")
.when(89378393384832.3847849394).get(() -> "Yep")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -124,8 +125,8 @@ public class PrimitiveCasesSpec {
char c = 'T';

String result = match(c)
.when(caseChar('t')).get(() -> "Nope")
.when(caseChar('T')).get(() -> "T")
.when('t').get(() -> "Nope")
.when('T').get(() -> "T")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -137,8 +138,8 @@ public class PrimitiveCasesSpec {
String s = "Hello World";

String result = match(s)
.when(caseString("Goodbye World")).get(() -> "Nope")
.when(caseString("Hello World")).get(() -> "Hello World")
.when("Goodbye World").get(() -> "Nope")
.when("Hello World").get(() -> "Hello World")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -150,8 +151,8 @@ public class PrimitiveCasesSpec {
boolean t = true;

String result = match(t)
.when(caseBoolean(false)).get(() -> "Nope")
.when(caseBoolean(true)).get(() -> "true")
.when(false).get(() -> "Nope")
.when(true).get(() -> "true")
.orElse(x -> "orElse")
.getMatch();

Expand All @@ -164,8 +165,8 @@ public class PrimitiveCasesSpec {

// No short literals in Java so you have to cast
String result = match(f)
.when(caseBoolean(true)).get(() -> "Nope")
.when(caseBoolean(false)).get(() -> "false")
.when(true).get(() -> "Nope")
.when(false).get(() -> "false")
.orElse(x -> "orElse")
.getMatch();

Expand Down

0 comments on commit f60952e

Please sign in to comment.