Browse Source

getIdentityType is not passing

Nuridalia.Hernandez 5 years ago
parent
commit
f40d7f9aed

+ 47
- 8
src/main/java/com/zipcodewilmington/generic/group/Group.java View File

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

+ 13
- 8
src/main/java/com/zipcodewilmington/generic/identifiables/ActionFigure.java View File

@@ -5,14 +5,19 @@ 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<TypeOfId extends Serializable> implements IdentifiableInterface<TypeOfId> {
9
+
10
+    private TypeOfId id;
9 11
 
10
-    @Override
11
-    public Serializable getIdentity() {
12
-        return null;
13
-    }
14 12
 
15
-    public Class getIdentityType() {
16
-        return null;
13
+    public void setId(TypeOfId id){
14
+        this.id=id;
15
+    }
16
+    public TypeOfId getIdentity(){
17
+        return id;
17 18
     }
18
-}
19
+    public Class<? extends Serializable> getIdentityType(){
20
+        return id.getClass();
21
+    }
22
+
23
+}

+ 2
- 1
src/main/java/com/zipcodewilmington/generic/identifiables/IdentifiableInterface.java View File

@@ -7,6 +7,7 @@ import java.io.Serializable;
7 7
  * @ATTENTION_TO_STUDENTS - You are forbidden from modifying this interface
8 8
  */
9 9
 public interface IdentifiableInterface<TypeOfId extends Serializable> {
10
+    void setId(TypeOfId id);
10 11
     TypeOfId getIdentity();
11
-    Class<? extends TypeOfId> getIdentityType();
12
+    Class<? extends Serializable> getIdentityType();
12 13
 }

+ 18
- 5
src/main/java/com/zipcodewilmington/generic/identifiables/Person.java View File

@@ -5,14 +5,27 @@ import java.io.Serializable;
5 5
 /**
6 6
  * @author leon on 05/12/2018.
7 7
  */
8
-public class Person implements IdentifiableInterface {
8
+public class Person <TypeOfId extends Serializable> implements IdentifiableInterface <TypeOfId>{
9
+private TypeOfId id;
10
+
11
+    public Person() {
12
+    }
9 13
 
10 14
     @Override
11
-    public Serializable getIdentity() {
12
-        return null;
15
+    public void setId(TypeOfId id) {
16
+        this.id=id;
17
+
13 18
     }
14 19
 
15
-    public Class getIdentityType() {
16
-        return null;
20
+    @Override
21
+    public TypeOfId getIdentity() {
22
+        return this.id;
23
+    }
24
+
25
+    @Override
26
+    public Class<? extends Serializable> getIdentityType() {
27
+        return String.class;
17 28
     }
18 29
 }
30
+
31
+

+ 1
- 1
src/test/java/com/zipcodewilmington/generic/group/CountTest.java View File

@@ -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(1000);
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {

+ 2
- 1
src/test/java/com/zipcodewilmington/generic/group/DeleteByIdTest.java View File

@@ -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(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 View File

@@ -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(1000);
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {

+ 2
- 1
src/test/java/com/zipcodewilmington/generic/group/FilterTest.java View File

@@ -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(1000);
30 30
     }
31 31
 
32 32
 
@@ -42,6 +42,7 @@ public class FilterTest {
42 42
         // when
43 43
         for (int i = 0; i < numberOfObjectsToAdd; i++) {
44 44
             IdentifiableInterface<TypeOfId> identifiable = supplier.get();
45
+            identifiable.setId((TypeOfId)"foo");
45 46
             group.insert(identifiable);
46 47
 
47 48
             // then

+ 2
- 2
src/test/java/com/zipcodewilmington/generic/group/GetIdentityTypeTest.java View File

@@ -11,8 +11,8 @@ import org.junit.Test;
11 11
  * ( ͡☉ ͜ʖ ͡☉)
12 12
  */
13 13
 public class GetIdentityTypeTest {
14
-    private Group<String, Person> personGroup = new Group<>();
15
-    private Group<Long, ActionFigure> actionFigureGroup = new Group<>();
14
+    private Group<String, Person<String>> personGroup = new Group<>();
15
+    private Group<Long, ActionFigure<Long>> actionFigureGroup = new Group<>();
16 16
 
17 17
     @Test
18 18
     public void testGetIdentityType() { // ༼∵༽ ༼⍨༽ ༼⍢༽ ༼⍤༽

+ 2
- 2
src/test/java/com/zipcodewilmington/generic/group/HasByIdTest.java View File

@@ -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(1000);
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 View File

@@ -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(1000);
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
 }

+ 2
- 2
src/test/java/com/zipcodewilmington/generic/group/TestParameterization.java View File

@@ -12,7 +12,7 @@ import org.junit.Test;
12 12
 public class TestParameterization {
13 13
     @Test
14 14
     public void testStringAndPersonParameterization() {
15
-        Group<String, Person> personGroup = new Group<>();
15
+        Group<String, Person<String>> personGroup = new Group<>();
16 16
         String expectedIdentityType = "java.io.Serializable";
17 17
         String expectedIdentifiableType = "com.zipcodewilmington.generic.identifiables.IdentifiableInterface";
18 18
 
@@ -26,7 +26,7 @@ public class TestParameterization {
26 26
 
27 27
     @Test
28 28
     public void testLongAndActionFigureParameterization() {
29
-        Group<Long, ActionFigure> group = new Group<>();
29
+        Group<Long, ActionFigure<Long>> group = new Group<>();
30 30
         String expectedIdentityType = "java.io.Serializable";
31 31
         String expectedIdentifiableType = "com.zipcodewilmington.generic.identifiables.IdentifiableInterface";
32 32
 

+ 5
- 0
src/test/java/com/zipcodewilmington/generic/identifiables/ActionFigureTest.java View File

@@ -1,6 +1,7 @@
1 1
 package com.zipcodewilmington.generic.identifiables;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6 7
 /**
@@ -8,6 +9,10 @@ import org.junit.Test;
8 9
  */
9 10
 public class ActionFigureTest {
10 11
     ActionFigure actionFigure = new ActionFigure();
12
+    @Before
13
+    public void setUp(){
14
+        actionFigure.setId(44L);
15
+    }
11 16
 
12 17
     @Test
13 18
     public void testImplementation() {

+ 11
- 1
src/test/java/com/zipcodewilmington/generic/identifiables/PersonTest.java View File

@@ -1,19 +1,29 @@
1 1
 package com.zipcodewilmington.generic.identifiables;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6 7
 /**
7 8
  * @author leon on 06/12/2018.
8 9
  */
9 10
 public class PersonTest {
11
+    Person<String>person;
12
+
13
+    @Before
14
+    public void setUp() throws Exception{
15
+        person= new Person<>();
16
+        person.setId("rose");
17
+    }
10 18
     @Test
11 19
     public void testImplementation() {
20
+
12 21
         Assert.assertTrue(new Person() instanceof IdentifiableInterface);
13 22
     }
14 23
 
15 24
     @Test
16 25
     public void testGetIdentityType() {
17
-        Assert.assertEquals(new Person().getIdentityType(), String.class);
26
+
27
+        Assert.assertEquals(person.getIdentityType(), String.class);
18 28
     }
19 29
 }