001package ezvcard.property;
002
003import java.util.ArrayList;
004import java.util.LinkedHashMap;
005import java.util.List;
006import java.util.Map;
007
008import ezvcard.VCard;
009import ezvcard.VCardVersion;
010import ezvcard.ValidationWarning;
011
012/*
013 Copyright (c) 2012-2023, Michael Angstadt
014 All rights reserved.
015
016 Redistribution and use in source and binary forms, with or without
017 modification, are permitted provided that the following conditions are met: 
018
019 1. Redistributions of source code must retain the above copyright notice, this
020 list of conditions and the following disclaimer. 
021 2. Redistributions in binary form must reproduce the above copyright notice,
022 this list of conditions and the following disclaimer in the documentation
023 and/or other materials provided with the distribution. 
024
025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
035
036 The views and conclusions contained in the software and documentation are those
037 of the authors and should not be interpreted as representing official policies, 
038 either expressed or implied, of the FreeBSD Project.
039 */
040
041/**
042 * Represents a property whose value is a list of textual values.
043 * @author Michael Angstadt
044 * @param <T> the type of values stored in the list
045 */
046public class ListProperty<T> extends VCardProperty {
047        protected final List<T> values;
048
049        public ListProperty() {
050                values = new ArrayList<>();
051        }
052
053        /**
054         * Copy constructor.
055         * @param original the property to make a copy of
056         */
057        public ListProperty(ListProperty<T> original) {
058                super(original);
059                values = new ArrayList<>(original.values);
060        }
061
062        /**
063         * Gets the list that stores this property's values.
064         * @return the list of values (this list is mutable)
065         */
066        public List<T> getValues() {
067                return values;
068        }
069
070        @Override
071        protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
072                if (values.isEmpty()) {
073                        warnings.add(new ValidationWarning(8));
074                }
075        }
076
077        @Override
078        protected Map<String, Object> toStringValues() {
079                Map<String, Object> values = new LinkedHashMap<>();
080                values.put("values", this.values);
081                return values;
082        }
083
084        @Override
085        public int hashCode() {
086                final int prime = 31;
087                int result = super.hashCode();
088                result = prime * result + values.hashCode();
089                return result;
090        }
091
092        @Override
093        public boolean equals(Object obj) {
094                if (this == obj) return true;
095                if (!super.equals(obj)) return false;
096                ListProperty<?> other = (ListProperty<?>) obj;
097                if (!values.equals(other.values)) return false;
098                return true;
099        }
100}