package mariaJava;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class InAction241 {

	public void alInAction241() {
		sortWithComparator();	
		sortWithLambda();
		sortWithComparatorLambda();
		createThread1();
		createThread2();
	}
	
	//-- Comparator
	public void sortWithComparator() {
		System.out.println("Anonymous class");
		
		List<Apple> inventory = createInventoryOfApples();
		
		inventory.sort(new Comparator<Apple>() {
			@Override
			public int compare(Apple a1, Apple a2) {
				return a1.getWeight().compareTo(a2.getWeight());
			}
		});
		
		for (Apple a: inventory) {
			System.out.println("Sorted weight with anonymous class: " + a.getWeight());
		}
	}
	
	public void sortWithLambda() {
		List<Apple> inventory = createInventoryOfApples();
		
		inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
		
		for(Apple a : inventory) {
			System.out.println("Sorted weight with Lamblda " + a.getWeight());	
		}
				
	}
	
	public void sortWithComparatorLambda() {
		List<Apple> inventory = createInventoryOfApples();
		
		Comparator<Apple> comp = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
		
		inventory.sort(comp);
		
		for(Apple a : inventory) {
			System.out.println("Sorted with a lambda comparator: " + a.getWeight());
		}
	}
	
	//--- Runnable
	public void createThread1() {
		System.out.println("Create thread the old way");
		
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("Hi there");
			}
		});
		
		t.start();
		
	}
	
	public void createThread2() {
		System.out.println("Create thread in Java8");
		
		Thread t = new Thread (() -> System.out.println("Hej hej"));
		t.start();
	}
	
	//-------------------------
	private List<Apple> createInventoryOfApples() {
		Apple a1 = new Apple(100, "red");
		Apple a2 = new Apple(240, "red");
		Apple a3 = new Apple(160,  "green");
		
		List<Apple> inventory = new ArrayList<Apple>();
		inventory.add(a1);
		inventory.add(a2);
		inventory.add(a3);
		
		return inventory;
	}

}
