#10 everything but getIdentityType !!!!

Open
chu1ta26 wants to merge 1 commits from chu1ta26/GenericGroupObject:master into master

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

3
 import com.zipcodewilmington.generic.identifiables.IdentifiableInterface;
3
 import com.zipcodewilmington.generic.identifiables.IdentifiableInterface;
4
 
4
 
5
 import java.io.Serializable;
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
 import java.util.List;
10
 import java.util.List;
7
 import java.util.function.Predicate;
11
 import java.util.function.Predicate;
12
+import java.util.stream.Collectors;
8
 
13
 
9
 /**
14
 /**
10
  * @author leon on 06/12/2018.
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
     @Override
27
     @Override
14
     public Integer count() {
28
     public Integer count() {
15
-        return null;
29
+        return myGenList.size();
16
     }
30
     }
17
 
31
 
32
+
33
+
18
     @Override
34
     @Override
19
     public void insert(IdentifiableInterface object) {
35
     public void insert(IdentifiableInterface object) {
36
+        myGenList.add((TypeOfEntity) object);
20
 
37
 
21
     }
38
     }
22
 
39
 
23
     @Override
40
     @Override
24
     public void delete(IdentifiableInterface object) {
41
     public void delete(IdentifiableInterface object) {
25
 
42
 
43
+        myGenList.remove(object);
44
+
26
     }
45
     }
27
 
46
 
28
     @Override
47
     @Override
29
     public void delete(Serializable serializable) {
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
     @Override
58
     @Override
34
     public Boolean has(IdentifiableInterface object) {
59
     public Boolean has(IdentifiableInterface object) {
35
-        return null;
60
+        return myGenList.contains(object);
36
     }
61
     }
37
 
62
 
38
     @Override
63
     @Override
39
     public Boolean has(Serializable serializable) {
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
     @Override
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
     @Override
85
     @Override
49
-    public Class getIdentityType() {
86
+    public Class<? extends TypeOfId> getIdentityType() {
50
         return null;
87
         return null;
51
     }
88
     }
52
 
89
 
53
     @Override
90
     @Override
54
-    public Class getIdentifiableType() {
91
+    public Class<? extends TypeOfEntity> getIdentifiableType() {
55
         return null;
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
 /**
5
 /**
6
  * @author leon on 05/12/2018.
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
  * @ATTENTION_TO_STUDENTS - You are forbidden from modifying this interface
7
  * @ATTENTION_TO_STUDENTS - You are forbidden from modifying this interface
8
  */
8
  */
9
 public interface IdentifiableInterface<TypeOfId extends Serializable> {
9
 public interface IdentifiableInterface<TypeOfId extends Serializable> {
10
+    void setId(TypeOfId id);
10
     TypeOfId getIdentity();
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
 /**
5
 /**
6
  * @author leon on 05/12/2018.
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
     @Override
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
 
26
 
27
     @Test
27
     @Test
28
     public void testRandom() {
28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(1000);
30
     }
30
     }
31
 
31
 
32
     private void test(Integer numberOfObjectsToAdd) {
32
     private void test(Integer numberOfObjectsToAdd) {

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

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

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

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

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

26
 
26
 
27
     @Test
27
     @Test
28
     public void testRandom() {
28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(1000);
30
     }
30
     }
31
 
31
 
32
 
32
 
42
         // when
42
         // when
43
         for (int i = 0; i < numberOfObjectsToAdd; i++) {
43
         for (int i = 0; i < numberOfObjectsToAdd; i++) {
44
             IdentifiableInterface<TypeOfId> identifiable = supplier.get();
44
             IdentifiableInterface<TypeOfId> identifiable = supplier.get();
45
+            identifiable.setId((TypeOfId)"foo");
45
             group.insert(identifiable);
46
             group.insert(identifiable);
46
 
47
 
47
             // then
48
             // then

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

11
  * ( ͡☉ ͜ʖ ͡☉)
11
  * ( ͡☉ ͜ʖ ͡☉)
12
  */
12
  */
13
 public class GetIdentityTypeTest {
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
     @Test
17
     @Test
18
     public void testGetIdentityType() { // ༼∵༽ ༼⍨༽ ༼⍢༽ ༼⍤༽
18
     public void testGetIdentityType() { // ༼∵༽ ༼⍨༽ ༼⍢༽ ༼⍤༽

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

26
 
26
 
27
     @Test
27
     @Test
28
     public void testRandom() {
28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(1000);
30
     }
30
     }
31
 
31
 
32
     private void test(Integer numberOfObjectsToAdd) {
32
     private void test(Integer numberOfObjectsToAdd) {
43
             IdentifiableInterface<TypeOfId> identifiable = supplier.get();
43
             IdentifiableInterface<TypeOfId> identifiable = supplier.get();
44
             group.insert(identifiable);
44
             group.insert(identifiable);
45
             // then
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
 
26
 
27
     @Test
27
     @Test
28
     public void testRandom() {
28
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
29
+        test(1000);
30
     }
30
     }
31
 
31
 
32
     private void test(Integer numberOfObjectsToAdd) {
32
     private void test(Integer numberOfObjectsToAdd) {
44
             group.insert(identifiable);
44
             group.insert(identifiable);
45
 
45
 
46
             // then
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
 public class TestParameterization {
12
 public class TestParameterization {
13
     @Test
13
     @Test
14
     public void testStringAndPersonParameterization() {
14
     public void testStringAndPersonParameterization() {
15
-        Group<String, Person> personGroup = new Group<>();
15
+        Group<String, Person<String>> personGroup = new Group<>();
16
         String expectedIdentityType = "java.io.Serializable";
16
         String expectedIdentityType = "java.io.Serializable";
17
         String expectedIdentifiableType = "com.zipcodewilmington.generic.identifiables.IdentifiableInterface";
17
         String expectedIdentifiableType = "com.zipcodewilmington.generic.identifiables.IdentifiableInterface";
18
 
18
 
26
 
26
 
27
     @Test
27
     @Test
28
     public void testLongAndActionFigureParameterization() {
28
     public void testLongAndActionFigureParameterization() {
29
-        Group<Long, ActionFigure> group = new Group<>();
29
+        Group<Long, ActionFigure<Long>> group = new Group<>();
30
         String expectedIdentityType = "java.io.Serializable";
30
         String expectedIdentityType = "java.io.Serializable";
31
         String expectedIdentifiableType = "com.zipcodewilmington.generic.identifiables.IdentifiableInterface";
31
         String expectedIdentifiableType = "com.zipcodewilmington.generic.identifiables.IdentifiableInterface";
32
 
32
 

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

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

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

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