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

example.functionalj.ref.RefExamples Maven / Gradle / Ivy

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

import static functionalj.ref.Run.With;
import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Function;

import org.junit.Test;

import functionalj.ref.Ref;
import lombok.val;

public class RefExamples {
    
    static Ref> greeting = Ref.ofValue(RefExamples::defaultGreeting);
    static Ref>         println  = Ref.ofValue(System.out ::println);
    
    
    private static String defaultGreeting(String name) {
        return String.format("Hello %s!!", name);
    }
    
    public static void greet(String name) {
        val greetingString = greeting.value().apply(name);
        println.value().accept(greetingString);
    }
    
    public static void main(String[] args) {
        // Production
        greet("Jack");
    }
    
    @Test
    public void testDefaultMessage() {
        val logs = new ArrayList();
        With(println.butWith(logs::add))
        .run(()-> {
            greet("Jack");
        });
        assertEquals("[Hello Jack!!]", logs.toString());
    }
    
    @Test
    public void testCustomMessage() {
        val logs = new ArrayList();
        With(println .butWith(logs::add),
             greeting.butWith(name -> "What's up " + name + "?"))
        .run(()-> {
            greet("Jack");
        });
        assertEquals("[What's up Jack?]", logs.toString());
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy