All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mike10004.seleniumhelp.MultimapCookieCollection Maven / Gradle / Ivy

There is a newer version: 0.58
Show newest version
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> comparatorFactory) {
        requireNonNull(comparatorFactory, "comparator");
        ImmutableList.Builder b = ImmutableList.builder();
        cookies.asMap().forEach((key, cookieList) -> {
            Comparator 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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy