Skip to content

Commit

Permalink
updated readme's example
Browse files Browse the repository at this point in the history
  • Loading branch information
lprakashv committed Jun 12, 2020
1 parent 8b133db commit 6f6af50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
# patternmatcher4j "Switch-Case on Steroids"
An FP style pattern matcher library for Java. Pattern matching is one of the the single most popular concept in functional programming. This is inpired from [Scala's pattern matching](https://docs.scala-lang.org/tour/pattern-matching.html).

**[See Javadoc](https://lprakashv.github.io/patternmatcher4j/)**

### Dependency (replace `{version}` with the latest version)

Maven
Expand All @@ -24,6 +22,8 @@ Gradle
implementation 'io.github.lprakashv:patternmatcher4j:{version}'
```

**[See Javadoc](https://lprakashv.github.io/patternmatcher4j/)**

## Concept
This is just like switch-case block, but you can do much more than just matching primitive values and enums.

Expand Down Expand Up @@ -52,8 +52,8 @@ String stringFromObjectPatternMatching =
Matcher
.<Object, String>matchFor((Object) person)
.matchCase(
Field.with("name", name -> ((String) name).toLowerCase().equals("lalit")),
Field.with("age", age -> (Integer) age < 60),
Field.with("name", name -> name != null && ((String) name).toLowerCase().equals("lalit")),
Field.with("age", age -> age != null && (Integer) age < 60),
Field.withValue("eligible", true)
)
.action(p -> "Young Lalit found")
Expand All @@ -67,7 +67,7 @@ String stringFromObjectPatternMatching =
.action(p -> "Person with String extra found with extra value=" + ((Person) p).extra)
.matchCase(NonPerson.class)
.action(p -> "This is not a person")
.matchCase(p -> ((Person) p).age > 100)
.matchCase(p -> p != null && ((Person) p).age > 100)
.action(p -> "Very old person")
.getOrElse("Unknown");
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private Matcher<Object, String> createPatternMatchForPerson(Object person) {
.<Object, String>matchFor((Object) person)
.matchCase( // any number of field matches
// this is a field with predicate match with Function<Object, Boolean> passed
Field.with("name", name -> ((String) name).toLowerCase().equals("lalit")),
Field.with("name", name -> name != null && ((String) name).toLowerCase().equals("lalit")),
Field.with("age", age -> age != null && (Integer) age < 60),
// this is field with value match with .withValue method
Field.withValue("eligible", true)
Expand All @@ -41,7 +41,7 @@ private Matcher<Object, String> createPatternMatchForPerson(Object person) {
// this is type match
.matchCase(NonPerson.class)
.action(p -> "This is not a person")
.matchCase(p -> ((Person) p).age > 100)
.matchCase(p -> p != null && ((Person) p).age > 100)
.action(p -> "Very old person");
// we can use get() which will return Optional<R>
}
Expand Down

0 comments on commit 6f6af50

Please sign in to comment.