#8 Completed all but two impossible tasks

Atvērta
mpierse vēlas sapludināt 2 revīzijas no mpierse/GenericGroupObject:master uz master

+ 44
- 15
src/main/java/com/zipcodewilmington/generic/group/Group.java Parādīt failu

@@ -3,56 +3,85 @@ package com.zipcodewilmington.generic.group;
3 3
 import com.zipcodewilmington.generic.identifiables.IdentifiableInterface;
4 4
 
5 5
 import java.io.Serializable;
6
+import java.util.ArrayList;
7
+import java.util.Iterator;
6 8
 import java.util.List;
7 9
 import java.util.function.Predicate;
10
+import java.util.function.Supplier;
11
+import java.util.stream.Collectors;
8 12
 
9 13
 /**
10 14
  * @author leon on 06/12/2018.
11 15
  */
12
-public class Group<ChangeThisTypeRespectively,ChangeThisOneToo> extends AbstractGroup{
16
+public class Group<TypeOfId extends Serializable ,TypeOfEntity extends IdentifiableInterface<TypeOfId>>
17
+        extends AbstractGroup<TypeOfId, TypeOfEntity>{
18
+
19
+    public Group() {
20
+        this.list = new ArrayList<>();
21
+    }
22
+
23
+    private ArrayList<TypeOfEntity> list;
24
+
25
+
13 26
     @Override
14 27
     public Integer count() {
15
-        return null;
28
+        return list.size();
16 29
     }
17 30
 
18 31
     @Override
19
-    public void insert(IdentifiableInterface object) {
20
-
32
+    public void insert(TypeOfEntity object) {
33
+        list.add(object);
21 34
     }
22 35
 
23 36
     @Override
24
-    public void delete(IdentifiableInterface object) {
25
-
37
+    public void delete(TypeOfEntity object) {
38
+        list.remove(object);
26 39
     }
27 40
 
28 41
     @Override
29
-    public void delete(Serializable serializable) {
30
-
42
+    public void delete(TypeOfId serializable) {
43
+        Iterator iterator = list.iterator();
44
+        while (iterator.hasNext()){
45
+            IdentifiableInterface next = (IdentifiableInterface) iterator.next();
46
+            if(next.getIdentity().equals(serializable)) iterator.remove();
47
+        }
31 48
     }
32 49
 
33 50
     @Override
34
-    public Boolean has(IdentifiableInterface object) {
35
-        return null;
51
+    public Boolean has(TypeOfEntity object) {
52
+        for (IdentifiableInterface id : list) {
53
+            if (list.contains(id)) return true;
54
+        }
55
+        return false;
36 56
     }
37 57
 
38 58
     @Override
39
-    public Boolean has(Serializable serializable) {
40
-        return null;
59
+    public Boolean has(TypeOfId serializable) {
60
+        Iterator iterator = list.iterator();
61
+        while (iterator.hasNext()){
62
+            IdentifiableInterface next = (IdentifiableInterface) iterator.next();
63
+            if(next.getIdentity().equals(serializable)) return true;
64
+        }
65
+        return false;
41 66
     }
42 67
 
43 68
     @Override
44 69
     public List filter(Predicate predicate) {
45
-        return null;
70
+        return (ArrayList<TypeOfEntity>)list.stream()
71
+                .filter(predicate)
72
+                .collect(Collectors.toList());
46 73
     }
47 74
 
48 75
     @Override
49 76
     public Class getIdentityType() {
50
-        return null;
77
+        return list.getClass();
51 78
     }
52 79
 
53 80
     @Override
54 81
     public Class getIdentifiableType() {
55
-        return null;
82
+
83
+        return this.getClass().getEnclosingClass();
56 84
     }
57 85
 
86
+
58 87
 }

+ 27
- 4
src/main/java/com/zipcodewilmington/generic/identifiables/ActionFigure.java Parādīt failu

@@ -5,14 +5,37 @@ import java.io.Serializable;
5 5
 /**
6 6
  * @author leon on 05/12/2018.
7 7
  */
8
-public class ActionFigure implements IdentifiableInterface {
8
+public class ActionFigure implements IdentifiableInterface<Long> {
9
+
10
+    Long id;
11
+    Class thisClass = this.getClass();
12
+
13
+    public ActionFigure() {
14
+        this.id =  (long) (Math.random() * 100L);
15
+    }
16
+
17
+    public Class getThisClass() {
18
+        return thisClass;
19
+    }
20
+
9 21
 
10 22
     @Override
11
-    public Serializable getIdentity() {
12
-        return null;
23
+    public Long getIdentity() {
24
+        return id;
13 25
     }
14 26
 
15 27
     public Class getIdentityType() {
16
-        return null;
28
+        return id.getClass();
17 29
     }
30
+
31
+    @Override
32
+    public void setId(Long aLong) {
33
+
34
+    }
35
+
36
+//    @Override
37
+//    public void setId() {
38
+//        this.id = (long) (Math.random() * 100L);
39
+//    }
40
+
18 41
 }

+ 1
- 0
src/main/java/com/zipcodewilmington/generic/identifiables/IdentifiableInterface.java Parādīt failu

@@ -9,4 +9,5 @@ import java.io.Serializable;
9 9
 public interface IdentifiableInterface<TypeOfId extends Serializable> {
10 10
     TypeOfId getIdentity();
11 11
     Class<? extends TypeOfId> getIdentityType();
12
+    void setId(TypeOfId id);
12 13
 }

+ 29
- 4
src/main/java/com/zipcodewilmington/generic/identifiables/Person.java Parādīt failu

@@ -1,18 +1,43 @@
1 1
 package com.zipcodewilmington.generic.identifiables;
2 2
 
3 3
 import java.io.Serializable;
4
+import java.nio.charset.Charset;
5
+import java.util.Random;
4 6
 
5 7
 /**
6 8
  * @author leon on 05/12/2018.
7 9
  */
8
-public class Person implements IdentifiableInterface {
10
+public class Person implements IdentifiableInterface<String> {
11
+
12
+    String id;
13
+
14
+
15
+    public Person() {
16
+        this.id = randomString();
17
+    }
18
+
19
+
20
+    private String randomString(){
21
+        byte[] array = new byte[7]; // length is bounded by 7
22
+        new Random().nextBytes(array);
23
+        String generatedString = new String(array, Charset.forName("UTF-8"));
24
+        return generatedString;
25
+    }
26
+
9 27
 
10 28
     @Override
11
-    public Serializable getIdentity() {
12
-        return null;
29
+    public String getIdentity() {
30
+        return id;
13 31
     }
14 32
 
15 33
     public Class getIdentityType() {
16
-        return null;
34
+        System.out.println(id.getClass());
35
+        return id.getClass();
36
+    }
37
+
38
+    @Override
39
+    public void setId(String id) {
40
+        this.id = id;
17 41
     }
42
+
18 43
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/generic/group/CountTest.java Parādīt failu

@@ -26,7 +26,7 @@ public class CountTest {
26 26
 
27 27
     @Test
28 28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(Math.abs(104543));
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {

+ 2
- 1
src/test/java/com/zipcodewilmington/generic/group/DeleteByIdTest.java Parādīt failu

@@ -26,7 +26,7 @@ public class DeleteByIdTest {
26 26
 
27 27
     @Test
28 28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(Math.abs(1000));
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {
@@ -41,6 +41,7 @@ public class DeleteByIdTest {
41 41
         // when
42 42
         for (int i = 0; i < numberOfObjectsToAdd; i++) {
43 43
             IdentifiableInterface<TypeOfId> identifiable = supplier.get();
44
+            identifiable.getIdentity();
44 45
             group.insert(identifiable);
45 46
             if(!group.has(identifiable)) {
46 47
                 throw new UnsupportedOperationException("`.insert` has not yet been implemented");

+ 1
- 1
src/test/java/com/zipcodewilmington/generic/group/DeleteByValueTest.java Parādīt failu

@@ -26,7 +26,7 @@ public class DeleteByValueTest {
26 26
 
27 27
     @Test
28 28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(Math.abs(3));
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {

+ 1
- 1
src/test/java/com/zipcodewilmington/generic/group/FilterTest.java Parādīt failu

@@ -26,7 +26,7 @@ public class FilterTest {
26 26
 
27 27
     @Test
28 28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(Math.abs(1000));
30 30
     }
31 31
 
32 32
 

+ 2
- 2
src/test/java/com/zipcodewilmington/generic/group/HasByIdTest.java Parādīt failu

@@ -26,7 +26,7 @@ public class HasByIdTest {
26 26
 
27 27
     @Test
28 28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(Math.abs(5000));
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {
@@ -43,7 +43,7 @@ public class HasByIdTest {
43 43
             IdentifiableInterface<TypeOfId> identifiable = supplier.get();
44 44
             group.insert(identifiable);
45 45
             // then
46
-            Assert.assertFalse(group.has(identifiable.getIdentity()));
46
+            Assert.assertTrue(group.has(identifiable.getIdentity()));
47 47
         }
48 48
     }
49 49
 }

+ 2
- 2
src/test/java/com/zipcodewilmington/generic/group/InsertTest.java Parādīt failu

@@ -26,7 +26,7 @@ public class InsertTest {
26 26
 
27 27
     @Test
28 28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(Math.abs(50000));
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {
@@ -44,7 +44,7 @@ public class InsertTest {
44 44
             group.insert(identifiable);
45 45
 
46 46
             // then
47
-            Assert.assertFalse(group.has(identifiable));
47
+            Assert.assertTrue(group.has(identifiable));
48 48
         }
49 49
     }
50 50
 }