| 1 | package ezvcard.property; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.LinkedHashMap; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import ezvcard.VCard; |
| 9 | import ezvcard.VCardVersion; |
| 10 | import ezvcard.ValidationWarning; |
| 11 | |
| 12 | /* |
| 13 | Copyright (c) 2012-2023, Michael Angstadt |
| 14 | All rights reserved. |
| 15 | |
| 16 | Redistribution and use in source and binary forms, with or without |
| 17 | modification, are permitted provided that the following conditions are met: |
| 18 | |
| 19 | 1. Redistributions of source code must retain the above copyright notice, this |
| 20 | list of conditions and the following disclaimer. |
| 21 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 22 | this list of conditions and the following disclaimer in the documentation |
| 23 | and/or other materials provided with the distribution. |
| 24 | |
| 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 26 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 27 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 29 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 30 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 32 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | |
| 36 | The views and conclusions contained in the software and documentation are those |
| 37 | of the authors and should not be interpreted as representing official policies, |
| 38 | either expressed or implied, of the FreeBSD Project. |
| 39 | */ |
| 40 | |
| 41 | /** |
| 42 | * Represents a property whose value is a list of textual values. |
| 43 | * @author Michael Angstadt |
| 44 | * @param <T> the type of values stored in the list |
| 45 | */ |
| 46 | public class ListProperty<T> extends VCardProperty { |
| 47 | protected final List<T> values; |
| 48 | |
| 49 | public ListProperty() { |
| 50 | values = new ArrayList<>(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Copy constructor. |
| 55 | * @param original the property to make a copy of |
| 56 | */ |
| 57 | public ListProperty(ListProperty<T> original) { |
| 58 | super(original); |
| 59 | values = new ArrayList<>(original.values); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Gets the list that stores this property's values. |
| 64 | * @return the list of values (this list is mutable) |
| 65 | */ |
| 66 | public List<T> getValues() { |
| 67 | return values; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) { |
| 72 | if (values.isEmpty()) { |
| 73 | warnings.add(new ValidationWarning(8)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected Map<String, Object> toStringValues() { |
| 79 | Map<String, Object> values = new LinkedHashMap<>(); |
| 80 | values.put("values", this.values); |
| 81 | return values; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | final int prime = 31; |
| 87 | int result = super.hashCode(); |
| 88 | result = prime * result + values.hashCode(); |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean equals(Object obj) { |
| 94 | if (this == obj) return true; |
| 95 | if (!super.equals(obj)) return false; |
| 96 | ListProperty<?> other = (ListProperty<?>) obj; |
| 97 | if (!values.equals(other.values)) return false; |
| 98 | return true; |
| 99 | } |
| 100 | } |