2 Commits

Autor SHA1 Mensaje Fecha
  jacob andersen 181c4c0d55 methods written, tests passing hace 6 años
  Leon 4aa89c5ce4 added methods and testing for `GroupInterface.getIdentityType()` and `GroupInterface.getIdentifiableType()` hace 6 años

+ 1
- 1
pom.xml Ver fichero

@@ -5,7 +5,7 @@
5 5
     <modelVersion>4.0.0</modelVersion>
6 6
 
7 7
     <groupId>com.zipcodewilmington</groupId>
8
-    <artifactId>quiz2</artifactId>
8
+    <artifactId>genericslab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11 11
     <properties>

+ 1
- 4
src/main/java/com/zipcodewilmington/generic/group/AbstractGroup.java Ver fichero

@@ -7,9 +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
-
15 12
 }

+ 51
- 11
src/main/java/com/zipcodewilmington/generic/group/Group.java Ver fichero

@@ -3,45 +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;
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;
14
+public class Group<TypeOfId extends Serializable, TypeOfEntity extends IdentifiableInterface<TypeOfId>> extends AbstractGroup<TypeOfId, TypeOfEntity>
15
+{
16
+
17
+    List<TypeOfEntity>list;
18
+
19
+    public Group() {
20
+        list = new ArrayList<>();
16 21
     }
17 22
 
23
+
18 24
     @Override
19
-    public void insert(IdentifiableInterface object) {
25
+    public Integer count()
26
+    {
27
+        return list.size();
28
+    }
20 29
 
30
+    @Override
31
+    public void insert(IdentifiableInterface object)
32
+    {
33
+    list.add((TypeOfEntity)object);
21 34
     }
22 35
 
23 36
     @Override
24
-    public void delete(IdentifiableInterface object) {
37
+    public void delete(IdentifiableInterface object)
38
+    {
39
+    list.remove(object);
40
+    }
25 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
+       }
26 50
     }
27 51
 
28 52
     @Override
29
-    public void delete(Serializable serializable) {
53
+    public Boolean has(IdentifiableInterface object)
54
+    {
55
+        return list.contains(object);
56
+    }
30 57
 
58
+    @Override
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;
31 67
     }
32 68
 
33 69
     @Override
34
-    public Boolean has(IdentifiableInterface object) {
35
-        return null;
70
+    public List<TypeOfEntity> filter(Predicate<TypeOfEntity> predicate)
71
+    {
72
+        return list.stream().filter(predicate).collect(Collectors.toList());
36 73
     }
37 74
 
38 75
     @Override
39
-    public Boolean has(Serializable serializable) {
76
+    public Class getIdentityType()
77
+    {
40 78
         return null;
41 79
     }
42 80
 
43 81
     @Override
44
-    public List filter(Predicate predicate) {
82
+    public Class getIdentifiableType()
83
+    {
45 84
         return null;
46 85
     }
86
+
47 87
 }

+ 13
- 11
src/main/java/com/zipcodewilmington/generic/group/GroupInterface.java Ver fichero

@@ -10,21 +10,23 @@ 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
-   Integer count();
15
+    Integer count();
18 16
 
19
-   void insert(TypeOfEntity object);
20
-    
21
-   void delete(TypeOfEntity object);
17
+    void insert(TypeOfEntity object);
22 18
 
23
-   void delete(TypeOfId id);
19
+    void delete(TypeOfEntity object);
24 20
 
25
-   Boolean has(TypeOfEntity object);
21
+    void delete(TypeOfId id);
26 22
 
27
-   Boolean has(TypeOfId id);
23
+    Boolean has(TypeOfEntity object);
28 24
 
29
-   List<TypeOfEntity> filter(Predicate<TypeOfEntity> predicate);
25
+    Boolean has(TypeOfId id);
26
+
27
+    List<TypeOfEntity> filter(Predicate<TypeOfEntity> predicate);
28
+
29
+    Class<? extends TypeOfId> getIdentityType();
30
+
31
+    Class<? extends TypeOfEntity> getIdentifiableType();
30 32
 }

+ 13
- 5
src/main/java/com/zipcodewilmington/generic/identifiables/ActionFigure.java Ver fichero

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

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

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

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

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

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

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

+ 28
- 0
src/test/java/com/zipcodewilmington/generic/group/GetIdentityTypeTest.java Ver fichero

@@ -0,0 +1,28 @@
1
+package com.zipcodewilmington.generic.group;
2
+
3
+import com.zipcodewilmington.generic.identifiables.ActionFigure;
4
+import com.zipcodewilmington.generic.identifiables.Person;
5
+import org.junit.Assert;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * @author leon on 06/12/2018.
10
+ * there is meaning to the madness
11
+ * ( ͡☉ ͜ʖ ͡☉)
12
+ */
13
+public class GetIdentityTypeTest {
14
+    private Group<String, Person<String>> personGroup = new Group<>();
15
+    private Group<Long, ActionFigure<Long>> actionFigureGroup = new Group<>();
16
+
17
+    @Test
18
+    public void testGetIdentityType() { // ༼∵༽ ༼⍨༽ ༼⍢༽ ༼⍤༽
19
+        Assert.assertNotEquals(personGroup.getIdentityType(), String.class);
20
+        Assert.assertNotEquals(actionFigureGroup.getIdentityType(), Long.class);
21
+    }
22
+
23
+    @Test
24
+    public void testGetIdentifiableType() { // ヽ༼ ಠ益ಠ ༽ノ
25
+        Assert.assertNotEquals(personGroup.getIdentifiableType(), Person.class);
26
+        Assert.assertNotEquals(actionFigureGroup.getIdentifiableType(), ActionFigure.class);
27
+    }
28
+}

+ 2
- 2
src/test/java/com/zipcodewilmington/generic/group/HasByIdTest.java Ver fichero

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

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

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

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

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