Skip to content

Commit 7fe9917

Browse files
committed
#8 codereview
1 parent 9bfc8f8 commit 7fe9917

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/main/java/org/cactoos/http/HtCookies.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
*/
2424
package org.cactoos.http;
2525

26-
import java.util.HashMap;
27-
import java.util.Map;
26+
import java.util.Iterator;
2827
import org.cactoos.Input;
2928
import org.cactoos.Text;
29+
import org.cactoos.iterable.LengthOf;
30+
import org.cactoos.list.Mapped;
31+
import org.cactoos.map.MapEntry;
3032
import org.cactoos.map.MapEnvelope;
33+
import org.cactoos.map.MapOf;
3134
import org.cactoos.text.SplitText;
3235

3336
/**
@@ -51,20 +54,23 @@ public final class HtCookies extends MapEnvelope<String, String> {
5154
* @param rsp Response
5255
*/
5356
public HtCookies(final Input rsp) {
54-
super(() -> {
55-
final Map<String, String> result = new HashMap<>();
56-
final String cookie = new HtHeaders(rsp).get("set-cookie");
57-
for (final Text item : new SplitText(cookie, ";\\s+")) {
58-
final String[] entry = item.asString().split("=", 2);
59-
if (entry.length == 2) {
60-
result.put(entry[0], entry[1]);
61-
} else {
62-
throw new IllegalArgumentException(
63-
"Incorrect HTTP Response cookie"
57+
super(() -> new MapOf<>(
58+
new Mapped<>(
59+
entry -> {
60+
final Iterable<Text> parts = new SplitText(entry, "=");
61+
if (new LengthOf(parts).intValue() != 2) {
62+
throw new IllegalArgumentException(
63+
"Incorrect HTTP Response cookie"
64+
);
65+
}
66+
final Iterator<Text> iter = parts.iterator();
67+
return new MapEntry<>(
68+
iter.next().asString(),
69+
iter.next().asString()
6470
);
65-
}
66-
}
67-
return result;
68-
});
71+
},
72+
new SplitText(new HtHeaders(rsp).get("set-cookie"), ";\\s+")
73+
)
74+
));
6975
}
7076
}

0 commit comments

Comments
 (0)