ソースを参照

Generic class for functions.

pull/22/head
Greg Holmes 9年前
コミット
d51ed35a4e

src/com/dmdirc/util/collections/URLEncodingMapFlattener.java → src/com/dmdirc/util/collections/CollectionFunctions.java ファイルの表示

@@ -25,17 +25,17 @@ package com.dmdirc.util.collections;
25 25
 import java.io.UnsupportedEncodingException;
26 26
 import java.net.URLEncoder;
27 27
 import java.util.Map;
28
-import java.util.function.Function;
29 28
 import java.util.stream.Stream;
30 29
 
31 30
 /**
32
- * Flattens a map into a key=value pair, URLEncoding both the key and the entry.
31
+ * Collection related functions.
33 32
  */
34
-public class URLEncodingMapFlattener
35
-        implements Function<Map.Entry<String, String>, Stream<String>> {
33
+public final class CollectionFunctions {
36 34
 
37
-    @Override
38
-    public Stream<String> apply(final Map.Entry<String, String> entry) {
35
+    private CollectionFunctions() {
36
+    }
37
+
38
+    public static Stream<String> flattenAndEncodeKeyPair(final Map.Entry<String, String> entry) {
39 39
         String key;
40 40
         String value;
41 41
         try {

+ 2
- 2
src/com/dmdirc/util/io/Downloader.java ファイルの表示

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.util.io;
24 24
 
25
-import com.dmdirc.util.collections.URLEncodingMapFlattener;
25
+import com.dmdirc.util.collections.CollectionFunctions;
26 26
 
27 27
 import java.io.BufferedReader;
28 28
 import java.io.DataOutputStream;
@@ -82,7 +82,7 @@ public class Downloader {
82 82
     public List<String> getPage(final String url,
83 83
             final Map<String, String> postData) throws IOException {
84 84
         return getPage(url, postData.entrySet().stream()
85
-                .flatMap(e -> new URLEncodingMapFlattener().apply(e))
85
+                .flatMap(CollectionFunctions::flattenAndEncodeKeyPair)
86 86
                 .collect(Collectors.joining("&")));
87 87
     }
88 88
 

test/com/dmdirc/util/collections/URLEncodingMapFlattenerTest.java → test/com/dmdirc/util/collections/CollectionFunctionsTest.java ファイルの表示

@@ -28,32 +28,20 @@ import java.util.List;
28 28
 import java.util.Map;
29 29
 import java.util.stream.Collectors;
30 30
 
31
-import org.junit.Before;
32 31
 import org.junit.Test;
33
-import org.junit.runner.RunWith;
34
-import org.mockito.runners.MockitoJUnitRunner;
35 32
 
36 33
 import static junit.framework.TestCase.assertEquals;
37 34
 
38
-@RunWith(MockitoJUnitRunner.class)
39
-public class URLEncodingMapFlattenerTest {
35
+public class CollectionFunctionsTest {
40 36
 
41
-    private URLEncodingMapFlattener instance;
42
-    private Map<String, String> map;
43
-
44
-    @Before
45
-    public void setUp() throws Exception {
46
-        instance = new URLEncodingMapFlattener();
47
-        map = ImmutableMap.<String, String>builder()
37
+    @Test
38
+    public void testflattenAndEncodeKeyPair() throws Exception {
39
+        final Map<String, String> map = ImmutableMap.<String, String>builder()
48 40
                 .put("key", "value")
49 41
                 .put("key&", "value&")
50 42
                 .build();
51
-    }
52
-
53
-    @Test
54
-    public void testApply() throws Exception {
55 43
         final List<String> result = map.entrySet().stream()
56
-                .flatMap(instance)
44
+                .flatMap(CollectionFunctions::flattenAndEncodeKeyPair)
57 45
                 .collect(Collectors.toList());
58 46
         assertEquals(2, result.size());
59 47
         assertEquals("key=value", result.get(0));

読み込み中…
キャンセル
保存