All Downloads are FREE. Search and download functionalities are using the official Maven repository.

example.functionalj.result.ResultExample Maven / Gradle / Ivy

There is a newer version: 0.5.2.0
Show newest version
package example.functionalj.result;

import static functionalj.functions.StrFuncs.matches;
import static org.junit.Assert.assertEquals;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

import org.junit.Test;

import functionalj.result.Result;
import lombok.val;

public class ResultExample {

    @Test
    public void testHandleException() {
        val wordCount
                = Result.of(()->Files.readAllBytes(Paths.get("FileNotFound.txt")))
                .map        (String::new)
                .map        (matches("[a-zA-Z]+"))
                .map        (Stream::count)
                .ifException(Exception::printStackTrace)
                .orElse     (0L)
                ;
        assertEquals(0L, wordCount.longValue());
    }
    
    @Test
    public void testValidation() {
        val result1
                = Result.valueOf("One Two Three Four Five Six")
                .map     (matches("[a-zA-Z]+"))
                .map     (Stream::count)
                .validate("Too few words: %d", count -> count > 5)
                ;
        assertEquals("Result:{ Value: 6 }", result1.toString());
        
        val result2
                = Result.valueOf("One Two Three Four")
                .map     (matches("[a-zA-Z]+"))
                .map     (Stream::count)
                .validate("Too few words: %d", count -> count > 5)
                ;
        assertEquals("Result:{ Invalid: Too few word: 4 }", result2.toString());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy