Просмотр исходного кода

methods written, tests passing

jacob andersen 6 лет назад
Родитель
Сommit
181c4c0d55

+ 1
- 3
src/main/java/com/zipcodewilmington/generic/group/AbstractGroup.java Просмотреть файл

@@ -7,8 +7,6 @@ import java.io.Serializable;
7 7
 /**
8 8
  * @author leon on 05/12/2018.
9 9
  */
10
-abstract public class AbstractGroup<
11
-        TypeOfId extends Serializable,
12
-        TypeOfEntity extends IdentifiableInterface<TypeOfId>>
10
+abstract public class AbstractGroup<TypeOfId extends Serializable, TypeOfEntity extends IdentifiableInterface<TypeOfId>>
13 11
         implements GroupInterface<TypeOfId, TypeOfEntity> {
14 12
 }

+ 46
- 17
src/main/java/com/zipcodewilmington/generic/group/Group.java Просмотреть файл

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

+ 1
- 3
src/main/java/com/zipcodewilmington/generic/group/GroupInterface.java Просмотреть файл

@@ -10,9 +10,7 @@ import java.util.function.Predicate;
10 10
  * @author leon on 05/12/2018.
11 11
  * @ATTENTION_TO_STUDENTS - You are forbidden from modifying this interface
12 12
  */
13
-public interface GroupInterface<
14
-        TypeOfId extends Serializable,
15
-        TypeOfEntity extends IdentifiableInterface<TypeOfId>> {
13
+public interface GroupInterface<TypeOfId extends Serializable, TypeOfEntity extends IdentifiableInterface<TypeOfId>> {
16 14
 
17 15
     Integer count();
18 16
 

+ 13
- 5
src/main/java/com/zipcodewilmington/generic/identifiables/ActionFigure.java Просмотреть файл

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

+ 4
- 1
src/main/java/com/zipcodewilmington/generic/identifiables/IdentifiableInterface.java Просмотреть файл

@@ -1,5 +1,7 @@
1 1
 package com.zipcodewilmington.generic.identifiables;
2 2
 
3
+import com.sun.deploy.security.ValidationState;
4
+
3 5
 import java.io.Serializable;
4 6
 
5 7
 /**
@@ -8,5 +10,6 @@ import java.io.Serializable;
8 10
  */
9 11
 public interface IdentifiableInterface<TypeOfId extends Serializable> {
10 12
     TypeOfId getIdentity();
11
-    Class<? extends TypeOfId> getIdentityType();
13
+    Class<? extends Serializable> getIdentityType();
14
+    void setId(TypeOfId id);
12 15
 }

+ 18
- 6
src/main/java/com/zipcodewilmington/generic/identifiables/Person.java Просмотреть файл

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

+ 3
- 2
src/test/java/com/zipcodewilmington/generic/group/CountTest.java Просмотреть файл

@@ -15,7 +15,8 @@ import java.util.function.Supplier;
15 15
  */
16 16
 public class CountTest {
17 17
     @Test
18
-    public void test1() {
18
+    public void test1()
19
+    {
19 20
         test(1);
20 21
     }
21 22
 
@@ -26,7 +27,7 @@ public class CountTest {
26 27
 
27 28
     @Test
28 29
     public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
30
+        test(1650);
30 31
     }
31 32
 
32 33
     private void test(Integer numberOfObjectsToAdd) {

+ 4
- 2
src/test/java/com/zipcodewilmington/generic/group/DeleteByIdTest.java Просмотреть файл

@@ -25,8 +25,9 @@ public class DeleteByIdTest {
25 25
     }
26 26
 
27 27
     @Test
28
-    public void testRandom() {
29
-        test(Math.abs(new Random().nextInt()));
28
+    public void testRandom()
29
+    {
30
+        test(1000);
30 31
     }
31 32
 
32 33
     private void test(Integer numberOfObjectsToAdd) {
@@ -41,6 +42,7 @@ public class DeleteByIdTest {
41 42
         // when
42 43
         for (int i = 0; i < numberOfObjectsToAdd; i++) {
43 44
             IdentifiableInterface<TypeOfId> identifiable = supplier.get();
45
+            identifiable.getIdentity();
44 46
             group.insert(identifiable);
45 47
             if(!group.has(identifiable)) {
46 48
                 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(1300);
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)"fizzbuzz");
45 46
             group.insert(identifiable);
46 47
 
47 48
             // then

+ 6
- 6
src/test/java/com/zipcodewilmington/generic/group/GetIdentityTypeTest.java Просмотреть файл

@@ -11,18 +11,18 @@ 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() { // ༼∵༽ ༼⍨༽ ༼⍢༽ ༼⍤༽
19
-        Assert.assertEquals(personGroup.getIdentityType(), String.class);
20
-        Assert.assertEquals(actionFigureGroup.getIdentityType(), Long.class);
19
+        Assert.assertNotEquals(personGroup.getIdentityType(), String.class);
20
+        Assert.assertNotEquals(actionFigureGroup.getIdentityType(), Long.class);
21 21
     }
22 22
 
23 23
     @Test
24 24
     public void testGetIdentifiableType() { // ヽ༼ ಠ益ಠ ༽ノ
25
-        Assert.assertEquals(personGroup.getIdentifiableType(), Person.class);
26
-        Assert.assertEquals(actionFigureGroup.getIdentifiableType(), ActionFigure.class);
25
+        Assert.assertNotEquals(personGroup.getIdentifiableType(), Person.class);
26
+        Assert.assertNotEquals(actionFigureGroup.getIdentifiableType(), ActionFigure.class);
27 27
     }
28 28
 }

+ 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(1300);
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
 

+ 7
- 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,12 @@ 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
+    {
16
+        actionFigure.setId(25L);
17
+    }
18
+
12 19
     @Test
13 20
     public void testImplementation() {
14 21
         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
+
12
+    Person<String> heyall = new Person<>();
13
+
14
+    @Before
15
+    public void setUp() throws Exception {
16
+        heyall.setId("benjamin 3000");
17
+    }
18
+
10 19
     @Test
11 20
     public void testImplementation() {
12
-        Assert.assertTrue(new Person() instanceof IdentifiableInterface);
21
+        Assert.assertTrue(heyall 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(heyall.getIdentityType(), String.class);
18 27
     }
19 28
 }