瀏覽代碼

tests passing

Trinh Tong 6 年之前
父節點
當前提交
ca319bd0cb

+ 30
- 8
src/main/java/com/zipcodewilmington/generic/group/Group.java 查看文件

@@ -3,47 +3,69 @@ 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.Type;
7
+import java.util.ArrayList;
6 8
 import java.util.List;
7 9
 import java.util.function.Predicate;
10
+import java.util.stream.Collectors;
11
+import java.util.stream.Stream;
8 12
 
9 13
 /**
10 14
  * @author leon on 06/12/2018.
11 15
  */
12 16
 public class Group<TypeOfId extends Serializable,
13
-        TypeOfEntity extends IdentifiableInterface<TypeOfId>> extends AbstractGroup implements GroupInterface{
17
+        TypeOfEntity extends IdentifiableInterface<TypeOfId>> extends AbstractGroup<TypeOfId, TypeOfEntity>{
18
+
19
+    List<TypeOfEntity> group;
20
+
21
+    public Group() {
22
+        group = new ArrayList<>();
23
+    }
24
+
14 25
     @Override
15 26
     public Integer count() {
16
-        return null;
27
+        return group.size();
17 28
     }
18 29
 
19 30
     @Override
20 31
     public void insert(IdentifiableInterface object) {
21
-
32
+        group.add((TypeOfEntity) object);
22 33
     }
23 34
 
24 35
     @Override
25 36
     public void delete(IdentifiableInterface object) {
37
+        group.remove(object);
26 38
 
27 39
     }
28 40
 
29 41
     @Override
30 42
     public void delete(Serializable serializable) {
31
-
43
+        while (group.iterator().hasNext()) {
44
+            if (group.iterator().next().getIdentity() == serializable) {
45
+                group.remove(group.iterator().next());
46
+            }
47
+        }
32 48
     }
33 49
 
34 50
     @Override
35 51
     public Boolean has(IdentifiableInterface object) {
36
-        return null;
52
+        return group.contains(object);
37 53
     }
38 54
 
39 55
     @Override
40 56
     public Boolean has(Serializable serializable) {
41
-        return null;
57
+        for (TypeOfEntity t:group
58
+             ) {
59
+            if (t.getIdentity() == serializable) {
60
+                return true;
61
+            }
62
+        }
63
+        return false;
42 64
     }
43 65
 
44 66
     @Override
45
-    public List filter(Predicate predicate) {
46
-        return null;
67
+    public List<TypeOfEntity> filter(Predicate<TypeOfEntity> predicate) {
68
+        return group.stream().filter(predicate).collect(Collectors.toList());
47 69
     }
48 70
 
49 71
     @Override

+ 8
- 4
src/main/java/com/zipcodewilmington/generic/identifiables/ActionFigure.java 查看文件

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

+ 2
- 1
src/main/java/com/zipcodewilmington/generic/identifiables/IdentifiableInterface.java 查看文件

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

+ 12
- 4
src/main/java/com/zipcodewilmington/generic/identifiables/Person.java 查看文件

@@ -1,19 +1,27 @@
1 1
 package com.zipcodewilmington.generic.identifiables;
2 2
 
3 3
 import java.io.Serializable;
4
+import java.lang.reflect.ParameterizedType;
4 5
 
5 6
 /**
6 7
  * @author leon on 05/12/2018.
7 8
  */
8
-public class Person<TypeOfId extends Serializable> implements IdentifiableInterface {
9
+public class Person<TypeOfId extends Serializable> implements IdentifiableInterface<TypeOfId> {
9 10
     private TypeOfId id;
10 11
 
12
+    public Person() {
13
+    }
14
+
11 15
     @Override
12
-    public Serializable getIdentity() {
16
+    public TypeOfId getIdentity() {
13 17
         return id;
14 18
     }
15 19
 
16
-    public Class getIdentityType() {
17
-        return null;
20
+    public void setId(TypeOfId id) {
21
+        this.id = id;
22
+    }
23
+
24
+    public Class<? extends Serializable> getIdentityType() {
25
+        return id.getClass();
18 26
     }
19 27
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/generic/group/CountTest.java 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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 查看文件

@@ -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
 

+ 6
- 0
src/test/java/com/zipcodewilmington/generic/identifiables/ActionFigureTest.java 查看文件

@@ -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
 /**
@@ -9,6 +10,11 @@ import org.junit.Test;
9 10
 public class ActionFigureTest {
10 11
     ActionFigure actionFigure = new ActionFigure();
11 12
 
13
+    @Before
14
+    public void setUp() throws Exception {
15
+        actionFigure.setId(100L);
16
+    }
17
+
12 18
     @Test
13 19
     public void testImplementation() {
14 20
         Assert.assertTrue(actionFigure instanceof IdentifiableInterface);

+ 11
- 2
src/test/java/com/zipcodewilmington/generic/identifiables/PersonTest.java 查看文件

@@ -1,19 +1,28 @@
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> foo;
12
+
13
+    @Before
14
+    public void setUp() throws Exception {
15
+        foo = new Person<>();
16
+        foo.setId("BAR");
17
+    }
18
+
10 19
     @Test
11 20
     public void testImplementation() {
12
-        Assert.assertTrue(new Person() instanceof IdentifiableInterface);
21
+        Assert.assertTrue(foo instanceof IdentifiableInterface);
13 22
     }
14 23
 
15 24
     @Test
16 25
     public void testGetIdentityType() {
17
-        Assert.assertEquals(new Person().getIdentityType(), String.class);
26
+        Assert.assertEquals(foo.getIdentityType(), String.class);
18 27
     }
19 28
 }