Bläddra i källkod

converted project to lambda exercise

Leon 7 år sedan
förälder
incheckning
78c1a00dcf
21 ändrade filer med 344 tillägg och 232 borttagningar
  1. 0
    37
      src/main/java/com/zipcode/ArrayListLoopMethods.java
  2. 0
    36
      src/main/java/com/zipcode/ArrayLoopMethods.java
  3. 0
    62
      src/main/java/com/zipcode/Person.java
  4. 0
    35
      src/main/java/com/zipcode/tools/PersonFactory.java
  5. 95
    0
      src/main/java/com/zipcodewilmington/lambdas/StreamFilter.java
  6. 7
    0
      src/main/java/com/zipcodewilmington/lambdas/StreamMap.java
  7. 45
    0
      src/main/java/com/zipcodewilmington/lambdas/anthropoid/Person.java
  8. 55
    0
      src/main/java/com/zipcodewilmington/lambdas/anthropoid/PersonFactory.java
  9. 25
    0
      src/main/java/com/zipcodewilmington/lambdas/exercises/ArrayConversion.java
  10. 7
    0
      src/main/java/com/zipcodewilmington/lambdas/exercises/ConversionAgent.java
  11. 7
    0
      src/main/java/com/zipcodewilmington/lambdas/exercises/ListConversion.java
  12. 7
    0
      src/main/java/com/zipcodewilmington/lambdas/exercises/StreamConversion.java
  13. 1
    1
      src/main/java/com/zipcodewilmington/lambdas/tools/RandomUtils.java
  14. 7
    0
      src/main/java/com/zipcodewilmington/lambdas/tools/ReflectionUtils.java
  15. 7
    0
      src/main/java/com/zipcodewilmington/lambdas/tools/StringUtils.java
  16. 0
    30
      src/test/java/com/zipcode/ArrayListLoopMethodsTest.java
  17. 0
    31
      src/test/java/com/zipcode/ArrayLoopMethodsTest.java
  18. 7
    0
      src/test/java/com/zipcodewilmington/lambdas/TestPersonFactory.java
  19. 53
    0
      src/test/java/com/zipcodewilmington/lambdas/TestStreamFilter.java
  20. 14
    0
      src/test/java/com/zipcodewilmington/lambdas/TestStreamMap.java
  21. 7
    0
      src/test/java/com/zipcodewilmington/lambdas/TestSuiteFullRegression.java

+ 0
- 37
src/main/java/com/zipcode/ArrayListLoopMethods.java Visa fil

@@ -1,37 +0,0 @@
1
-package com.zipcode;
2
-
3
-import com.zipcode.tools.PersonFactory;
4
-
5
-import java.util.ArrayList;
6
-
7
-/**
8
- * Created by leon on 5/2/17.
9
- */
10
-public class ArrayListLoopMethods {
11
-    private ArrayList<Person> personArrayList = PersonFactory.createPersonArrayList(100);
12
-
13
-    public void testWhileLoop() {
14
-        int i = 0;
15
-        while(i<personArrayList.size()) {
16
-            Person person = personArrayList.get(i);
17
-            String name = person.getName();
18
-            System.out.println(name);
19
-            i++;
20
-        }
21
-    }
22
-
23
-    public void testForLoop() {
24
-        for(int i=0; i<personArrayList.size(); i++) {
25
-            Person person = personArrayList.get(i);
26
-            String name = person.getName();
27
-            System.out.println(name);
28
-        }
29
-    }
30
-
31
-    public void testForEachloop() {
32
-        for(Person person : personArrayList) {
33
-            String name = person.getName();
34
-            System.out.println(name);
35
-        }
36
-    }
37
-}

+ 0
- 36
src/main/java/com/zipcode/ArrayLoopMethods.java Visa fil

@@ -1,36 +0,0 @@
1
-package com.zipcode;
2
-
3
-import com.zipcode.Person;
4
-import com.zipcode.tools.PersonFactory;
5
-
6
-/**
7
- * Created by leon on 5/2/17.
8
- */
9
-public class ArrayLoopMethods {
10
-    private Person[] personArray = PersonFactory.createPersonArray(100);
11
-
12
-    public void testWhileLoop() {
13
-        int i = 0;
14
-        while(i< personArray.length) {
15
-            Person person = personArray[i];
16
-            String name = person.getName();
17
-            System.out.println(name);
18
-            i++;
19
-        }
20
-    }
21
-
22
-    public void testForLoop() {
23
-        for(int i = 0; i< personArray.length; i++) {
24
-            Person person = personArray[i];
25
-            String name = person.getName();
26
-            System.out.println(name);
27
-        }
28
-    }
29
-
30
-    public void testForEachloop() {
31
-        for(Person person : personArray) {
32
-            String name = person.getName();
33
-            System.out.println(name);
34
-        }
35
-    }
36
-}

+ 0
- 62
src/main/java/com/zipcode/Person.java Visa fil

@@ -1,62 +0,0 @@
1
-package com.zipcode;
2
-
3
-import java.util.Date;
4
-
5
-/**
6
- * Created by leon on 5/1/17.
7
- */
8
-public class Person {
9
-    private String name;
10
-    private int age;
11
-    private boolean isMale;
12
-    private long personalId;
13
-    private Date birthDate;
14
-
15
-    public Person(String name, int age, boolean isMale, long personalId, Date birthDate) {
16
-        this.name = name;
17
-        this.age = age;
18
-        this.isMale = isMale;
19
-        this.personalId = personalId;
20
-        this.birthDate = birthDate;
21
-    }
22
-
23
-    public String getName() {
24
-        return name;
25
-    }
26
-
27
-    public int getAge() {
28
-        return age;
29
-    }
30
-
31
-    public boolean isMale() {
32
-        return isMale;
33
-    }
34
-
35
-    public long getPersonalId() {
36
-        return personalId;
37
-    }
38
-
39
-    public Date getBirthDate() {
40
-        return birthDate;
41
-    }
42
-
43
-    public void setPersonalId(long personalId) {
44
-        this.personalId = personalId;
45
-    }
46
-
47
-    public void setMale(boolean male) {
48
-        isMale = male;
49
-    }
50
-
51
-    public void setAge(int age) {
52
-        this.age = age;
53
-    }
54
-
55
-    public void setName(String name) {
56
-        this.name = name;
57
-    }
58
-
59
-    public void setBirthDate(Date birthDate) {
60
-        this.birthDate = birthDate;
61
-    }
62
-}

+ 0
- 35
src/main/java/com/zipcode/tools/PersonFactory.java Visa fil

@@ -1,35 +0,0 @@
1
-package com.zipcode.tools;
2
-import com.zipcode.Person;
3
-
4
-import java.util.ArrayList;
5
-import java.util.Date;
6
-
7
-/** Created by leon on 5/1/17. */
8
-public class PersonFactory {
9
-    public static Person createRandomPerson() {
10
-        String name = RandomUtils.createString('a', 'z', 10);
11
-        int age = RandomUtils.createInteger(0, 99);
12
-        boolean isMale = RandomUtils.chance(50);
13
-        long personalId = System.nanoTime();
14
-        Date birthDate = RandomUtils.createDate(1950, 2010);
15
-        Person randomPerson = new Person(name, age, isMale, personalId, birthDate);
16
-        return randomPerson;
17
-    }
18
-
19
-    public static ArrayList<Person> createPersonArrayList(int personCount) {
20
-        ArrayList<Person> people = new ArrayList<Person>();
21
-        for(int i=0;i<people.size();i++) {
22
-            people.add(PersonFactory.createRandomPerson());
23
-        }
24
-        return people;
25
-    }
26
-
27
-
28
-    public static Person[] createPersonArray(int personCount) {
29
-        Person[] people = new Person[personCount];
30
-        for(int i=0;i<people.length;i++) {
31
-            people[i] = PersonFactory.createRandomPerson();
32
-        }
33
-        return people;
34
-    }
35
-}

+ 95
- 0
src/main/java/com/zipcodewilmington/lambdas/StreamFilter.java Visa fil

@@ -0,0 +1,95 @@
1
+package com.zipcodewilmington.lambdas;
2
+
3
+import com.zipcodewilmington.lambdas.anthropoid.Person;
4
+import com.zipcodewilmington.lambdas.anthropoid.PersonFactory;
5
+
6
+import java.util.List;
7
+import java.util.stream.Collectors;
8
+import java.util.stream.Stream;
9
+
10
+/**
11
+ * Created by leon on 5/2/17.
12
+ */
13
+public class LambdaFilter {
14
+    private final Stream<Person> personStream;
15
+    public final String startingCharacter;
16
+
17
+    /**
18
+     * No arg constructor
19
+     */
20
+    public LambdaFilter() {
21
+        this(PersonFactory.createPersonStream(100), 'A');
22
+    }
23
+
24
+    /**
25
+     * @param people - Array of person objects
26
+     * @param startingCharacter - character to filter by
27
+     */
28
+    public LambdaFilter(Person[] people, Character startingCharacter) {
29
+        this(Stream.of(people), startingCharacter);
30
+    }
31
+
32
+    /**
33
+     * @param people - List of person objects
34
+     * @param startingCharacter - character to filter by
35
+     */
36
+    public LambdaFilter(List<Person> people, Character startingCharacter) {
37
+        this(people.stream(), startingCharacter);
38
+    }
39
+
40
+
41
+    /**
42
+     * @param people - Stream of person objects
43
+     * @param startingCharacter - character to filter by
44
+     */
45
+    public LambdaFilter(Stream<Person> people, Character startingCharacter) {
46
+        this.personStream = people;
47
+        this.startingCharacter = startingCharacter.toString();
48
+    }
49
+
50
+
51
+
52
+    /**
53
+     * Using multi-line lambda syntax
54
+     * @return a list of person object whose name starts with "A"
55
+     */
56
+    public List<Person> toListMultiLine() {
57
+        Stream<Person> personStreamFiltered = personStream.filter(person -> person.name.getValue().startsWith(startingCharacter));
58
+        List list = personStreamFiltered.collect(Collectors.toList());
59
+        return list;
60
+    }
61
+
62
+
63
+    /**
64
+     * Using one-line lambda syntax
65
+     * @return a list of person object whose name starts with "A"
66
+     */
67
+    public List<Person> toListOneLine() {
68
+        return personStream
69
+                .filter(person -> person.name.getValue().startsWith(startingCharacter))
70
+                .collect(Collectors.toList());
71
+    }
72
+
73
+
74
+    /**
75
+     * Using one-line lambda syntax
76
+     * @return an array of person object whose name starts with "A"
77
+     */
78
+    public Person[] toArrayOneLine() {
79
+        return personStream
80
+                .filter(person -> person.name.getValue().startsWith(startingCharacter))
81
+                .toArray(Person[]::new); // method reference
82
+    }
83
+
84
+
85
+    /**
86
+     * Using multi-line lambda syntax
87
+     * @return an array of person object whose name starts with "A"
88
+     */
89
+    public Person[] toArrayMultiLine() {
90
+        Stream<Person> personStreamFiltered = personStream.filter(person -> person.name.getValue().startsWith(startingCharacter));
91
+        Person[] personArray = personStreamFiltered.toArray(Person[]::new); // method reference
92
+        return personArray;
93
+    }
94
+
95
+}

+ 7
- 0
src/main/java/com/zipcodewilmington/lambdas/StreamMap.java Visa fil

@@ -0,0 +1,7 @@
1
+package com.zipcodewilmington.lambdas;
2
+
3
+/**
4
+ * Created by leon on 5/24/17.
5
+ */
6
+public class MapSyntax {
7
+}

+ 45
- 0
src/main/java/com/zipcodewilmington/lambdas/anthropoid/Person.java Visa fil

@@ -0,0 +1,45 @@
1
+package com.zipcodewilmington.lambdas.tools.anthropoid;
2
+
3
+import java.util.Date;
4
+
5
+/**
6
+ * Created by leon on 5/1/17.
7
+ */
8
+public class Person {
9
+    public final PersonProperty<String> name;
10
+    public final PersonProperty<Integer> age;
11
+    public final PersonProperty<Boolean> isMale;
12
+    public final PersonProperty<Long> personalId;
13
+    public final PersonProperty<Date> birthDate;
14
+
15
+    /**
16
+     * @param name - name of person
17
+     * @param age - age of person
18
+     * @param isMale - gender flag
19
+     * @param personalId - unique id
20
+     * @param birthDate - date of birth
21
+     */
22
+    public Person(String name, int age, boolean isMale, long personalId, Date birthDate) {
23
+        this.name = new PersonProperty<>(name);
24
+        this.age = new PersonProperty<>(age);
25
+        this.isMale = new PersonProperty<>(isMale);
26
+        this.personalId = new PersonProperty<>(personalId);
27
+        this.birthDate = new PersonProperty<>(birthDate);
28
+    }
29
+
30
+    /** the purpose of this class is compress verbose getter and setter logic
31
+     * @param <E> the type of data to store */
32
+    public class PersonProperty<E> {
33
+        private E val; // value of the data
34
+
35
+        /** @param val - value of data for this object to store */
36
+        public PersonProperty(E val) {
37
+            this.val = val;
38
+        }
39
+
40
+        /** @return value this object is storing*/
41
+        public E getValue() {
42
+            return val;
43
+        }
44
+    }
45
+}

+ 55
- 0
src/main/java/com/zipcodewilmington/lambdas/anthropoid/PersonFactory.java Visa fil

@@ -0,0 +1,55 @@
1
+package com.zipcodewilmington.lambdas.tools.anthropoid;
2
+
3
+import com.zipcodewilmington.lambdas.tools.RandomUtils;
4
+import com.zipcodewilmington.lambdas.tools.StringUtils;
5
+
6
+import java.util.ArrayList;
7
+import java.util.Date;
8
+import java.util.List;
9
+import java.util.stream.Collectors;
10
+import java.util.stream.Stream;
11
+
12
+/**
13
+ * Created by leon on 5/1/17.
14
+ */
15
+public class PersonFactory {
16
+    /**
17
+     * @return a new instance of a person with randomized fields
18
+     */
19
+    public static Person createRandomPerson() {
20
+        String name = StringUtils.capitalizeFirstChar(RandomUtils.createString('a', 'z', 10));
21
+        int age = RandomUtils.createInteger(0, 99);
22
+        boolean isMale = RandomUtils.chance(50);
23
+        long personalId = System.nanoTime();
24
+        Date birthDate = RandomUtils.createDate(1950, 2010);
25
+
26
+        Person randomPerson = new Person(name, age, isMale, personalId, birthDate);
27
+        return randomPerson;
28
+    }
29
+
30
+    /**
31
+     * @param personCount - number of Person objects to create
32
+     * @return - ArrayList of Person objects
33
+     */
34
+    public static List<Person> createPersonList(int personCount) {
35
+        return createPersonStream(personCount).collect(Collectors.toList());
36
+    }
37
+
38
+
39
+    /**
40
+     * @param personCount - number of Person objects to create
41
+     * @return - Array of Person objects
42
+     */
43
+    public static Person[] createPersonArray(int personCount) {
44
+        return createPersonStream(personCount).toArray(Person[]::new);
45
+    }
46
+
47
+
48
+    /**
49
+     * @param personCount - number of Person objects to create
50
+     * @return - Stream representation of collection of Person objects
51
+     */
52
+    public static Stream<Person> createPersonStream(int personCount) {
53
+        return Stream.generate(PersonFactory::createRandomPerson).limit(personCount);
54
+    }
55
+}

+ 25
- 0
src/main/java/com/zipcodewilmington/lambdas/exercises/ArrayConversion.java Visa fil

@@ -0,0 +1,25 @@
1
+package com.zipcodewilmington.lambdas;
2
+
3
+import com.zipcodewilmington.lambdas.anthropoid.Person;
4
+
5
+import java.util.List;
6
+import java.util.function.Predicate;
7
+import java.util.stream.Stream;
8
+
9
+/**
10
+ * Created by leon on 5/25/17.
11
+ */
12
+public class ArrayConversions {
13
+    public Stream<Person> getStream() {
14
+        return null;
15
+    }
16
+
17
+    public static List<Person> getList() {
18
+        return null;
19
+    }
20
+
21
+    public Stream<Person> filter(Predicate<? super Person> predicate) {
22
+        return getStream().filter(predicate);
23
+    }
24
+
25
+}

+ 7
- 0
src/main/java/com/zipcodewilmington/lambdas/exercises/ConversionAgent.java Visa fil

@@ -0,0 +1,7 @@
1
+package com.zipcodewilmington.lambdas.exercises;
2
+
3
+/**
4
+ * Created by leon on 5/25/17.
5
+ */
6
+public class Conversion {
7
+}

+ 7
- 0
src/main/java/com/zipcodewilmington/lambdas/exercises/ListConversion.java Visa fil

@@ -0,0 +1,7 @@
1
+package com.zipcodewilmington.lambdas;
2
+
3
+/**
4
+ * Created by leon on 5/25/17.
5
+ */
6
+public class ListConversions {
7
+}

+ 7
- 0
src/main/java/com/zipcodewilmington/lambdas/exercises/StreamConversion.java Visa fil

@@ -0,0 +1,7 @@
1
+package com.zipcodewilmington.lambdas.exercises;
2
+
3
+/**
4
+ * Created by leon on 5/25/17.
5
+ */
6
+public class StreamConversions {
7
+}

src/main/java/com/zipcode/tools/RandomUtils.java → src/main/java/com/zipcodewilmington/lambdas/tools/RandomUtils.java Visa fil

@@ -17,7 +17,7 @@ public abstract class RandomUtils {
17 17
 
18 18
     /** @return a random character between the specified min and max character range */
19 19
     public static Character createCharacter(char min, char max) {
20
-        return (char) createInteger((int) min, (int) min).intValue();
20
+        return (char) createInteger((int) min, (int) max).intValue();
21 21
     }
22 22
 
23 23
     /** @return a random double between the specified min and max numeric range */

+ 7
- 0
src/main/java/com/zipcodewilmington/lambdas/tools/ReflectionUtils.java Visa fil

@@ -0,0 +1,7 @@
1
+package com.zipcodewilmington.lambdas.tools;
2
+
3
+/**
4
+ * Created by leon on 5/24/17.
5
+ */
6
+public class ReflectionUtils {
7
+}

+ 7
- 0
src/main/java/com/zipcodewilmington/lambdas/tools/StringUtils.java Visa fil

@@ -0,0 +1,7 @@
1
+package com.zipcodewilmington.lambdas.tools;
2
+
3
+/**
4
+ * Created by leon on 5/24/17.
5
+ */
6
+public class StringUtils {
7
+}

+ 0
- 30
src/test/java/com/zipcode/ArrayListLoopMethodsTest.java Visa fil

@@ -1,30 +0,0 @@
1
-package com.zipcode;
2
-
3
-import org.junit.Before;
4
-import org.junit.Test;
5
-
6
-/**
7
- * Created by leon on 5/2/17.
8
- */
9
-public class ArrayListLoopMethodsTest {
10
-    private ArrayListLoopMethods arrayListLoopMethods;
11
-    @Before
12
-    public void setup() {
13
-        arrayListLoopMethods = new ArrayListLoopMethods();
14
-    }
15
-
16
-    @Test
17
-    public void testWhileLoop() {
18
-        arrayListLoopMethods.testWhileLoop();
19
-    }
20
-
21
-    @Test
22
-    public void testForLoop() {
23
-        arrayListLoopMethods.testForLoop();
24
-    }
25
-
26
-    @Test
27
-    public void testForEachLoop() {
28
-        arrayListLoopMethods.testForEachloop();
29
-    }
30
-}

+ 0
- 31
src/test/java/com/zipcode/ArrayLoopMethodsTest.java Visa fil

@@ -1,31 +0,0 @@
1
-package com.zipcode;
2
-
3
-import org.junit.Before;
4
-import org.junit.Test;
5
-
6
-/**
7
- * Created by leon on 5/2/17.
8
- */
9
-public class ArrayLoopMethodsTest {
10
-    private ArrayLoopMethods arrayLoopMethods;
11
-
12
-    @Before
13
-    public void setup() {
14
-        arrayLoopMethods = new ArrayLoopMethods();
15
-    }
16
-
17
-    @Test
18
-    public void testWhileLoop() {
19
-        arrayLoopMethods.testWhileLoop();
20
-    }
21
-
22
-    @Test
23
-    public void testForLoop() {
24
-        arrayLoopMethods.testForLoop();
25
-    }
26
-
27
-    @Test
28
-    public void testForEachLoop() {
29
-        arrayLoopMethods.testForEachloop();
30
-    }
31
-}

+ 7
- 0
src/test/java/com/zipcodewilmington/lambdas/TestPersonFactory.java Visa fil

@@ -0,0 +1,7 @@
1
+package com.zipcodewilmington.lambdas;
2
+
3
+/**
4
+ * Created by leon on 5/24/17.
5
+ */
6
+public class TestPersonFactory {
7
+}

+ 53
- 0
src/test/java/com/zipcodewilmington/lambdas/TestStreamFilter.java Visa fil

@@ -0,0 +1,53 @@
1
+package com.zipcodewilmington.lambdas;
2
+
3
+import com.zipcodewilmington.lambdas.anthropoid.Person;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.Arrays;
8
+import java.util.List;
9
+
10
+/**
11
+ * Created by leon on 5/2/17.
12
+ */
13
+public class TestLambdaFilter {
14
+    private LambdaFilter lambdaFilter;
15
+
16
+    @Before
17
+    public void setup() {
18
+        lambdaFilter = new LambdaFilter();
19
+    }
20
+
21
+    @Test
22
+    public void testToListMultiLine() {
23
+        testFilter(lambdaFilter.toListMultiLine());
24
+    }
25
+
26
+    @Test
27
+    public void testToListOneLine() {
28
+        testFilter(lambdaFilter.toListOneLine());
29
+    }
30
+
31
+    @Test
32
+    public void testToArrayMultiLine() {
33
+        testFilter(lambdaFilter.toArrayMultiLine());
34
+    }
35
+
36
+    @Test
37
+    public void testToArrayOneLine() {
38
+        testFilter(lambdaFilter.toArrayOneLine());
39
+    }
40
+
41
+
42
+
43
+
44
+    private void testFilter(Person[] persons) {
45
+        testFilter(Arrays.asList(persons));
46
+    }
47
+
48
+    private void testFilter(List<Person> persons) {
49
+        for (Person p : persons) {
50
+            assert (p.name.getValue().startsWith(lambdaFilter.startingCharacter));
51
+        }
52
+    }
53
+}

+ 14
- 0
src/test/java/com/zipcodewilmington/lambdas/TestStreamMap.java Visa fil

@@ -0,0 +1,14 @@
1
+package com.zipcodewilmington.lambdas;
2
+
3
+import org.junit.Test;
4
+
5
+/**
6
+ * Created by leon on 5/24/17.
7
+ */
8
+public class
9
+TestMapSyntax {
10
+    @Test
11
+    public void test() {
12
+
13
+    }
14
+}

+ 7
- 0
src/test/java/com/zipcodewilmington/lambdas/TestSuiteFullRegression.java Visa fil

@@ -0,0 +1,7 @@
1
+package com.zipcodewilmington.lambdas;
2
+
3
+/**
4
+ * Created by leon on 5/24/17.
5
+ */
6
+public class TestSuiteFullRegression {
7
+}