Eric Cordell 6 年 前
コミット
4f0281963a
共有2 個のファイルを変更した62 個の追加37 個の削除を含む
  1. 27
    2
      src/main/java/ArrayListCombiner/ArrayListCombiner.java
  2. 35
    35
      src/test/java/ArrayListCombiner/ArrayListCombinerTest.java

+ 27
- 2
src/main/java/ArrayListCombiner/ArrayListCombiner.java ファイルの表示

@@ -1,12 +1,37 @@
1 1
 package ArrayListCombiner;
2 2
 
3
+import Employee.Employee;
4
+
3 5
 import java.util.ArrayList;
4 6
 
5 7
 /**
6
- * Create two generic methods that take two arraylists.  The methods should both append the second ArrayList's items,
7
- * to the first.  Use a wildcard for one of the type arguments in each method.
8
+ * Create two generic methods that take two arraylists.
9
+ *
10
+ * The methods should both append the second ArrayList's items, to the first.
11
+ * Use a wildcard for one of the type arguments in each method.
12
+ *
8 13
  * The first method should be called extendCombiner and should use ? extends E
14
+ *
9 15
  * The second method should be called superCombiner and should use ? super E
10 16
  */
11 17
 public class ArrayListCombiner {
18
+
19
+
20
+    //public static void extendCombiner(ArrayList<E> first, ArrayList<E> second)
21
+
22
+    // ? extends guaranteeing it's passing in correct type
23
+    public static <E> void extendCombiner(ArrayList<E> first, ArrayList<? extends E> second) {
24
+        first.addAll(second);
25
+    }
26
+
27
+
28
+    public static <E> void superCombiner(ArrayList<? super E> first, ArrayList<E> second) {
29
+        first.addAll(second);
30
+    }
31
+
32
+
33
+
12 34
 }
35
+
36
+
37
+

+ 35
- 35
src/test/java/ArrayListCombiner/ArrayListCombinerTest.java ファイルの表示

@@ -9,40 +9,40 @@ import org.junit.Assert;
9 9
 import java.util.ArrayList;
10 10
 
11 11
 public class ArrayListCombinerTest {
12
-//    Employee foo = new Employee("FOO", 100);
13
-//    Manager bar = new Manager("BAR", 100);
14
-//    @Test
15
-//    public void testExtendCombiner() throws Exception {
16
-//        // Given an array list with employees
17
-//        ArrayList<Employee> first = new ArrayList<>();
18
-//        first.add(foo);
19
-//        // An an array list with managers
20
-//        ArrayList<Manager> second = new ArrayList<>();
21
-//        second.add(bar);
22
-//        // When  I combine them
23
-//        ArrayListCombiner.extendCombiner(first, second);
24
-//        // Then I should get an arrayList with both
25
-//        ArrayList<Employee> expected = new ArrayList<>();
26
-//        expected.add(foo);
27
-//        expected.add(bar);
28
-//        Assert.assertEquals(expected, first);
29
-//    }
30
-//
31
-//    @Test
32
-//    public void testSuperCombiner() throws Exception {
33
-//        // Given an array list with employees
34
-//        ArrayList<Employee> first = new ArrayList<>();
35
-//        first.add(foo);
36
-//        // An an array list with managers
37
-//        ArrayList<Manager> second = new ArrayList<>();
38
-//        second.add(bar);
39
-//        // When  I combine them
40
-//        ArrayListCombiner.superCombiner(first, second);
41
-//        // Then I should get an arrayList with both
42
-//        ArrayList<Employee> expected = new ArrayList<>();
43
-//        expected.add(foo);
44
-//        expected.add(bar);
45
-//        Assert.assertEquals(expected, first);
46
-//    }
12
+    Employee foo = new Employee("FOO", 100);
13
+    Manager bar = new Manager("BAR", 100);
14
+    @Test
15
+    public void testExtendCombiner() throws Exception {
16
+        // Given an array list with employees
17
+        ArrayList<Employee> first = new ArrayList<>();
18
+        first.add(foo);
19
+        // An an array list with managers
20
+        ArrayList<Manager> second = new ArrayList<>();
21
+        second.add(bar);
22
+        // When  I combine them
23
+        ArrayListCombiner.extendCombiner(first, second);
24
+        // Then I should get an arrayList with both
25
+        ArrayList<Employee> expected = new ArrayList<>();
26
+        expected.add(foo);
27
+        expected.add(bar);
28
+        Assert.assertEquals(expected, first);
29
+    }
30
+
31
+    @Test
32
+    public void testSuperCombiner() throws Exception {
33
+        // Given an array list with employees
34
+        ArrayList<Employee> first = new ArrayList<>();
35
+        first.add(foo);
36
+        // An an array list with managers
37
+        ArrayList<Manager> second = new ArrayList<>();
38
+        second.add(bar);
39
+        // When  I combine them
40
+        ArrayListCombiner.superCombiner(first, second);
41
+        // Then I should get an arrayList with both
42
+        ArrayList<Employee> expected = new ArrayList<>();
43
+        expected.add(foo);
44
+        expected.add(bar);
45
+        Assert.assertEquals(expected, first);
46
+    }
47 47
 
48 48
 }