Преглед изворни кода

all but the reflection piece

NedRedmond пре 6 година
родитељ
комит
e44574e830

+ 34
- 20
src/main/java/com/zipcodewilmington/generic/group/Group.java Прегледај датотеку

@@ -1,10 +1,11 @@
1 1
 package com.zipcodewilmington.generic.group;
2 2
 
3 3
 import com.zipcodewilmington.generic.identifiables.IdentifiableInterface;
4
+import com.zipcodewilmington.generic.identifiables.Person;
4 5
 
5 6
 import java.io.Serializable;
7
+import java.lang.reflect.Field;
6 8
 import java.lang.reflect.ParameterizedType;
7
-import java.lang.reflect.Type;
8 9
 import java.util.ArrayList;
9 10
 import java.util.List;
10 11
 import java.util.function.Predicate;
@@ -18,46 +19,44 @@ public class Group<
18 19
         TypeOfEntity extends IdentifiableInterface<TypeOfId>>
19 20
         extends AbstractGroup<TypeOfId, TypeOfEntity> {
20 21
 
21
-    List<TypeOfEntity> group = new ArrayList<>();
22
+    List<TypeOfEntity> group;
23
+
24
+    public Group() {
25
+        this.group = new ArrayList<>();
26
+    }
22 27
 
23 28
     @Override
24 29
     public Integer count() {
25
-        return group.size();
30
+        return this.group.size();
26 31
     }
27 32
 
28 33
     @Override
29 34
     public void insert(TypeOfEntity object) {
30
-        group.add(object);
35
+        this.group.add(object);
31 36
     }
32 37
 
33 38
     @Override
34 39
     public void delete(TypeOfEntity object) {
35
-        group.remove(object);
40
+        this.group.remove(object);
36 41
     }
37 42
 
38 43
     @Override
39 44
     public void delete(Serializable serializable) {
40
-        for (IdentifiableInterface i : group) {
41
-            if (serializable instanceof Number) {
42
-                if (i.getIdentity() == serializable) {
43
-                    group.remove(i);
44
-                }
45
-            } else {
46
-                if (i.getIdentity().equals(serializable)) {
47
-                    group.remove(i);
48
-                }
45
+        while(this.group.iterator().hasNext()) {
46
+            if (this.group.iterator().next().getIdentity() == serializable) {
47
+                this.group.remove(this.group.iterator().next());
49 48
             }
50 49
         }
51 50
     }
52 51
 
53 52
     @Override
54 53
     public Boolean has(IdentifiableInterface object) {
55
-        return group.contains(object);
54
+        return this.group.contains(object);
56 55
     }
57 56
 
58 57
     @Override
59 58
     public Boolean has(Serializable serializable) {
60
-            for (IdentifiableInterface i : group) {
59
+            for (IdentifiableInterface i : this.group) {
61 60
                 if (serializable instanceof Number) {
62 61
                     if (i.getIdentity() == serializable) {
63 62
                         return true;
@@ -74,17 +73,32 @@ public class Group<
74 73
 
75 74
     @Override
76 75
     public List filter(Predicate<TypeOfEntity> predicate) {
77
-        return group.stream().filter(predicate).collect(Collectors.toList());
76
+        return this.group.stream().filter(predicate).collect(Collectors.toList());
78 77
     }
79 78
 
80 79
     @Override
81 80
     public Class getIdentityType() {
82
-        return group.get(0).getIdentityType();
81
+        try {
82
+            Field groupField = Group.class.getDeclaredField("group");
83
+            ParameterizedType groupType = (ParameterizedType) groupField.getGenericType();
84
+            Class<?> groupClass = (Class<?>) groupType.getActualTypeArguments()[0];
85
+            return groupClass;
86
+        } catch (NoSuchFieldException e) {
87
+            e.printStackTrace();
88
+        }
89
+        return null;
83 90
     }
84 91
 
85 92
     @Override
86 93
     public Class getIdentifiableType() {
87
-        return group.get(0).getClass();
88
-    }
94
+        try {
95
+            Field groupField = Group.class.getDeclaredField("group");
96
+            ParameterizedType groupType = (ParameterizedType) groupField.getGenericType();
97
+            Class<?> groupClass = (Class<?>) groupType.getActualTypeArguments()[0];
98
+            return groupClass;
99
+        } catch (NoSuchFieldException e) {
100
+            e.printStackTrace();
101
+        }
102
+        return null;    }
89 103
 
90 104
 }

+ 4
- 0
src/main/java/com/zipcodewilmington/generic/identifiables/ActionFigure.java Прегледај датотеку

@@ -6,6 +6,10 @@ import java.io.Serializable;
6 6
  * @author leon on 05/12/2018.
7 7
  */
8 8
 public class ActionFigure implements IdentifiableInterface<Long> {
9
+    public ActionFigure() {
10
+        identity = 999L;
11
+    }
12
+
9 13
     private Long identity;
10 14
 
11 15
     @Override

+ 4
- 0
src/main/java/com/zipcodewilmington/generic/identifiables/Person.java Прегледај датотеку

@@ -6,6 +6,10 @@ import java.io.Serializable;
6 6
  * @author leon on 05/12/2018.
7 7
  */
8 8
 public class Person implements IdentifiableInterface<String> {
9
+    public Person() {
10
+        identity = "foo";
11
+    }
12
+
9 13
     private String identity;
10 14
 
11 15
     @Override

+ 1
- 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(999);
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {

+ 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(999);
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {

+ 1
- 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(999);
30 30
     }
31 31
 
32 32
 

+ 1
- 0
src/test/java/com/zipcodewilmington/generic/group/GetIdentityTypeTest.java Прегледај датотеку

@@ -11,6 +11,7 @@ import org.junit.Test;
11 11
  * ( ͡☉ ͜ʖ ͡☉)
12 12
  */
13 13
 public class GetIdentityTypeTest {
14
+
14 15
     private Group<String, Person> personGroup = new Group<>();
15 16
     private Group<Long, ActionFigure> actionFigureGroup = new Group<>();
16 17
 

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