com.github.mike10004.seleniumhelp.MultimapCookieCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-capture Show documentation
Show all versions of selenium-capture Show documentation
Capture HTTP traffic generated by Selenium WebDriver in Java
package com.github.mike10004.seleniumhelp;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import java.util.Comparator;
import java.util.function.Function;
import static java.util.Objects.requireNonNull;
class MultimapCookieCollection implements CookieCollection {
private final ImmutableMultimap cookies;
private MultimapCookieCollection(Multimap cookiesByEntry) {
this.cookies = ImmutableMultimap.copyOf(cookiesByEntry);
}
static CookieCollection build(Iterable cookies) {
return new MultimapCookieCollection(buildCookieKeyMultimap(cookies));
}
@Override
public ImmutableList makeCookieList(Function super CookieKey, Comparator super DeserializableCookie>> comparatorFactory) {
requireNonNull(comparatorFactory, "comparator");
ImmutableList.Builder b = ImmutableList.builder();
cookies.asMap().forEach((key, cookieList) -> {
Comparator super DeserializableCookie> comparator = comparatorFactory.apply(key);
b.add(Ordering.from(comparator).max(cookieList));
});
return b.build();
}
private static Multimap buildCookieKeyMultimap(Iterable cookies) {
Multimap updatingMap = ArrayListMultimap.create();
cookies.forEach(cookie -> {
CookieKey key = CookieKey.from(cookie);
updatingMap.put(key, cookie);
});
return updatingMap;
}
}