001/*
002 * This file is part of the JDrupes non-blocking HTTP Codec
003 * Copyright (C) 2017 Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published
007 * by the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
013 * License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.httpcodec.types;
020
021import java.net.HttpCookie;
022import java.util.Collection;
023import java.util.Iterator;
024import java.util.LinkedHashMap;
025import java.util.Optional;
026import java.util.stream.Stream;
027
028/**
029 * Represents a list of cookies.
030 */
031public class CookieList implements Iterable<HttpCookie> {
032
033        private LinkedHashMap<String, HttpCookie> cookies;
034        
035        /**
036         * Creates a new empty cookie list.
037         */
038        public CookieList() {
039                cookies = new LinkedHashMap<>();
040        }
041
042        /**
043         * Creates a new list with items copied from the existing collection.
044         * 
045         * @param existing the existing collection
046         */
047        public CookieList(Collection<HttpCookie> existing) {
048                cookies = new LinkedHashMap<>();
049                for (HttpCookie cookie: existing) {
050                        cookies.put(cookie.getName(), cookie);
051                }
052        }
053
054        /**
055         * Returns the value for the cookie with the given name.
056         * 
057         * @param name the name
058         * @return the value if a cookie with the given name exists
059         */
060        public Optional<String> valueForName(String name) {
061                return Optional.ofNullable(cookies.get(name))
062                                .map(HttpCookie::getValue);
063        }
064
065        /**
066         * Adds a cookie to the list. If a cookie with the same name
067         * already exists, it is replaced.
068         * 
069         * @param cookie the cookie
070         * @return the cookie list for easy chaining
071         */
072        public CookieList add(HttpCookie cookie) {
073                cookies.remove(cookie.getName());
074                cookies.put(cookie.getName(), cookie);
075                return this;
076        }
077
078        /**
079         * Removes all cookies from the list.
080         * 
081         * @return the cookie list for easy chaining
082         */
083        public CookieList clear() {
084                cookies.clear();
085                return this;
086        }
087
088        public boolean isEmpty() {
089                return cookies.isEmpty();
090        }
091
092        /* (non-Javadoc)
093         * @see java.util.List#iterator()
094         */
095        @Override
096        public Iterator<HttpCookie> iterator() {
097                return cookies.values().iterator();
098        }
099
100        public Stream<HttpCookie> stream() {
101                return cookies.values().stream();
102        }
103        
104        /**
105         * Remove the cookie with the given name.
106         * 
107         * @param name
108         * @return the cookie list for easy chaining
109         */
110        public boolean remove(String name) {
111                return false;
112        }
113
114        public int size() {
115                return cookies.size();
116        }
117
118}