#8 trtong - deletedDemDupes with generics

Open
trtong wants to merge 4 commits from trtong/CR-MesoLabs-OOP-DuplicateDeleter:master into master

+ 62
- 0
src/main/java/com/zipcodewilmington/looplabs/ArrayUtils.java View File

@@ -0,0 +1,62 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+import java.lang.reflect.Array;
4
+import java.util.Arrays;
5
+
6
+public class ArrayUtils<T> {
7
+
8
+    private T[] content;
9
+
10
+    public ArrayUtils(T[] content) {
11
+        this.content = content;
12
+    }
13
+
14
+    public T[] removeDuplicates(T[] arr, T value) {
15
+        int countOfValue = countValues(arr, value);
16
+        int index = 0;
17
+        T[] arr2 =  (T[]) Array.newInstance(content.getClass().getComponentType(), arr.length - countOfValue);
18
+        for (T i: arr
19
+             ) {
20
+            if (!i.equals(value)) {
21
+                arr2[index++] = i;
22
+            }
23
+        }
24
+        return arr2;
25
+    }
26
+
27
+    public int countValues(T[] arr, T value) {
28
+        int countOfValue = 0;
29
+
30
+        for (T t: arr
31
+        ) {
32
+            if (t.equals(value)) {
33
+                countOfValue++;
34
+            }
35
+        }
36
+        return countOfValue;
37
+    }
38
+
39
+    public T[] removeDupes(T[] arr, int maxNumberOfDuplications) {
40
+        T[] arr2 = Arrays.copyOf(arr, arr.length);
41
+        for (T i: arr2) {
42
+            if ((countValues(arr2, i) >= maxNumberOfDuplications)) {
43
+                arr2 = removeDuplicates(arr2, i);
44
+            }
45
+        }
46
+        return arr2;
47
+    }
48
+
49
+    public T[] removeDupesExactly(T[] arr, int exactNumberOfDuplications) {
50
+
51
+        T[] arr2 = Arrays.copyOf(arr, arr.length);
52
+        for (T i: arr2
53
+             ) {
54
+            if ((countValues(arr2, i) == exactNumberOfDuplications)) {
55
+                arr2 = removeDuplicates(arr2, i);
56
+            }
57
+        }
58
+
59
+        return arr2;
60
+    }
61
+
62
+}

+ 18
- 0
src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java View File

@@ -1,8 +1,26 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * Created by leon on 1/29/18.
5 7
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6 8
  */
7 9
 public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
10
+
11
+    private ArrayUtils<Integer> arrayUtils;
12
+    public IntegerDuplicateDeleter(Integer[] intArray) {
13
+        super(intArray);
14
+         arrayUtils = new ArrayUtils<>(intArray);
15
+    }
16
+
17
+    @Override
18
+    public Integer[] removeDuplicates(int maxNumberOfDuplications) {
19
+        return arrayUtils.removeDupes(super.array, maxNumberOfDuplications);
20
+    }
21
+
22
+    @Override
23
+    public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications) {
24
+        return arrayUtils.removeDupesExactly(super.array, exactNumberOfDuplications);
25
+    }
8 26
 }

+ 17
- 0
src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java View File

@@ -1,8 +1,25 @@
1 1
 package com.zipcodewilmington.looplabs;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * Created by leon on 1/28/18.
5 7
  * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6 8
  */
7 9
 public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
10
+    ArrayUtils<String> arrayUtils;
11
+    public StringDuplicateDeleter(String[] intArray) {
12
+        super(intArray);
13
+        arrayUtils = new ArrayUtils<>(intArray);
14
+    }
15
+
16
+    @Override
17
+    public String[] removeDuplicates(int maxNumberOfDuplications) {
18
+        return arrayUtils.removeDupes(super.array, maxNumberOfDuplications);
19
+    }
20
+
21
+    @Override
22
+    public String[] removeDuplicatesExactly(int exactNumberOfDuplications) {
23
+        return arrayUtils.removeDupesExactly(super.array, exactNumberOfDuplications);
24
+    }
8 25
 }