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