Javaslang project (#927)
* made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article * added project for regex * changed regex project from own model to core-java * added project for routing in play * made changes to regex project * refactored code for REST API with Play project * refactored student store indexing to zero base * added unit tests, removed bad names * added NIO Selector project under core-java module * requested changes made * added project for nio2 * standardized exception based tests * fixed exception based tests * removed redundant files * added network interface project * used UUID other than timestamps * fixed network interface tests * removed filetest change * made changes to NIO2 FileTest names * added project for asyncronous channel apis * added project for NIO2 advanced filesystems APIS * merge conflicts * merged changes to asyncfiletest with future get API * removed while loops from async client and server * added project for java8 optional * fixed merge conflicts in spring-core * fixed optional * fixed optional * fixed asyncechotest * shifted optional to own package * made additional tests to demo filter API * added code for Map * added javaslang project * javaslang project * added pattern matcher code
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
c5b67b5620
commit
babd9da492
@@ -0,0 +1,41 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>Javaslang</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>Javaslang</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.javaslang</groupId>
|
||||
<artifactId>javaslang</artifactId>
|
||||
<version>2.1.0-alpha</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.javaslang;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Person() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [name=" + name + ", age=" + age + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.javaslang;
|
||||
|
||||
import javaslang.collection.List;
|
||||
import javaslang.control.Validation;
|
||||
|
||||
class PersonValidator {
|
||||
String NAME_ERR = "Invalid characters in name: ";
|
||||
String AGE_ERR = "Age must be at least 0";
|
||||
|
||||
public Validation<List<String>, Person> validatePerson(String name, int age) {
|
||||
return Validation.combine(validateName(name), validateAge(age)).ap(Person::new);
|
||||
}
|
||||
|
||||
private Validation<String, String> validateName(String name) {
|
||||
String invalidChars = name.replaceAll("[a-zA-Z ]", "");
|
||||
return invalidChars.isEmpty() ? Validation.valid(name) : Validation.invalid(NAME_ERR + invalidChars);
|
||||
}
|
||||
|
||||
private Validation<String, Integer> validateAge(int age) {
|
||||
return age < 0 ? Validation.invalid(AGE_ERR) : Validation.valid(age);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,114 @@
|
||||
package com.baeldung.javaslang;
|
||||
|
||||
import static javaslang.API.$;
|
||||
import static javaslang.API.Case;
|
||||
import static javaslang.API.Match;
|
||||
import static javaslang.API.run;
|
||||
import static javaslang.Predicates.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javaslang.MatchError;
|
||||
import javaslang.control.Option;
|
||||
|
||||
public class PatternMatchingTest {
|
||||
@Test
|
||||
public void whenMatchesDefault_thenCorrect() {
|
||||
int input = 5;
|
||||
String output = Match(input).of(Case($(1), "one"), Case($(2), "two"), Case($(3), "three"), Case($(), "unknown"));
|
||||
|
||||
assertEquals("unknown", output);
|
||||
}
|
||||
|
||||
@Test(expected = MatchError.class)
|
||||
public void givenNoMatchAndNoDefault_whenThrows_thenCorrect() {
|
||||
int input = 5;
|
||||
Match(input).of(Case($(1), "one"), Case($(2), "two"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchWorksWithOption_thenCorrect() {
|
||||
int i = 10;
|
||||
Option<String> s = Match(i).option(Case($(0), "zero"));
|
||||
assertTrue(s.isEmpty());
|
||||
assertEquals("None", s.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchWorksWithPredicate_thenCorrect() {
|
||||
int i = 3;
|
||||
String s = Match(i).of(Case(is(1), "one"), Case(is(2), "two"), Case(is(3), "three"), Case($(), "?"));
|
||||
assertEquals("three", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInput_whenMatchesClass_thenCorrect() {
|
||||
Object obj = 5;
|
||||
String s = Match(obj).of(Case(instanceOf(String.class), "string matched"), Case($(), "not string"));
|
||||
assertEquals("not string", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInput_whenMatchesNull_thenCorrect() {
|
||||
Object obj = 5;
|
||||
String s = Match(obj).of(Case(isNull(), "no value"), Case(isNotNull(), "value found"));
|
||||
assertEquals("value found", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInput_whenContainsWorks_thenCorrect() {
|
||||
int i = 5;
|
||||
String s = Match(i).of(Case(isIn(2, 4, 6, 8), "Even Single Digit"), Case(isIn(1, 3, 5, 7, 9), "Odd Single Digit"), Case($(), "Out of range"));
|
||||
assertEquals("Odd Single Digit", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInput_whenMatchAllWorks_thenCorrect() {
|
||||
Integer i = null;
|
||||
String s = Match(i).of(Case(allOf(isNotNull(), isIn(1, 2, 3, null)), "Number found"), Case($(), "Not found"));
|
||||
assertEquals("Not found", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInput_whenMatchesAnyOfWorks_thenCorrect() {
|
||||
Integer year = 1990;
|
||||
String s = Match(year).of(Case(anyOf(isIn(1990, 1991, 1992), is(1986)), "Age match"), Case($(), "No age match"));
|
||||
assertEquals("Age match", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInput_whenMatchesNoneOfWorks_thenCorrect() {
|
||||
Integer year = 1990;
|
||||
String s = Match(year).of(Case(noneOf(isIn(1990, 1991, 1992), is(1986)), "Age match"), Case($(), "No age match"));
|
||||
assertEquals("No age match", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchWorksWithPredicate_thenCorrect2() {
|
||||
int i = 5;
|
||||
String s = Match(i).of(Case(isIn(2, 4, 6, 8), "Even Single Digit"), Case(isIn(1, 3, 5, 7, 9), "Odd Single Digit"), Case($(), "Out of range"));
|
||||
assertEquals("Odd Single Digit", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMatchCreatesSideEffects_thenCorrect() {
|
||||
int i = 4;
|
||||
Match(i).of(Case(isIn(2, 4, 6, 8), o -> run(this::displayEven)), Case(isIn(1, 3, 5, 7, 9), o -> run(this::displayOdd)), Case($(), o -> run(() -> {
|
||||
throw new IllegalArgumentException(String.valueOf(i));
|
||||
})));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void displayEven() {
|
||||
System.out.println("Input is even");
|
||||
}
|
||||
|
||||
public void displayOdd() {
|
||||
System.out.println("Input is odd");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user