Trinh Tong 7 лет назад
Родитель
Сommit
ca319bd0cb

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

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.Type;
7
+import java.util.ArrayList;
6
 import java.util.List;
8
 import java.util.List;
7
 import java.util.function.Predicate;
9
 import java.util.function.Predicate;
10
+import java.util.stream.Collectors;
11
+import java.util.stream.Stream;
8
 
12
 
9
 /**
13
 /**
10
  * @author leon on 06/12/2018.
14
  * @author leon on 06/12/2018.
11
  */
15
  */
12
 public class Group<TypeOfId extends Serializable,
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
     @Override
25
     @Override
15
     public Integer count() {
26
     public Integer count() {
16
-        return null;
27
+        return group.size();
17
     }
28
     }
18
 
29
 
19
     @Override
30
     @Override
20
     public void insert(IdentifiableInterface object) {
31
     public void insert(IdentifiableInterface object) {
21
-
32
+        group.add((TypeOfEntity) object);
22
     }
33
     }
23
 
34
 
24
     @Override
35
     @Override
25
     public void delete(IdentifiableInterface object) {
36
     public void delete(IdentifiableInterface object) {
37
+        group.remove(object);
26
 
38
 
27
     }
39
     }
28
 
40
 
29
     @Override
41
     @Override
30
     public void delete(Serializable serializable) {
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
     @Override
50
     @Override
35
     public Boolean has(IdentifiableInterface object) {
51
     public Boolean has(IdentifiableInterface object) {
36
-        return null;
52
+        return group.contains(object);
37
     }
53
     }
38
 
54
 
39
     @Override
55
     @Override
40
     public Boolean has(Serializable serializable) {
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
     @Override
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
     @Override
71
     @Override

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

5
 /**
5
 /**
6
  * @author leon on 05/12/2018.
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
     private TypeOfId id;
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
         return id;
16
         return id;
14
     }
17
     }
15
 
18
 
16
-    public Class getIdentityType() {
19
+
20
+    public Class<? extends Serializable> getIdentityType() {
17
         return id.getClass();
21
         return id.getClass();
18
     }
22
     }
19
 }
23
 }

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

8
  */
8
  */
9
 public interface IdentifiableInterface<TypeOfId extends Serializable> {
9
 public interface IdentifiableInterface<TypeOfId extends Serializable> {
10
     TypeOfId getIdentity();
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
 package com.zipcodewilmington.generic.identifiables;
1
 package com.zipcodewilmington.generic.identifiables;
2
 
2
 
3
 import java.io.Serializable;
3
 import java.io.Serializable;
4
+import java.lang.reflect.ParameterizedType;
4
 
5
 
5
 /**
6
 /**
6
  * @author leon on 05/12/2018.
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
     private TypeOfId id;
10
     private TypeOfId id;
10
 
11
 
12
+    public Person() {
13
+    }
14
+
11
     @Override
15
     @Override
12
-    public Serializable getIdentity() {
16
+    public TypeOfId getIdentity() {
13
         return id;
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
 
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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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
 

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

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

+ 11
- 2
src/test/java/com/zipcodewilmington/generic/identifiables/PersonTest.java Просмотреть файл

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