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.util.ArrayList;
022import java.util.Arrays;
023import java.util.Collection;
024
025/**
026 * Represents a list of strings with some additional methods.
027 */
028@SuppressWarnings("serial")
029public class StringList extends ArrayList<String> {
030
031        /**
032         * Creates a new empty list of strings.
033         */
034        public StringList() {
035        }
036
037        /**
038         * Creates a new empty list of strings with an initial capacity.
039         * 
040         * @param initialCapacity the capacity
041         */
042        public StringList(int initialCapacity) {
043                super(initialCapacity);
044        }
045
046        /**
047         * Creates a new list with items copied from the existing collection.
048         * 
049         * @param existing the existing collection
050         */
051        public StringList(Collection<String> existing) {
052                super(existing);
053        }
054
055        /**
056         * Creates a new list with given items.
057         * 
058         * @param item first item
059         * @param items more items
060         */
061        public StringList(String item, String ... items) {
062                super();
063                add(item);
064                addAll(Arrays.asList(items));
065        }
066
067        /**
068         * Returns whether the list contains the given value, ignoring
069         * differences in the cases of the letters.
070         * 
071         * @param value the value to compare with
072         * @return the result
073         */
074        public boolean containsIgnoreCase(String value) {
075                for (String s: this) {
076                        if (s.equalsIgnoreCase(value)) {
077                                return true;
078                        }
079                }
080                return false;
081        }
082        
083        /**
084         * Removes all strings equal to the given value, ignoring
085         * differences in the cases of the letters.
086         * 
087         * @param value the value to compare with
088         */
089        public void removeIgnoreCase(String value) {
090                removeIf(s -> s.equalsIgnoreCase(value));
091        }
092        
093        /**
094         * Appends the value to the list of values if it is not already in the list.
095         * 
096         * @param value the value
097         * @return the field
098         */
099        public StringList appendIfNotContained(String value) {
100                if (!contains(value)) {
101                        add(value);
102                }
103                return this;
104        }
105        
106}