package com.seleniumtests.tests.web;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

import com.seleniumtests.core.SeleniumTestPlan;
import com.seleniumtests.webpage.RegistrationPage;

public class SeleniumWithJava8Session2 extends SeleniumTestPlan {

    @Test
    public void usingJava8() throws Exception {
        RegistrationPage registrationPage = new RegistrationPage(true);
        List<WebElement> inputElements = registrationPage.getAllLinks();

        System.out.println("###################");
        System.out.println("###################");
        System.out.println("get link count containing text selenium with out lambda");
        List<WebElement> resultantList = new ArrayList<>();
        for (WebElement webElement : inputElements) {
            if(webElement.getText().toLowerCase().contains("selenium")) {
                resultantList.add(webElement);
            }
        }
        System.out.println("Total links containing word selenium: " +resultantList.size());


        System.out.println("###################");
        System.out.println("###################");
        System.out.println("get link count containing text selenium with lambda");
        System.out.println("Total links containing word selenium: " +
                inputElements.stream()
                .filter(s -> s.getText().toLowerCase().contains("selenium"))
                .count());


        System.out.println("###################");
        System.out.println("###################");
        System.out.println("Abstract out the common statements to a method:");
        System.out.println("Total links containing word selenium: "
                +getLinkCountForGivenWord(inputElements, "selenium"));


        System.out.println("###################");
        System.out.println("###################");
        System.out.println("get link count containing text test and word length is > 4,  with out lambda");
        resultantList = new ArrayList<>();
        for (WebElement webElement : inputElements) {
            String word = webElement.getText().toLowerCase();
            if(word.contains("test") && word.length() > 4) {
                resultantList.add(webElement);
            }
        }
        System.out.println("resultant list size: " +resultantList.size());
        System.out.println("print total text of those links: ");
        for(WebElement webElement : resultantList) {
            System.out.println(webElement.getText());
        }

        System.out.println("###################");
        System.out.println("###################");
        System.out.println("get link count containing text test and word length is > 4,  with lambda");
        List<WebElement> listRetrievedUsingLambda =
                inputElements.stream()
                                .filter(element -> element.getText().toLowerCase().contains("test"))
                                .filter(element -> element.getText().length() > 4)
                                .collect(Collectors.toList());
        System.out.println("resultant list size: " +listRetrievedUsingLambda.size());
        System.out.println("print total text of those links: ");
        listRetrievedUsingLambda.forEach(element -> System.out.println(element.getText()));
    }


    private static long getLinkCountForGivenWord(List<WebElement> inputElements, String word) {
        return inputElements.stream()
                .filter(s -> hasGivenWord(s, word))
                .count();

    }

    private static boolean hasGivenWord(WebElement element, String word) {
        return element.getText().toLowerCase().contains(word);
    }


}

@@ -509,37 +509,36 @@
 ist%3CWebElement%3E 
+l
 in
-put
+k
 Elements = regis
@@ -843,37 +843,36 @@
 nt webElement : 
+l
 in
-put
+k
 Elements) %7B%0A    
@@ -1369,37 +1369,36 @@
                 
+l
 in
-put
+k
 Elements.stream(
@@ -1796,21 +1796,20 @@
 venWord(
+l
 in
-put
+k
 Elements
@@ -2116,21 +2116,20 @@
 ement : 
+l
 in
-put
+k
 Elements
@@ -2858,21 +2858,20 @@
         
+l
 in
-put
+k
 Elements

Fix comment