diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Lambdas2-ZCW.iml b/.idea/Lambdas2-ZCW.iml new file mode 100644 index 0000000..81ce5fd --- /dev/null +++ b/.idea/Lambdas2-ZCW.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..1efcc24 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_13_2.xml b/.idea/libraries/Maven__junit_junit_4_13_2.xml new file mode 100644 index 0000000..606c352 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_13_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..97947a5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e080e35 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fba90c3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + groupId + Lambdas2-ZCW + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + junit + junit + RELEASE + test + + + + \ No newline at end of file diff --git a/src/main/java/CheckPerson.java b/src/main/java/CheckPerson.java new file mode 100644 index 0000000..26a5bae --- /dev/null +++ b/src/main/java/CheckPerson.java @@ -0,0 +1,3 @@ +public interface CheckPerson { + boolean test(Person p); +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..2aac17c --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,59 @@ +import com.sun.jndi.toolkit.dir.SearchFilter; +import com.sun.tools.javac.comp.Check; +import sun.nio.ch.Net; + +import javax.naming.directory.SearchControls; +import java.util.ArrayList; +import java.util.List; + +public class Main { + + public static void printPersons(List roster, CheckPerson filter) { + for (Person p : roster) { + if (filter.test(p)) { + p.printPerson(); + } + } + } + + public static void main(String[] args) { + + Person p1 = new Person("Bobby", 21, Person.Sex.MALE,"hotmail"); + Person p2 = new Person("Ana",25, Person.Sex.FEMALE,"Yahoo"); + Person p3 = new Person("Jack",30, Person.Sex.MALE,"Google"); + Person[] people = {p1,p2,p3}; + + Network network = new Network(); + network.addArray(people); + + //--------------------------LOCAL CLASS------------------------------- + //Creating local class + class Under30 implements CheckPerson{ + @Override + public boolean test(Person p) { + return p.getAge() < 30; + } + } + //Testing using local class + System.out.println("===========================LOCAL========================="); + printPersons(network.getList(),new Under30()); + + //--------------------------ANONYMOUS CLASS--------------------------- + //Creating anonymous class + CheckPerson maleUnder30 = new CheckPerson() { + @Override + public boolean test(Person p) { + return p.getGender() == Person.Sex.MALE && p.getAge() < 30; + } + }; + //Testing using anonymous class + System.out.println("=========================ANONYMOUS========================"); + printPersons(network.getList(), maleUnder30); + + //--------------------------LAMBDA EXPRESSION------------------------ + //Testing using lambda expression for female under 30 + System.out.println("===========================LAMBDA========================="); + printPersons(network.getList(), person -> + person.getGender()== Person.Sex.FEMALE && person.getAge() < 30); + } +} diff --git a/src/main/java/Network.java b/src/main/java/Network.java new file mode 100644 index 0000000..c067675 --- /dev/null +++ b/src/main/java/Network.java @@ -0,0 +1,30 @@ +import java.util.ArrayList; +import java.util.List; + +public class Network { + private List list; + + public Network() { + this.list = new ArrayList<>(); + } + + public void add(Person person){ + this.list.add(person); + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public void addArray(Person[] people){ + for (int i = 0; i < people.length; i++) { + this.list.add(people[i]); + } + } + + +} diff --git a/src/main/java/Person.java b/src/main/java/Person.java new file mode 100644 index 0000000..797221a --- /dev/null +++ b/src/main/java/Person.java @@ -0,0 +1,65 @@ +public class Person { + + public enum Sex { + MALE, FEMALE + } + + private String name; + private Integer age; + private Sex gender; + private String emailAddress; + + public Person() { + } + + public Person(String name, Integer age, Sex gender, String emailAddress) { + this.name = name; + this.age = age; + this.gender = gender; + this.emailAddress = emailAddress; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Sex getGender() { + return gender; + } + + public void setGender(Sex gender) { + this.gender = gender; + } + + public String getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + @Override + public String toString(){ + return "Person = " + name + + ", age = " + age + + ", gender = " + gender + + ", emailAddress = " + emailAddress + "\n"; + } + + public void printPerson() { + System.out.println(this.toString()); + } +} diff --git a/src/main/java/PersonUtilities.java b/src/main/java/PersonUtilities.java new file mode 100644 index 0000000..8438525 --- /dev/null +++ b/src/main/java/PersonUtilities.java @@ -0,0 +1,109 @@ +import com.sun.tools.javac.comp.Check; + +import java.util.ArrayList; +import java.util.List; + +public class PersonUtilities { + + + public static List personsOlderThan(List list, int age) { + List newList = new ArrayList<>(); + for (Person p : list) { + if (p.getAge() >= age) { + newList.add(p); + } + } + return newList; + } + + public static List personsYoungerThan(List list, int age) { + List newList = new ArrayList<>(); + for (Person p : list) { + if (p.getAge() <= age) { + newList.add(p); + } + } + return newList; + } + + public static Person findByName(List list, String name){ + for(Person p: list){ + if(p.getName().equals(name)){ + return p; + } + } + return null; + } + + public static List personsOfGender(List list, Person.Sex gender) { + List newList = new ArrayList<>(); + for (Person p : list) { + if (p.getGender().equals(gender)) { + newList.add(p); + } + } + return newList; + } + + public static void printPersons(List roster, CheckPerson filter) { + for (Person p : roster) { + if (filter.test(p)) { + p.printPerson(); + } + } + } + + public static List getPersons(List list, CheckPerson filter){ + List filtered = new ArrayList<>(); + for(Person p : list){ + if(filter.test(p)){ + filtered.add(p); + } + } + return filtered; + } + + //-------------Local Class-------------- + static class Under30 implements CheckPerson{ + public Under30() { + } + @Override + public boolean test(Person p) { + return p.getAge() < 30; + } + } + + static class FemaleWithGoogle implements CheckPerson{ + public FemaleWithGoogle() { + } + @Override + public boolean test(Person p) { + return p.getGender() == Person.Sex.FEMALE && p.getEmailAddress().equals("Google"); + } + } + + + //--------------------------ANONYMOUS CLASS--------------------------- + //Creating anonymous class + private static CheckPerson maleUnder30 = new CheckPerson() { + @Override + public boolean test(Person p) { + return p.getGender() == Person.Sex.MALE && p.getAge() < 30; + } + }; + + public static CheckPerson getMaleUnder30() { + return maleUnder30; + } + + private static CheckPerson femaleAbove30 = new CheckPerson(){ + @Override + public boolean test(Person p) { + return p.getGender().equals(Person.Sex.FEMALE) && p.getAge() > 30; + } + }; + public static CheckPerson femaleAbove30(){ + return femaleAbove30; + } + +} diff --git a/src/test/java/PersonUtilitiesTest.java b/src/test/java/PersonUtilitiesTest.java new file mode 100644 index 0000000..ef8203b --- /dev/null +++ b/src/test/java/PersonUtilitiesTest.java @@ -0,0 +1,114 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +public class PersonUtilitiesTest { + Network network = new Network(); + Person p1 = new Person("Bobby", 21, Person.Sex.MALE,"Hotmail"); + Person p2 = new Person("Ana",25, Person.Sex.FEMALE,"Yahoo"); + Person p3 = new Person("Jack",30, Person.Sex.MALE,"Google"); + Person p4 = new Person("Jill",41, Person.Sex.FEMALE,"AOL"); + Person p5 = new Person("Pete",55, Person.Sex.MALE,"YMail"); + Person p6 = new Person("Beth",34, Person.Sex.FEMALE,"Google"); + + @Before + public void populateNetwork(){ + Person[] people = {p1,p2,p3,p4,p5,p6}; + network.addArray(people); + } + + @Test + public void testPersonOlderThan(){ + //when + List filtered = PersonUtilities.personsOlderThan(network.getList(),35); + boolean result = filtered.contains(p4) && filtered.contains(p5); + //then + Assert.assertTrue(result); + } + + @Test + public void testPersonYoungerThan(){ + //when + List filtered = PersonUtilities.personsYoungerThan(network.getList(),30); + boolean result = filtered.contains(p1) && filtered.contains(p2); + //then + Assert.assertTrue(result); + } + + @Test + public void testFindByName(){ + //when + Person retrieved = PersonUtilities.findByName(network.getList(),"Bobby"); + //then + Assert.assertEquals(p1,retrieved); + } + + @Test + public void testPersonsOfGender(){ + //when + List filtered = PersonUtilities.personsOfGender(network.getList(), Person.Sex.FEMALE); + boolean result = filtered.contains(p2) && filtered.contains(p4) && filtered.contains(p6); + //then + Assert.assertTrue(result); + } + + //----------------------------Local Class------------------------ + @Test + public void testLocalClassUnder30(){ + //when + List filtered = PersonUtilities.getPersons(network.getList(), new PersonUtilities.Under30()); + boolean result = filtered.contains(p1) && filtered.contains(p2); + //then + Assert.assertTrue(result); + } + + @Test + public void testFemaleWithGoogle(){ + //when + List filtered = PersonUtilities.getPersons(network.getList(), new PersonUtilities.FemaleWithGoogle()); + boolean result = filtered.contains(p6); + //then + Assert.assertTrue(result); + } + + //---------------------------Anonymous Class--------------------- + @Test + public void testMaleUnder30(){ + List filtered = PersonUtilities.getPersons(network.getList(), PersonUtilities.getMaleUnder30()); + boolean result = filtered.contains(p1); + //then + Assert.assertTrue(result); + } + + @Test + public void testFemaleAbove30(){ + List filtered = PersonUtilities.getPersons(network.getList(), PersonUtilities.femaleAbove30()); + boolean result = filtered.contains(p4) && filtered.contains(p6); + //then + Assert.assertTrue(result); + } + + + //------------------------------Lambda-------------------------- + @Test + public void testMaleOver30(){ + List filtered = PersonUtilities.getPersons(network.getList(), + person -> person.getAge() > 30 && person.getGender() == Person.Sex.MALE); + boolean result = filtered.contains(p5); + //then + Assert.assertTrue(result); + } + + @Test + public void testMaleWithYahoo(){ + List filtered = PersonUtilities.getPersons(network.getList(), + person -> person.getGender().equals(Person.Sex.MALE) && person.getEmailAddress().equals("Yahoo")); + boolean result = filtered.isEmpty(); + //then + Assert.assertTrue(result); + + } + +} diff --git a/target/classes/CheckPerson.class b/target/classes/CheckPerson.class new file mode 100644 index 0000000..faa13df Binary files /dev/null and b/target/classes/CheckPerson.class differ diff --git a/target/classes/Main$1.class b/target/classes/Main$1.class new file mode 100644 index 0000000..e783862 Binary files /dev/null and b/target/classes/Main$1.class differ diff --git a/target/classes/Main$1Under30.class b/target/classes/Main$1Under30.class new file mode 100644 index 0000000..dcf97e2 Binary files /dev/null and b/target/classes/Main$1Under30.class differ diff --git a/target/classes/Main.class b/target/classes/Main.class new file mode 100644 index 0000000..e14d6c9 Binary files /dev/null and b/target/classes/Main.class differ diff --git a/target/classes/Network.class b/target/classes/Network.class new file mode 100644 index 0000000..4d6af43 Binary files /dev/null and b/target/classes/Network.class differ diff --git a/target/classes/Person$Sex.class b/target/classes/Person$Sex.class new file mode 100644 index 0000000..b06b7c0 Binary files /dev/null and b/target/classes/Person$Sex.class differ diff --git a/target/classes/Person.class b/target/classes/Person.class new file mode 100644 index 0000000..0078795 Binary files /dev/null and b/target/classes/Person.class differ diff --git a/target/classes/PersonUtilities$1.class b/target/classes/PersonUtilities$1.class new file mode 100644 index 0000000..da7b2a1 Binary files /dev/null and b/target/classes/PersonUtilities$1.class differ diff --git a/target/classes/PersonUtilities$2.class b/target/classes/PersonUtilities$2.class new file mode 100644 index 0000000..326292c Binary files /dev/null and b/target/classes/PersonUtilities$2.class differ diff --git a/target/classes/PersonUtilities$FemaleWithGoogle.class b/target/classes/PersonUtilities$FemaleWithGoogle.class new file mode 100644 index 0000000..3284f5d Binary files /dev/null and b/target/classes/PersonUtilities$FemaleWithGoogle.class differ diff --git a/target/classes/PersonUtilities$Under30.class b/target/classes/PersonUtilities$Under30.class new file mode 100644 index 0000000..1966fb5 Binary files /dev/null and b/target/classes/PersonUtilities$Under30.class differ diff --git a/target/classes/PersonUtilities.class b/target/classes/PersonUtilities.class new file mode 100644 index 0000000..4767365 Binary files /dev/null and b/target/classes/PersonUtilities.class differ diff --git a/target/test-classes/PersonUtilitiesTest.class b/target/test-classes/PersonUtilitiesTest.class new file mode 100644 index 0000000..2db8b7c Binary files /dev/null and b/target/test-classes/PersonUtilitiesTest.class differ