mpierse hace 6 años
padre
commit
1edbe4b857

+ 38
- 13
src/main/java/com/zipcodewilmington/generic/group/Group.java Ver fichero

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

+ 22
- 4
src/main/java/com/zipcodewilmington/generic/identifiables/ActionFigure.java Ver fichero

@@ -5,14 +5,32 @@ 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 implements IdentifiableInterface<Long> {
9
+
10
+    Long id;
11
+
12
+    public ActionFigure() {
13
+        this.id =  (long) (Math.random() * 100L);;
14
+    }
15
+
9 16
 
10 17
     @Override
11
-    public Serializable getIdentity() {
12
-        return null;
18
+    public Long getIdentity() {
19
+        return id;
13 20
     }
14 21
 
15 22
     public Class getIdentityType() {
16
-        return null;
23
+        return id.getClass();
24
+    }
25
+
26
+    @Override
27
+    public void setId(Long aLong) {
28
+
17 29
     }
30
+
31
+//    @Override
32
+//    public void setId() {
33
+//        this.id = (long) (Math.random() * 100L);
34
+//    }
35
+
18 36
 }

+ 1
- 0
src/main/java/com/zipcodewilmington/generic/identifiables/IdentifiableInterface.java Ver fichero

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

+ 27
- 4
src/main/java/com/zipcodewilmington/generic/identifiables/Person.java Ver fichero

@@ -1,18 +1,41 @@
1 1
 package com.zipcodewilmington.generic.identifiables;
2 2
 
3 3
 import java.io.Serializable;
4
+import java.nio.charset.Charset;
5
+import java.util.Random;
4 6
 
5 7
 /**
6 8
  * @author leon on 05/12/2018.
7 9
  */
8
-public class Person implements IdentifiableInterface {
10
+public class Person implements IdentifiableInterface<String> {
11
+
12
+    String id;
13
+
14
+    public Person() {
15
+        this.id = "string";
16
+    }
17
+
18
+
19
+    private String randomString(){
20
+        byte[] array = new byte[7]; // length is bounded by 7
21
+        new Random().nextBytes(array);
22
+        String generatedString = new String(array, Charset.forName("UTF-8"));
23
+        return generatedString;
24
+    }
9 25
 
10 26
     @Override
11
-    public Serializable getIdentity() {
12
-        return null;
27
+    public String getIdentity() {
28
+        return id;
13 29
     }
14 30
 
15 31
     public Class getIdentityType() {
16
-        return null;
32
+        System.out.println(id.getClass());
33
+        return id.getClass();
17 34
     }
35
+
36
+    @Override
37
+    public void setId(String id) {
38
+        this.id = id;
39
+    }
40
+
18 41
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/generic/group/CountTest.java Ver fichero

@@ -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(Math.abs(104543));
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {

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

@@ -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(Math.abs(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 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(Math.abs(3));
30 30
     }
31 31
 
32 32
     private void test(Integer numberOfObjectsToAdd) {