package com.insightfullogic.java8.answers.chapter2;

import org.junit.Test;

import javax.swing.*;

import java.text.DateFormat;
import java.util.Date;
import java.util.function.Function;
import java.util.function.Predicate;

//see : https://github.com/junit-team/junit4/wiki/Assertions
import static org.junit.Assert.assertEquals;

public class LambdaExercises {

	@Test
	public void chapter2_1b() {
		Function<Double, Double> negate = (x) -> -1 * x;
		Function<Double, Double> square = (x) -> x * x;
		Function<Double, Double> percent = (x) -> 100 * x;
		assertEquals(negate.apply(1.0), -1.0, 1e-10);
		assertEquals(square.apply(11.0), 121, 1e-10);
		assertEquals(percent.apply(1.0), 100, 1e-10);
	}

	@Test
	public void chapter2_1c() {
		Function<Integer, Integer> one = x -> x + 1;
		// 2 isn't
		Function<Integer, Boolean> three = x -> x == 1;
		assertEquals(one.apply(1), Integer.valueOf(2)); // עƥ,assertEquals
		assertEquals(three.apply(1), true);
	}

	@Test
	public void chapter2_2b() {
		ThreadLocal<DateFormat> threadSafeFormatter = ThreadLocal.withInitial(() -> DateFormat.getDateInstance());
		DateFormat formatter = threadSafeFormatter.get();
		assertEquals("1970-1-1", formatter.format(new Date(0)));
	}

	@Test
	public void chapter2_3a() {
		// pass,RunnableǶ߳̽ӿ
		Runnable helloWorld = () -> System.out.println("Hello World!");
		helloWorld.run();
	}
	
	@Test
	public void chapter2_3b(){
		// pass
		JButton jButton=new JButton();
		jButton.addActionListener(event->System.out.println(event.getActionCommand()));
	}
}

@@ -1581,11 +1581,85 @@
 );%0D%0A%09%7D%0D%0A
+%09%0D%0A%09@Test %0D%0A%09public void chapter2_3c()%7B%0D%0A%09%09// can't pass%0D%0A%09%09// ,%DE%B7%CA%B5%D0%A7%D0%B6%0D%0A%09%7D%0D%0A
 %7D%0D%0A

complete the answer in chapter2