private void addCookies(Iterable<Cookie> newCookies, AsciiString cookieHeaderName,
                            Function<Iterable<? extends Cookie>, Object> toCookiesString) {
        @SuppressWarnings("unchecked")
        Iterable<Cookie> cachedCookies = (Iterable<Cookie>) cache.get(cookieHeaderName);
        if (cachedCookies == null) {
            if (newCookies instanceof Cookies) {
                cachedCookies = newCookies;
            } else {
                final ArrayList<Cookie> copied = new ArrayList<>(4);
                for (Cookie cookie : newCookies) {
                    copied.add(cookie);
                }
                cachedCookies = copied;
            }
        } else {
            // Append 'newCookies' to 'cachedCookies'
            if (cachedCookies instanceof Cookies) {
                // Make cached Cookies mutable
                cachedCookies = new ArrayList<>((Collection<Cookie>) cachedCookies);
            }
            assert cachedCookies instanceof ArrayList;
            final ArrayList<Cookie> cookieList = (ArrayList<Cookie>) cachedCookies;
            for (Cookie cookie : newCookies) {
                if (!cookieList.contains(cookie)) {
                    // Add only a non-duplicate Cookie.
                    cookieList.add(cookie);
                }
            }
        }
        // Cache mutable cookies for efficiency.
        // The mutable cookies will be changed into (immutable) Cookies when cookie() is called.
        cache.put(cookieHeaderName, cachedCookies);

        if (HttpHeaderNames.COOKIE.equals(cookieHeaderName)) {
            // Stringify all cookies
            final Object cookiesString = toCookiesString.apply(cachedCookies);
            assert cookiesString instanceof String;
            assert cookieHeaderName.equals(HttpHeaderNames.COOKIE);
            setWithoutNotifying(cookieHeaderName, (String) cookiesString);
        } else if (HttpHeaderNames.SET_COOKIE.equals(cookieHeaderName)) {
            // Only stringify new cookies
            final Object cookiesString = toCookiesString.apply(newCookies);
            assert cookiesString instanceof Iterable;
            //noinspection unchecked
            addWithoutNotifying(cookieHeaderName, (Iterable<String>) cookiesString);
        } else {
            throw new Error(); // Should never reach here.
        }
    }
Adds the specified {@code setCookies} with {@link HttpHeaderNames#SET_COOKIE}.
core/src/main/java/com/linecorp/armeria/common/HttpHeadersBase.java